def test_should_return_true_to_wrong_password(self): user = UserLogin("user_id", "name", "login", "password") user_login_information = UserLogin("user_id", "name", "login", "password2") response = Validator.is_wrong_credentials(user, user_login_information) self.assertEqual(True, response)
def test_should_raise_that_user_exists(self): user = UserLogin(None, "name", "login", "password") new_user = UserLogin("user_id", "name", "login", "password") self.__user_repository_instance.get_user_by_login.return_value = new_user self.assertRaises(UserAlreadyExistsException, self.__user_business.add_new_user, user)
def test_should_raise_authentication_exception(self): user = UserLogin("user_id", "name", "login", "password") user_login_information = UserLogin("user_id", "name", "login", "password2") self.__user_repository_instance.get_user_by_id.return_value = user_login_information self.assertRaises(AuthenticationException, self.__user_business.authenticate, user)
def login(self, login_json: dict): self.__logger.info(f"Login user: {login_json['login']}") Validator.validate_user_dict_login(login_json) user = UserLogin(None, None, login_json["login"], login_json["password"]) user = self.__user_bussiness.authenticate(user) user = self.__user_bussiness.get_access_token(user) user_json = dumps(user.access_token_to_dict()) return Response(user_json, status=200)
def test_should_update_access_token(self): user = UserLogin("5c7c0c22fd8a2f36f6cdea65", "name", "login", "password") user.access_token = "access_token" self.__user_repository.set_access_token(user) self.assertTrue(self.__mongo_configuration_instance.database. user_collection.update_one.called)
def validate_token(self, access_token_json: dict): self.__logger.info( f"Validating access token user: {access_token_json['login']}") Validator.validate_user_dict_validate_access_token(access_token_json) user = UserLogin(None, None, access_token_json["login"], "") user.access_token = access_token_json["access_token"] self.__user_bussiness.get_user_by_login(user.login) self.__user_bussiness.validate_access_token(user) user_json = dumps(user.access_token_validate_to_dict()) return Response(user_json, status=200)
def test_should_pass_into_authentication(self): user = UserLogin("user_id", "name", "login", "password") user_login_information = UserLogin("user_id", "name", "login", "password") self.__user_repository_instance.get_user_by_login.return_value = user_login_information self.__user_business.authenticate(user) self.assertTrue( self.__user_repository_instance.get_user_by_login.called)
def test_should_add_new_user(self): user = UserLogin(None, "name", "login", "password") new_user = UserLogin("user_id", "name", "login", "password") self.__user_repository_instance.get_user_by_login.return_value = None self.__user_repository_instance.add_new_user.return_value = new_user response = self.__user_business.add_new_user(user) self.assertTrue( self.__user_repository_instance.get_user_by_login.called) self.assertTrue(self.__user_repository_instance.add_new_user.called) self.assertEqual(new_user, response)
def test_should_raise_access_token_exception(self): user = UserLogin("user_id", "name", "login", "password") self.__user_repository_instance.set_access_token.return_value = None self.assertRaises(AccessTokenException, self.__user_business.get_access_token, user)
def test_should_add_new_user(self): new_user = UserLogin("user_id", "name", "login", "password") self.__user_repository.add_new_user(new_user) self.assertTrue(self.__mongo_configuration_instance.database. user_collection.insert_one.called)
class TestUserLogin(unittest.TestCase): def setUp(self): self.__user_login = UserLogin("user_id", "user_name", "login", "password") def test_should_set_correct_user_login(self): expected_hash = UserHashPassword.hash_password("password") self.assertEqual(self.__user_login.user_id, "user_id", "Wrong User Id") self.assertEqual(self.__user_login.name, "user_name", "Wrong User Id") self.assertEqual(self.__user_login.login, "login", "Wrong Login") self.assertEqual(self.__user_login.password, expected_hash, "Wrong Password") def test_should_set_correct_user_login(self): expected_hash = UserHashPassword.hash_password("password") self.assertEqual(self.__user_login.user_id, "user_id", "Wrong User Id") self.assertEqual(self.__user_login.name, "user_name", "Wrong User Id") self.assertEqual(self.__user_login.login, "login", "Wrong Login") self.assertEqual(self.__user_login.password, expected_hash, "Wrong Password") def test_should_set_int_password(self): expected_hash = "password".__hash__() self.__user_login.password = expected_hash self.assertEqual(expected_hash, self.__user_login.password) def test_should_return_dict(self): data = self.__user_login.to_dict() self.assertEqual("user_id", data["id"], "Wrong User Login Dict Return") self.assertEqual("user_name", data["name"], "Wrong User Login Dict Return") self.assertEqual("login", data["login"], "Wrong User Login Dict Return")
def test_should_get_user_by_id(self): user = UserLogin("user_id", "name", "login", "password") self.__user_repository_instance.get_user_by_id.return_value = user response = self.__user_business.get_user_by_id("user_id") self.assertTrue(self.__user_repository_instance.get_user_by_id.called) self.assertEqual(user, response)
def get_user_by_login(self, login: str) -> UserLogin: self.__logger.info(f"Getting user by login: {login}") user = None for document in self.__user_collection.find({"login": login}): user_id = str(document["_id"]) user_name = str(document["name"]) user_login = str(document["login"]) user_password = str(document["password"]) user_access_token = str(document["access_token"]) user = UserLogin(user_id, user_name, user_login, "") user.password = user_password user.access_token = user_access_token return user
def add_new_user(self, user_json: dict): self.__logger.info(f"Adding an new user") Validator.validate_user_dict_add_user(user_json) user = UserLogin(None, user_json["name"], user_json["login"], user_json["password"]) new_user = self.__user_bussiness.add_new_user(user) new_user_json = dumps(new_user, cls=JsonObjectEncoder) return Response(new_user_json, status=201)
def test_should_add_new_user(self): user_json = {"name": "name", "login": "******", "password": "******"} new_user = UserLogin("user_id", "name", "login", "password") self.__user_business_instance.add_new_user.return_value = new_user response = self.__user_controller.add_new_user(user_json) self.assertTrue(self.__user_business_instance.add_new_user.called) self.assertEqual(201, response.status_code)
def test_should_login_an_user(self): login_json = {"login": "******", "password": "******"} user = UserLogin("user_id", "name", "login", "password") self.__user_business_instance.get_access_token.return_value = user response = self.__user_controller.login(login_json) self.assertTrue(self.__user_business_instance.authenticate.called) self.assertTrue(self.__user_business_instance.get_access_token.called) self.assertEqual(200, response.status_code)
def get_access_token(self, user: UserLogin): self.__logger.info( f"Login process for user - Access Token: {user.user_id}") access_token = self.__access_token_business.create_access_token(user) user.access_token = access_token created = self.__user_repository.set_access_token(user) if not created: raise AccessTokenException() return user
def test_should_return_false_to_correct_access_token(self): user = UserLogin("user_id", "name", "login", "password") user.access_token = "access_token" user_login_information = UserLogin("user_id", "name", "login", "password") user_login_information.access_token = "access_token" response = Validator.is_wrong_access_token(user, user_login_information) self.assertEqual(False, response)
def test_should_raise_non_valid_access_token_exception(self): user = UserLogin("user_id", "name", "login", "password") user.access_token = "access_token" user_login_information = UserLogin("user_id", "name", "login", "password") user_login_information.access_token = "access_token2" self.__user_repository_instance.get_user_by_login.return_value = user_login_information self.assertRaises(NonValidAccessTokenException, self.__user_business.validate_access_token, user)
def test_should_validate_access_token(self): user = UserLogin("user_id", "name", "login", "password") user.access_token = "access_token" user_login_information = UserLogin("user_id", "name", "login", "password") user_login_information.access_token = "access_token" self.__user_repository_instance.get_user_by_login.return_value = user_login_information self.__user_business.validate_access_token(user) self.assertTrue( self.__user_repository_instance.get_user_by_login.called)
def add_new_user(self, user: UserLogin) -> UserLogin: self.__logger.info(f"Inserting new user: {user.login}") mongo_object_id = self.__user_collection.insert_one( user.to_mongo_dict()).inserted_id user.user_id = str(mongo_object_id) return user
def setUp(self): self.__user = UserLogin("user_id", "name", "login", "password")
def setUp(self): self.__user = UserLogin("user_id", "name", "login", "password") self.__access_token_business = AccessTokenBusiness()