def process_request(self, request): auth_header = request.META.get('HTTP_AUTHORIZATION', '') if auth_header.startswith('PDB'): token = auth_header[4:] user_service = UserService() user = user_service.get_user_by_auth_token(token) if user is not None: request.pdbuser = user else: request.pdbuser = None
def __init__(self, auth_service=None, user_service=None, *args, **kwargs): if auth_service is None: self.__auth_service = AuthService() else: self.__auth_service = auth_service if user_service is None: self.__user_service = UserService() else: self.__user_service = user_service super(UserViewSet, self).__init__(UserJsonSerializer(), *args, **kwargs)
class UserViewSet(BaseViewSet): def __init__(self, auth_service=None, user_service=None, *args, **kwargs): if auth_service is None: self.__auth_service = AuthService() else: self.__auth_service = auth_service if user_service is None: self.__user_service = UserService() else: self.__user_service = user_service super(UserViewSet, self).__init__(UserJsonSerializer(), *args, **kwargs) def list(self, request): if request.pdbuser is None: return self._create_generic_response( response_type=ResponseType.authentication_error) return self._create_response_by_inner_service_call( self.__user_service.get_ranking, message='users retrieved') def create(self, request): nickname = request.data.get('nickname', None) email = request.data.get('email', None) password = request.data.get('password', None) role = request.data.get('role', None) if email is None or password is None or nickname is None or role is None: return self._create_generic_response( response_type=ResponseType.missing_request_field) try: user = User.from_json(request.data) except Exception as e: return self._create_generic_response( response_type=ResponseType.server_error, exception=e) if UserFormatService().has_valid_fields(user): return self._create_response_by_inner_service_call( self.__user_service.create_user, user, message='user created') else: return self._create_generic_response( response_type=ResponseType.invalid_field_format) @list_route(methods=['POST'], url_path='login') def login(self, request, pk=None): email = request.data.get('email', None) password = request.data.get('password', None) if email is None or password is None: return self._create_generic_response( response_type=ResponseType.missing_request_field) user = self.__user_service.get_user_by_credentials(email=email, password=password) if user is None: return self._create_generic_response( response_type=ResponseType.authentication_error) else: return self._create_response_by_inner_service_call( self.__auth_service.authenticate, user.get_id(), message='user authenticated')
def __init__(self, exercise_evaluation_query_repository=None, exercise_evaluation_command_repository=None, user_service=None): if exercise_evaluation_query_repository is not None: self.__exercise_evaluation_query_repository = exercise_evaluation_query_repository else: exercise_evaluation_query_repository_factory = ExerciseEvaluationQueryRepositoryFactory( ) self.__exercise_evaluation_query_repository = exercise_evaluation_query_repository_factory.create_exercise_evaluation_query_repository( ) if exercise_evaluation_command_repository is not None: self.__exercise_evaluation_command_repository = exercise_evaluation_command_repository else: exercise_evaluation_command_repository_factory = ExerciseEvaluationCommandRepositoryFactory( ) self.__exercise_evaluation_command_repository = exercise_evaluation_command_repository_factory.create_exercise_evaluation_command_repository( ) if user_service is not None: self.__user_service = user_service else: self.__user_service = UserService()
def setUp(self): self.stub_user_query_repository = mock.Mock(spec=IUserQueryRepository) self.stub_user_command_repository = mock.Mock(spec=IUserCommandRepository) self.sut = UserService(self.stub_user_query_repository, self.stub_user_command_repository)
class UserServiceUnitTest(unittest.TestCase): def setUp(self): self.stub_user_query_repository = mock.Mock(spec=IUserQueryRepository) self.stub_user_command_repository = mock.Mock(spec=IUserCommandRepository) self.sut = UserService(self.stub_user_query_repository, self.stub_user_command_repository) @data( {'email': None, 'password': '******'}, {'email': '*****@*****.**', 'password': None}, {'email': None, 'password': None} ) def test_getUserByCredentials_calledWithNoneParams_raiseValueError(self, input): self.assertRaises(ValueError, self.sut.get_user_by_credentials, email=input['email'], password=input['password']) def test_getUserByCredentials_calledWithQueryRepositoryWhichRaisesQueryError_raiseServiceError(self): self.stub_user_query_repository.get_user_by_credentials.side_effect = QueryError() self.assertRaises(ServiceError, self.sut.get_user_by_credentials, email='*****@*****.**', password='******') def test_getUserByCredentials_calledWithValidParams_innerQueryRepositoryCalledWithValidParams(self): self.sut.get_user_by_credentials('*****@*****.**', 'testpwd') self.stub_user_query_repository.get_user_by_credentials.assert_called_once_with('*****@*****.**', 'testpwd') def test_getUserByAuthToken_calledWithNoneParams_raiseValueError(self): self.assertRaises(ValueError, self.sut.get_user_by_auth_token, token=None) def test_getUserByAuthToken_calledWithQueryRepositoryWhichRaisesQueryError_raiseServiceError(self): self.stub_user_query_repository.get_user_by_auth_token.side_effect = QueryError() self.assertRaises(ServiceError, self.sut.get_user_by_auth_token, token='faketoken') def test_getUserByAuthToken_calledWithValidParams_innerQueryRepositoryCalledWithValidParams(self): self.sut.get_user_by_auth_token('faketoken') self.stub_user_query_repository.get_user_by_auth_token.assert_called_once_with('faketoken') def test_createUser_calledWithNoneUser_raiseValueError(self): self.assertRaises(ValueError, self.sut.create_user, None) def test_createUser_calledWithExistentEmail_raiseServiceError(self): self.stub_user_query_repository.get_user_by_nickname.return_value = None self.stub_user_query_repository.get_user_by_email.return_value = self.__get_user_test_instance() self.assertRaises(ServiceError, self.sut.create_user, self.__get_user_test_instance()) def test_createUser_calledWithCommandRepositoryWhichRaisesCommandError_raiseServiceError(self): self.stub_user_command_repository.create_user.side_effect = CommandError() self.assertRaises(ServiceError, self.sut.create_user, self.__get_user_test_instance()) def test_createUser_calledWithValidParams_correctCallToInnerCommandRepositoryWithRightParams(self): self.stub_user_query_repository.get_user_by_nickname.return_value = None self.stub_user_query_repository.get_user_by_email.return_value = None test_user = self.__get_user_test_instance() self.sut.create_user(test_user) self.stub_user_command_repository.create_user.assert_called_once_with(test_user) def test_createUser_calledWithExistentNickname_raiseServiceError(self): self.stub_user_query_repository.get_user_by_nickname.return_value = self.__get_user_test_instance() self.stub_user_query_repository.get_user_by_email.return_value = None self.assertRaises(ServiceError, self.sut.create_user, self.__get_user_test_instance()) @data( {'user_id': None, 'score': 10}, {'user_id': 'testid', 'score': None}, ) def test_incrementUserScore_calledWithNoneParams_raiseValueError(self, input): self.assertRaises(ValueError, self.sut.increment_user_score, user_id=input['user_id'], score=input['score']) def test_incrementUserScore_calledWithCommandRepositoryWhichRaisesCommandError_raiseServiceError(self): self.stub_user_command_repository.increment_user_score.side_effect = CommandError() self.assertRaises(ServiceError, self.sut.increment_user_score, user_id='test', score=10) def test_incrementUserScore_calledWithValidParams_innerCommandRepositoryCalledWithValidParams(self): self.sut.increment_user_score('test', 10) self.stub_user_command_repository.increment_user_score.assert_called_once_with('test', 10) def test_getRanking_calledWithQueryRepositoryWhichRaisesQueryError_raiseServiceError(self): self.stub_user_query_repository.get_ranking.side_effect = QueryError() self.assertRaises(ServiceError, self.sut.get_ranking) def test_getRanking_called_innerQueryRepositoryCalled(self): self.sut.get_ranking() self.stub_user_query_repository.get_ranking.assert_called_once_with() def __get_user_test_instance(self): return User("*****@*****.**", "pwdpwdpwd", "master", "testnickname")
class ExerciseEvaluationService: def __init__(self, exercise_evaluation_query_repository=None, exercise_evaluation_command_repository=None, user_service=None): if exercise_evaluation_query_repository is not None: self.__exercise_evaluation_query_repository = exercise_evaluation_query_repository else: exercise_evaluation_query_repository_factory = ExerciseEvaluationQueryRepositoryFactory( ) self.__exercise_evaluation_query_repository = exercise_evaluation_query_repository_factory.create_exercise_evaluation_query_repository( ) if exercise_evaluation_command_repository is not None: self.__exercise_evaluation_command_repository = exercise_evaluation_command_repository else: exercise_evaluation_command_repository_factory = ExerciseEvaluationCommandRepositoryFactory( ) self.__exercise_evaluation_command_repository = exercise_evaluation_command_repository_factory.create_exercise_evaluation_command_repository( ) if user_service is not None: self.__user_service = user_service else: self.__user_service = UserService() def get_exercise_evaluation(self, user_id, exercise_id): if user_id is None: raise ValueError('user_id cannot be None') if exercise_id is None: raise ValueError('exercise_id cannot be None') try: return self.__exercise_evaluation_query_repository.get_exercise_evaluation( user_id, exercise_id) except QueryError as qe: raise ServiceError(str(qe)) def create_exercise_evaluation(self, exercise_evaluation): if exercise_evaluation is None: raise ValueError('Exercise evaluation cannot be None') try: self.__exercise_evaluation_command_repository.create_exercise_evaluation( exercise_evaluation=exercise_evaluation) except CommandError as ce: raise ServiceError(str(ce)) def increment_exercise_evaluation_attempts(self, exercise_evaluation): if exercise_evaluation is None: raise ValueError('Exercise evaluation cannot be None') try: self.__exercise_evaluation_command_repository.increment_exercise_evaluation_attempts( exercise_evaluation=exercise_evaluation) except CommandError as ce: raise ServiceError(str(ce)) def update_exercise_evaluation_as_solved(self, user_id, exercise_id, time_left): if user_id is None: raise ValueError('user_id cannot be None') if exercise_id is None: raise ValueError('exercise_id cannot be None') if time_left is None: raise ValueError('leftover_time cannot be None') exercise_evaluation = self.get_exercise_evaluation( user_id, exercise_id) score = self.__calculate_exercise_evaluation_score( exercise_evaluation.get_attempt(), time_left) try: self.__exercise_evaluation_command_repository.update_exercise_evaluation_as_solved( exercise_evaluation, score) self.__user_service.increment_user_score(user_id, score) except CommandError as ce: raise ServiceError(str(ce)) def __calculate_exercise_evaluation_score(self, attempt, time_left): return int(float(time_left) / attempt)