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
Пример #2
0
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")