def test_user_repository_find_by_id_not_found(self): user_repository = SQLUserRepository(self.session) id_user_1 = UserId(id=str(uuid.uuid4())) wrong_id_user = UserId(id=str(uuid.uuid4())) user1 = User( id=id_user_1, username="******", email="*****@*****.**", password='******', ) user_repository.save(user1) user2 = User( id=UserId(id=str(uuid.uuid4())), username="******", email="*****@*****.**", password='******', ) user_repository.save(user2) assert user_repository.find_by_id(wrong_id_user) is None
def test_user_save(self): user_id = UserId(id=str(uuid.uuid4())) user = User( id=user_id, username="******", email="*****@*****.**", password='******' ) user_repository = SQLUserRepository(self.session) user_repository.save(user) retrieved_user = user_repository.find_by_id(user_id) assert retrieved_user.id == user.id
def test_list_with_filtered_results(self): user_id = UserId(id=str(uuid.uuid4())) user_mock = User( id=user_id, username='******', email='*****@*****.**', password='******' ) self.uow.repository.all = MagicMock(return_value=[user_mock]) user_usecases = UserUseCases(self.uow, self.pwd_encoder) user_query = UserQuery(q='mock') self.assertEqual(user_usecases.list(user_query), [user_mock])
def test_user_repository_find_by_username(self): user_repository = SQLUserRepository(self.session) user = User( id=UserId(id=str(uuid.uuid4())), username="******", email="*****@*****.**", password='******', ) user_repository.save(user) user2 = User( id=UserId(id=str(uuid.uuid4())), username="******", email="*****@*****.**", password='******', ) user_repository.save(user2) assert user_repository.find_by_username("tesst5") == user2
def test_valid_credentials(self): user = User(id=UserId("id"), username="******", password="******", email="*****@*****.**") token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.5mhBHqs5_DTLdINd9p5m7ZJ6XD0Xc55kIaCRY5r6HRA" self.user_usecases.user_uow.repository.find_by_username = MagicMock( return_value=user) user_result = self.jwt_authenticator.authenticate(token) assert user_result.email == user.email
def test_user_repository_all(self): user_repository = SQLUserRepository(self.session) user1 = User( id=UserId(id=str(uuid.uuid4())), username="******", email="*****@*****.**", password='******', ) user_repository.save(user1) assert user_repository.all() == [user1]
def test_user_repository_all_filtered_empty(self): user_repository = SQLUserRepository(self.session) user1 = User( id=UserId(id=str(uuid.uuid4())), username="******", email="*****@*****.**", password='******', ) user_repository.save(user1) assert user_repository.all(q='InvalidMock') == []
def to_entity(self) -> User: return User( id=UserId(self.id), email=self.email, username=self.username, full_name=self.full_name, password=self.hashed_password, status=UserStatus(self.status), created_at=self.created_at, updated_at=self.updated_at, )
def test_register(self): user_id = UserId(id=str(uuid.uuid4())) user_mock = User( id=user_id, username='******', email='*****@*****.**', password='******' ) self.uow.repository.find_by_email_or_username = MagicMock(return_value=None) self.uow.repository.save = MagicMock(return_value=user_mock) self.pwd_encoder.encode = MagicMock(return_value='aaaa') user_usecases = UserUseCases(self.uow, self.pwd_encoder) user_register = UserCreateCommand( username='******', email='*****@*****.**', password='******' ) self.assertIsNotNone(user_usecases.register(user_register))
def test_update_user_status_to_active(self): user_id = UserId(id=str(uuid.uuid4())) user_mock = User( id=user_id, username='******', email='*****@*****.**', password='******', status=UserStatus.BLOCKED, ) self.uow.repository.find_by_id = MagicMock(return_value=user_mock) self.uow.repository.save = MagicMock(return_value=user_mock) user_usecases = UserUseCases(self.uow, self.pwd_encoder) command = UpdateUserStatusCommand( user_id=user_id.id, status=UserStatus.ACTIVE.value ) updated_user = user_usecases.update_status(command) assert not updated_user.is_blocked()
def update_status( self, update_user_status_command: UpdateUserStatusCommand ) -> User: user = self.user_uow.repository.find_by_id( UserId(update_user_status_command.user_id) ) if user is None: raise UsersNotFoundError(update_user_status_command.user_id) try: user.update_status(UserStatus(update_user_status_command.status)) self.user_uow.repository.update(user) self.user_uow.commit() except Exception as e: logging.error(e) self.user_uow.rollback() raise e return user
def test_register_duplicate_username(self): user_id = UserId(id=str(uuid.uuid4())) user_mock = User( id=user_id, username='******', email='*****@*****.**', password='******' ) previous_user_mock = User( id=user_id, username='******', email='*****@*****.**', password='******' ) self.uow.repository.find_by_email_or_username = MagicMock( return_value=previous_user_mock ) self.uow.repository.save = MagicMock(return_value=user_mock) self.pwd_encoder.encode = MagicMock(return_value='aaaa') user_usecases = UserUseCases(self.uow, self.pwd_encoder) user_register = UserCreateCommand( username='******', email='*****@*****.**', password='******' ) self.assertRaises( UserAlreadyExistException, user_usecases.register, user_register )
def register(self, user_command: UserCreateCommand) -> User: try: user = self.user_uow.repository.find_by_email_or_username( user_command.email, user_command.username ) if user: raise UserAlreadyExistException() user_id = UserId(str(uuid.uuid4())) user = User( id=user_id, username=user_command.username, email=user_command.email, full_name=user_command.full_name, password=self.pwd_encoder.encode(user_command.password), ) self.user_uow.repository.save(user) self.user_uow.commit() return self.user_uow.repository.find_by_id(user_id) except Exception as e: logging.error(e) self.user_uow.rollback() raise e
def test_user_update_status(self): user_id = UserId(id=str(uuid.uuid4())) new_user = User(username='******', email='email', password='******', id=user_id) new_user.update_status(UserStatus.BLOCKED) assert new_user.is_blocked()
def test_user_defaults(self): user_id = UserId(id=str(uuid.uuid4())) user_id2 = UserId(id=str(uuid.uuid4())) new_user = User(username='******', email='email', password='******', id=user_id) assert new_user.id != user_id2 assert not new_user.is_blocked()
def test_user_existing_user_id(self): user_id = UserId(id=str(uuid.uuid4())) assert ( User(username='******', email='email', id=user_id, password='******').id == user_id )
def find_by_id(self, user_id: str) -> User: user = self.user_uow.repository.find_by_id(UserId(user_id)) if not user: raise UsersNotFoundError(user_id) return user