def test_get_response_for_login_with_valid_details_returns_user_token_dict(): # Arrange presenter = PresenterImplementation() tokens_dto = UserAuthTokensDTO( user_id=1, access_token="123456", refresh_token="654321", expires_in=datetime.datetime ) is_admin = False expected_dict = { "access_token": "123456", "refresh_token": "654321", "is_admin": False } # Act response = presenter.get_response_for_login( tokens_dto=tokens_dto, is_admin=is_admin ) # Assert assert response == expected_dict
def user_auth_token_dto(self): from common.dtos import UserAuthTokensDTO auth_token_dto = UserAuthTokensDTO(user_id=1, access_token="test_access_token", refresh_token="test_refresh_token", expires_in=1000000) return auth_token_dto
def create_user_auth_tokens(self, user_id): from django.conf import settings from project_management_portal.storages.user_storage_implementation\ import UserStorageImplementation user_storage = UserStorageImplementation() is_admin = user_storage.is_admin(user_id) application, _ = self.oauth2_storage.get_or_create_default_application( user_id=user_id) if is_admin: token_scopes = "read write superuser" else: token_scopes = settings.DEFAULT_OAUTH_SCOPES access_token_obj = self.oauth2_storage.create_access_token( user_id=user_id, application_id=application.application_id, scopes=token_scopes, expiry_in_seconds=settings.DEFAULT_ACCESS_TOKEN_EXPIRY_IN_SECONDS) refresh_token_obj = self.oauth2_storage.create_refresh_token( user_id=user_id, application_id=application.application_id, access_token_id=access_token_obj.access_token_id) return UserAuthTokensDTO(user_id=user_id, access_token=access_token_obj.token, refresh_token=refresh_token_obj.token, expires_in=access_token_obj.expires)
def OAuthTokenDto(): oauth_dto = UserAuthTokensDTO(user_id=1, access_token="kjfewrfjbwg", refresh_token="sjdfbkgfsdg", expires_in=1000000) return oauth_dto
def user_token_dto(): from common.dtos import UserAuthTokensDTO user_access_token = UserAuthTokensDTO(access_token="user_access_token", refresh_token="user_refresh_token", expires_in=datetime.datetime( 2020, 5, 27, 11, 13, 44, 954147), user_id=1) return user_access_token
def test_case(self, user_token_dto): import json self.create_admin() user_token_dto = UserAuthTokensDTO(user_id=1, access_token="user access token", refresh_token="user refresh token", expires_in=10000000) OAuthUserAuthTokensService.create_user_auth_tokens.return_value = \ user_token_dto response = self.default_test_case() response_content = json.loads(response.content) self.assert_match_snapshot(name="token response", value=response_content)
def test_case(self, create_user_auth_tokens): user_auth_token_dto = UserAuthTokensDTO(user_id=28, access_token="token", refresh_token="refresh_token", expires_in=10000) create_user_auth_tokens.return_value = user_auth_token_dto user = User.objects.create(username="******", password="******", user_role="") response = self.default_test_case() response_content = json.loads(response.content) self.assert_match_snapshot(name="username", value=user.username) self.assert_match_snapshot(name="user_id", value=response_content['user_id']) self.assert_match_snapshot(name="access_token", value=response_content['access_token']) self.assert_match_snapshot(name="user_role", value=response_content['user_role'])
def test_for_login_interactor(self, create_user_auth_tokens): # Arrange username = "******" password = "******" user_role_dto = UserRoleDto(user_id=1, user_role="USER") user_access_token_dto = UserAuthTokensDTO( user_id=1, access_token="token", refresh_token="refresh_token", expires_in=100000) login_response = { "user_id": 1, "access_token": "token", "role": "USER" } storage = create_autospec(StorageInterface) presenter = create_autospec(PresenterInterface) oauth2_storage = create_autospec(OAuth2SQLStorage) interactor = LoginInteractor(storage=storage, oauth2_storage=oauth2_storage) storage.validate_username.return_value = True storage.validate_password.return_value = True storage.get_user_role_dto.return_value = user_role_dto create_user_auth_tokens.return_value = user_access_token_dto presenter.get_response_for_login_interactor = login_response # Act response = interactor.login_wrapper(username=username, password=password, presenter=presenter) # Assert storage.validate_username \ .assert_called_once_with(username=username) storage.validate_password \ .assert_called_once_with(username=username, password=password) storage.get_user_role_dto \ .assert_called_once_with(username=username, password=password)
def create_user_auth_tokens(self, user_id): from django.conf import settings application, _ = self.oauth2_storage.get_or_create_default_application( user_id=user_id) access_token_obj = self.oauth2_storage.create_access_token( user_id=user_id, application_id=application.application_id, scopes=settings.DEFAULT_OAUTH_SCOPES, expiry_in_seconds=settings.DEFAULT_ACCESS_TOKEN_EXPIRY_IN_SECONDS) refresh_token_obj = self.oauth2_storage.create_refresh_token( user_id=user_id, application_id=application.application_id, access_token_id=access_token_obj.access_token_id) return UserAuthTokensDTO(user_id=user_id, access_token=access_token_obj.token, refresh_token=refresh_token_obj.token, expires_in=access_token_obj.expires)
def test_user_login_with_valid_values(self): #arrange from .raw_inputs import user_dto from .expected_responses import access_token_dto username = "******" password = "******" user_id = 1 storage = create_autospec(UserStorageInterface) presenter = create_autospec(UserPresenterInterface) oauth_storage = create_autospec(OAuth2SQLStorage) interactor = UserLoginInteractor(storage=storage, oauth_storage=oauth_storage) access_token_dto = UserAuthTokensDTO( user_id=1, access_token="testing_access_token", refresh_token="testing_refresh_token", expires_in=10000000) storage.validate_username.return_value = user_id storage.validate_login_credentials.return_value = True storage.get_user_dtos.return_value = user_dto presenter.get_login_response.return_value = valid_login_response #act with patch.object(OAuthUserAuthTokensService, "create_user_auth_tokens", return_value=access_token_dto): response = interactor.login_wrapper(username=username, password=password, presenter=presenter) #assert storage.validate_username.assert_called_once_with(username=username) storage.validate_login_credentials.assert_called_once_with( username=username, password=password) storage.get_user_dtos.assert_called_once_with(user_id) presenter.get_login_response.assert_called_once_with( user_dto, access_token_dto) assert response == valid_login_response
def test_get_access_token_response_with_valid_details_return_dto(): # Arrange access_token_dto = UserAuthTokensDTO(user_id=1, access_token="QTRTUGFTTTBBBBNS1334", refresh_token="WQEDRTT#$%^REDET", expires_in=datetime.datetime( 2020, 10, 10, 0, 0, 0)) expected_access_token_dict = { "user_id": 1, "access_token": "QTRTUGFTTTBBBBNS1334", "refresh_token": "WQEDRTT#$%^REDET", "expires_in": "2020-10-10 00:00:00.000000" } user_presenter = UserPresenterImplementation() # Act actual_access_token_dict = user_presenter.get_access_token_response( access_token_dto) # Assert assert actual_access_token_dict == expected_access_token_dict
from unittest.mock import create_autospec, patch from lets_ride.interactors.create_user_interactor import CreateUserInteractor from lets_ride.interactors.presenters.presenter_interface \ import PresenterInterface from lets_ride.interactors.storages.post_storage_interface \ import PostStorageInterface import common from common.dtos import UserAuthTokensDTO from common.oauth2_storage import OAuth2SQLStorage from common.oauth_user_auth_tokens_service import OAuthUserAuthTokensService @pytest UserAuthTokensDTO( user_id = user_id, access_token = "fyiduasf", refresh_token = "fhjskfjaisk", expires_in = "2020, 12, 5" ) @patch.object(OAuthUserAuthTokensService, "oauth_user_auth_tokens_service", return_value=UserAuthTokensDTO) def test_create_user_returns_user_id(oauth_user_auth_tokens_service): #Arrange mobile_number = "0932825493" user_name = "user1" password = "******" user_id = 1 expected_output = {"user_id": user_id} storage = create_autospec(PostStorageInterface) presenter = create_autospec(PresenterInterface) oauth_storage = create_autospec(OAuth2SQLStorage) service = create_autospec(OAuthUserAuthTokensService(oauth2_storage=oauth_storage))
class TestLoginInteractor: def test_login_interactor_with_invalid_username_raises_error(self): # Arrange username = '******' password = '******' oauth2_storage = create_autospec(OAuth2SQLStorage) storage = create_autospec(UserStorageInterface) presenter = create_autospec(PresenterInterface) storage.is_valid_username.side_effect = InvalidUsername presenter.raise_exception_for_invalid_username.side_effect = BadRequest interactor = LoginInteractor( storage=storage, oauth2storage=oauth2_storage, ) # Act with pytest.raises(BadRequest): interactor.login_wrapper(username=username, password=password, presenter=presenter) # Assert storage.is_valid_username.assert_called_once_with(username=username) presenter.raise_exception_for_invalid_username.assert_called_once() def test_login_interactor_with_invalid_password_raises_error(self): # Arrange username = '******' password = '******' oauth2_storage = create_autospec(OAuth2SQLStorage) storage = create_autospec(UserStorageInterface) presenter = create_autospec(PresenterInterface) storage.is_valid_password.side_effect = InvalidPassword presenter.raise_exception_for_invalid_password.side_effect = BadRequest interactor = LoginInteractor( storage=storage, oauth2storage=oauth2_storage, ) # Act with pytest.raises(BadRequest): interactor.login_wrapper(username=username, password=password, presenter=presenter) # Assert storage.is_valid_username.assert_called_once_with(username=username) storage.is_valid_password.assert_called_once_with(username=username, password=password) presenter.raise_exception_for_invalid_password.assert_called_once() @patch.object(OAuthUserAuthTokensService, 'create_user_auth_tokens', return_value=UserAuthTokensDTO(user_id=1, access_token="123456", refresh_token="654321", expires_in=datetime.datetime( 2020, 6, 30))) def test_login_interactor_with_valid_details_returns_user_details_dict( self, auth_service_mock): # Arrange username = '******' password = '******' mock_presenter_response = { "access_token": "123456", "refresh_token": "654321", "is_admin": False } user_dto = UserDetailsDto(user_id=1, is_admin=False) oauth2_storage = create_autospec(OAuth2SQLStorage) storage = create_autospec(UserStorageInterface) presenter = create_autospec(PresenterInterface) storage.is_valid_password.return_value = user_dto presenter.get_response_for_login.return_value = \ mock_presenter_response interactor = LoginInteractor(storage=storage, oauth2storage=oauth2_storage) # Act response = interactor.login_wrapper(username=username, password=password, presenter=presenter) # Assert assert response == mock_presenter_response
from datetime import datetime from common.dtos import UserAuthTokensDTO from user_app.dtos\ import UserDto, ProjectDto,\ ProjectDetailsDto, TransitionDto,\ StateDto, TaskDetailsDto, WorkflowDetailsDto,\ TransitionDetailsDto, ChecklistDetailsDto,\ GetTransitionDetailsDto, StateDetailsDto from .raw_inputs import project_data, project_details_dto, user_dto access_token_dto = UserAuthTokensDTO(user_id=1, access_token="testing_access_token", refresh_token="testing_refresh_token", expires_in=10000000) valid_login_response = { "user_id": 1, "username": "******", "profile_pic": "http://www.google.com/", "is_admin": True, "access_token": "testing_access_token", "refresh_token": "testing_refresh_token", "expires_in": 10000000 } user_details = { "user_id": 1, "username": "******", "profile_pic": "http://www.google.com/", "phone_no": "8739835635"
def get_access_token_dto_fixture(): return UserAuthTokensDTO(user_id=1, access_token="DyRngpHdOSdrsiI12g12", refresh_token="31ty2yu2e2veut", expires_in=str(datetime.datetime.now()))
import pytest from resource_management_auth.presenters.presenter_implementation \ import PresenterImplementation from resource_management_auth.storages.storage_implementation \ import StorageImplementation from unittest.mock import create_autospec, patch from resource_management_auth.interactors.\ user_login_interactor import UserLoginInteractor from common.oauth2_storage import OAuth2SQLStorage from common.oauth_user_auth_tokens_service import OAuthUserAuthTokensService from common.dtos import UserAuthTokensDTO user_auth_dto= UserAuthTokensDTO( user_id=1, access_token="ghkddddddddd", refresh_token="fdhhhhhhh", expires_in="2025,12,5" ) class TestPresenterImplementation: @pytest.mark.django_db def test_user_login_raises_exception_for_invalid_user_name(self, snapshot, create_user): # Arrange user_name = "kavya" password = "******" storage = StorageImplementation() presenter = PresenterImplementation() oauth_storage = create_autospec(OAuth2SQLStorage) interactor = UserLoginInteractor( storage=storage, oauth_storage=oauth_storage)
from covid_dashboard.interactors.presenters.presenter_interface\ import PresenterInterface from covid_dashboard.interactors.login_interactor\ import LoginUserInteractor from common.oauth2_storage import OAuth2SQLStorage from common.oauth_user_auth_tokens_service\ import OAuthUserAuthTokensService from common.dtos import UserAuthTokensDTO from covid_dashboard.exceptions.exceptions\ import InvalidUserName, InvalidPassword from common.oauth_user_auth_tokens_service\ import OAuthUserAuthTokensService expected_user_auth_token_dto = UserAuthTokensDTO( user_id=1, access_token="HiplEtNJKFIPRsyDSDqrH7GHkSHSzq", refresh_token="DIxY1vyGQRkN7zJKqE4MZaq73tpKDj3ee444444444444e", expires_in=datetime.datetime(2020, 9, 21, 3, 45, 50, 163595)) def test_login_user_interactor_with_invalid_username(): # Arrange username = '******' password = "******" storage = create_autospec(UserStorageInterface) presenter = create_autospec(PresenterInterface) oauth_storage = OAuth2SQLStorage() storage.is_valid_username.side_effect = InvalidUserName presenter.raiseinvalidusername.side_effect = InvalidUserName