Advertisement
MaximCherchuk

coro

Nov 13th, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.04 KB | None | 0 0
  1. import asyncio
  2. from asyncio import coroutine
  3. from unittest import TestCase
  4. from unittest.mock import Mock
  5. from unittest.mock import patch
  6.  
  7. from cognito import CognitoAuthDomainService
  8. from domain.models.customer import Customer
  9. from domain import exceptions
  10. from domain import models
  11.  
  12. from tests.exceptions import (
  13.     UserNotFoundException,
  14.     NotAuthorizedException,
  15. )
  16.  
  17.  
  18. def get_coroutine_mock():
  19.     coro = Mock(name="corutine_result")
  20.     coroutine_function = Mock(
  21.         name="coroutine_function",
  22.         side_effect=coroutine(coro),
  23.     )
  24.     coroutine_function.coro = coro
  25.     return coroutine_function
  26.  
  27.  
  28. class TestCognitoPasswordAuth(TestCase):
  29.  
  30.     def setUp(self):
  31.         settings_mock = Mock()
  32.         settings_mock.customer_user_pool_id = 'customer_user_pool_id'
  33.         settings_mock.app_client_id = 'app_client_id'
  34.         settings_mock.user_pool_region = 'region'
  35.         settings_mock.app_client_secret = 'app_client_secret'
  36.         settings_mock.jwks = 'jwks'
  37.  
  38.         self.cognito_auth_domain_service = CognitoAuthDomainService(
  39.             cognito_settings=settings_mock,
  40.             logger=Mock(),
  41.         )
  42.  
  43.         self.customer = Customer(
  44.             first_name='first_name',
  45.             last_name='last_name',
  46.             profile_id='123123_4564564',
  47.             email='example@example.com',
  48.             auth_id='auth_id',
  49.             phone_number='+375291101128',
  50.         )
  51.  
  52.         self.password = 'password'
  53.  
  54.     def _run_helper(self, coroutine, *args):
  55.         return asyncio.get_event_loop().run_until_complete(coroutine(*args))
  56.  
  57.     @patch(
  58.         'cognito.library.identity.AsyncCognito.calc_expires_in',
  59.     )
  60.     @patch(
  61.         'cognito.library.identity.AsyncCognito.authenticate_username_password',
  62.         new_callable=get_coroutine_mock,
  63.     )
  64.     def test_password_auth__success(
  65.         self,
  66.         authenticate_username_password_mock,
  67.         calc_expires_in_mock,
  68.     ):
  69.         """Tests if the password auth function returns a correct value.
  70.        Parameters
  71.        ----------
  72.        authenticate_username_password_mock : Mock
  73.            Mock for the AsyncCognito.authenticate_username_password function.
  74.        calc_expires_in_mock : Mock
  75.            Mock for the AsyncCognito.calc_expires_in function.
  76.        """
  77.         calc_expires_in_mock.return_value = 100
  78.  
  79.         async def get_token_set():
  80.             async with self.cognito_auth_domain_service.cognito_getter() as cognito:
  81.                 with patch.object(cognito, 'access_token', 'access_token'), \
  82.                      patch.object(cognito, 'id_token', 'id_token'), \
  83.                      patch.object(cognito, 'refresh_token', 'refresh_token'), \
  84.                      patch.object(cognito, 'token_type', 'token_type'):
  85.  
  86.                         cognito_token_set = models.TokenSet(
  87.                             access_token=cognito.access_token,
  88.                             id_token=cognito.id_token,
  89.                             refresh_token=cognito.refresh_token,
  90.                             token_type=cognito.token_type,
  91.                             expires_in=cognito.calc_expires_in(),
  92.                         )
  93.                         return cognito_token_set
  94.  
  95.         self._run_helper(
  96.             self.cognito_auth_domain_service.password_auth,
  97.             self.customer,
  98.             self.password,
  99.         )
  100.  
  101.         token_set = self._run_helper(get_token_set)
  102.  
  103.         correct_token_set = models.TokenSet(
  104.             access_token='access_token',
  105.             id_token='id_token',
  106.             refresh_token='refresh_token',
  107.             token_type='token_type',
  108.             expires_in=100,
  109.         )
  110.  
  111.         self.assertEqual(token_set.access_token, correct_token_set.access_token)
  112.         self.assertEqual(token_set.id_token, correct_token_set.id_token)
  113.         self.assertEqual(token_set.refresh_token, correct_token_set.refresh_token)
  114.         self.assertEqual(token_set.token_type, correct_token_set.token_type)
  115.         self.assertEqual(token_set.expires_in, correct_token_set.expires_in)
  116.  
  117.     @patch(
  118.         'cognito.library.identity.AsyncCognito.calc_expires_in',
  119.     )
  120.     @patch(
  121.         'cognito.library.identity.AsyncCognito.authenticate_username_password',
  122.         new_callable=get_coroutine_mock,
  123.     )
  124.     def test_password_auth__returns_token_set(
  125.         self,
  126.         authenticate_username_password_mock,
  127.         calc_expires_in_mock,
  128.     ):
  129.         """Tests if the password auth function return value type is TokenSet.
  130.        Parameters
  131.        ----------
  132.        authenticate_username_password_mock : Mock
  133.            Mock for the AsyncCognito.authenticate_username_password function.
  134.        calc_expires_in_mock : Mock
  135.            Mock for the AsyncCognito.calc_expires_in function.
  136.        """
  137.         calc_expires_in_mock.return_value = 100
  138.  
  139.         coroutine_result = self._run_helper(
  140.             self.cognito_auth_domain_service.password_auth,
  141.             self.customer,
  142.             self.password,
  143.         )
  144.  
  145.         self.assertTrue(isinstance(coroutine_result, models.TokenSet))
  146.  
  147.     @patch(
  148.         'cognito.library.identity.AsyncCognito.authenticate_username_password',
  149.         new_callable=get_coroutine_mock,
  150.     )
  151.     def test_password_auth__user_not_found_exception(
  152.         self,
  153.         authenticate_username_password_mock,
  154.     ):
  155.         """Tests if the password auth function raises
  156.        domain.exceptions.FailedLoginException.
  157.        Parameters
  158.        ----------
  159.        authenticate_username_password_mock : Mock
  160.            Mock for the AsyncCognito.authenticate_username_password function.
  161.        """
  162.         authenticate_username_password_mock.coro.side_effect = Mock(
  163.             side_effect=UserNotFoundException(
  164.                 message='Invalid username / password',
  165.             ),
  166.         )
  167.  
  168.         with self.assertRaises(exceptions.FailedLoginException):
  169.             self._run_helper(
  170.                 self.cognito_auth_domain_service.password_auth,
  171.                 self.customer,
  172.                 self.password,
  173.             )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement