Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- from asyncio import coroutine
- from unittest import TestCase
- from unittest.mock import Mock
- from unittest.mock import patch
- from cognito import CognitoAuthDomainService
- from domain.models.customer import Customer
- from domain import exceptions
- from domain import models
- from tests.exceptions import (
- UserNotFoundException,
- NotAuthorizedException,
- )
- def get_coroutine_mock():
- coro = Mock(name="corutine_result")
- coroutine_function = Mock(
- name="coroutine_function",
- side_effect=coroutine(coro),
- )
- coroutine_function.coro = coro
- return coroutine_function
- class TestCognitoPasswordAuth(TestCase):
- def setUp(self):
- settings_mock = Mock()
- settings_mock.customer_user_pool_id = 'customer_user_pool_id'
- settings_mock.app_client_id = 'app_client_id'
- settings_mock.user_pool_region = 'region'
- settings_mock.app_client_secret = 'app_client_secret'
- settings_mock.jwks = 'jwks'
- self.cognito_auth_domain_service = CognitoAuthDomainService(
- cognito_settings=settings_mock,
- logger=Mock(),
- )
- self.customer = Customer(
- first_name='first_name',
- last_name='last_name',
- profile_id='123123_4564564',
- email='example@example.com',
- auth_id='auth_id',
- phone_number='+375291101128',
- )
- self.password = 'password'
- def _run_helper(self, coroutine, *args):
- return asyncio.get_event_loop().run_until_complete(coroutine(*args))
- @patch(
- 'cognito.library.identity.AsyncCognito.calc_expires_in',
- )
- @patch(
- 'cognito.library.identity.AsyncCognito.authenticate_username_password',
- new_callable=get_coroutine_mock,
- )
- def test_password_auth__success(
- self,
- authenticate_username_password_mock,
- calc_expires_in_mock,
- ):
- """Tests if the password auth function returns a correct value.
- Parameters
- ----------
- authenticate_username_password_mock : Mock
- Mock for the AsyncCognito.authenticate_username_password function.
- calc_expires_in_mock : Mock
- Mock for the AsyncCognito.calc_expires_in function.
- """
- calc_expires_in_mock.return_value = 100
- async def get_token_set():
- async with self.cognito_auth_domain_service.cognito_getter() as cognito:
- with patch.object(cognito, 'access_token', 'access_token'), \
- patch.object(cognito, 'id_token', 'id_token'), \
- patch.object(cognito, 'refresh_token', 'refresh_token'), \
- patch.object(cognito, 'token_type', 'token_type'):
- cognito_token_set = models.TokenSet(
- access_token=cognito.access_token,
- id_token=cognito.id_token,
- refresh_token=cognito.refresh_token,
- token_type=cognito.token_type,
- expires_in=cognito.calc_expires_in(),
- )
- return cognito_token_set
- self._run_helper(
- self.cognito_auth_domain_service.password_auth,
- self.customer,
- self.password,
- )
- token_set = self._run_helper(get_token_set)
- correct_token_set = models.TokenSet(
- access_token='access_token',
- id_token='id_token',
- refresh_token='refresh_token',
- token_type='token_type',
- expires_in=100,
- )
- self.assertEqual(token_set.access_token, correct_token_set.access_token)
- self.assertEqual(token_set.id_token, correct_token_set.id_token)
- self.assertEqual(token_set.refresh_token, correct_token_set.refresh_token)
- self.assertEqual(token_set.token_type, correct_token_set.token_type)
- self.assertEqual(token_set.expires_in, correct_token_set.expires_in)
- @patch(
- 'cognito.library.identity.AsyncCognito.calc_expires_in',
- )
- @patch(
- 'cognito.library.identity.AsyncCognito.authenticate_username_password',
- new_callable=get_coroutine_mock,
- )
- def test_password_auth__returns_token_set(
- self,
- authenticate_username_password_mock,
- calc_expires_in_mock,
- ):
- """Tests if the password auth function return value type is TokenSet.
- Parameters
- ----------
- authenticate_username_password_mock : Mock
- Mock for the AsyncCognito.authenticate_username_password function.
- calc_expires_in_mock : Mock
- Mock for the AsyncCognito.calc_expires_in function.
- """
- calc_expires_in_mock.return_value = 100
- coroutine_result = self._run_helper(
- self.cognito_auth_domain_service.password_auth,
- self.customer,
- self.password,
- )
- self.assertTrue(isinstance(coroutine_result, models.TokenSet))
- @patch(
- 'cognito.library.identity.AsyncCognito.authenticate_username_password',
- new_callable=get_coroutine_mock,
- )
- def test_password_auth__user_not_found_exception(
- self,
- authenticate_username_password_mock,
- ):
- """Tests if the password auth function raises
- domain.exceptions.FailedLoginException.
- Parameters
- ----------
- authenticate_username_password_mock : Mock
- Mock for the AsyncCognito.authenticate_username_password function.
- """
- authenticate_username_password_mock.coro.side_effect = Mock(
- side_effect=UserNotFoundException(
- message='Invalid username / password',
- ),
- )
- with self.assertRaises(exceptions.FailedLoginException):
- self._run_helper(
- self.cognito_auth_domain_service.password_auth,
- self.customer,
- self.password,
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement