def test_delete_drink_wish_fail( drink_repository_mock, drink_id, drink_name, drink_image_url, drink_type, ): drink_repository_mock.find_by_drink_id.side_effect = ResourceNotFoundError( ) drink_repository_mock.update.return_value = None drink_application_service = DrinkApplicationService( drink_repository=drink_repository_mock) input_dto = DeleteDrinkWishInputDto(drink_id=str(drink_id)) actual = drink_application_service.delete_drink_wish(input_dto) expected = FailedOutputDto(type="Resource Not Found Error", message="") assert actual == expected drink_repository_mock.find_by_drink_id.return_value = None drink_repository_mock.update.side_effect = ResourceNotFoundError() drink_application_service = DrinkApplicationService( drink_repository=drink_repository_mock) input_dto = DeleteDrinkWishInputDto(drink_id=str(drink_id)) actual = drink_application_service.delete_drink_wish(input_dto) expected = FailedOutputDto(type="Resource Not Found Error", message="") assert actual == expected
def delete_review( self, input_dto: DeleteReviewInputDto, drink_application_service: DrinkApplicationService, ) -> Union[DeleteReviewOutputDto, FailedOutputDto]: try: review_id = ReviewId.from_str(input_dto.review_id) review = self._review_repository.find_by_review_id(review_id) if review is None: return FailedOutputDto.build_resource_not_found_error( f"{str(input_dto.review_id)}의 리뷰를 찾을 수 없습니다.") self._review_repository.delete_by_review_id(review_id) drinks_input_dto = DeleteDrinkReviewInputDto( drink_id=str(review.drink_id), drink_rating=int(review.rating)) drink_delete_review_output_dto = drink_application_service.delete_drink_review( input_dto=drinks_input_dto) if not drink_delete_review_output_dto.status: return drink_delete_review_output_dto return DeleteReviewOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def test_add_drink_review_fail( drink_repository_mock, drink_id, drink_name, drink_image_url, drink_type, ): drink_repository_mock.find_by_drink_id.side_effect = ResourceNotFoundError( ) drink_repository_mock.update.return_value = None drink_application_service = DrinkApplicationService( drink_repository=drink_repository_mock) input_dto = AddDrinkReviewInputDto(drink_id=str(drink_id), drink_rating=5) actual = drink_application_service.add_drink_review(input_dto) expected = FailedOutputDto(type="Resource Not Found Error", message="") assert actual == expected drink_repository_mock.find_by_drink_id.return_value = Drink( id=drink_id, name=drink_name, image_url=drink_image_url, type=drink_type) drink_repository_mock.update.return_value = ResourceNotFoundError() drink_application_service = DrinkApplicationService( drink_repository=drink_repository_mock) input_dto = AddDrinkReviewInputDto(drink_id=str(drink_id), drink_rating=5) actual = drink_application_service.add_drink_review(input_dto) expected = FailedOutputDto(type="Resource Not Found Error", message="") assert actual == expected
def create_wish( self, input_dto: CreateWishInputDto, drink_application_service: DrinkApplicationService, ) -> Union[CreateWishOutputDto, FailedOutputDto]: try: wish = Wish( id=WishId.build(user_id=str(input_dto.user_id), drink_id=str(input_dto.drink_id)), user_id=UserId(value=input_dto.user_id), drink_id=DrinkId.from_str(input_dto.drink_id), created_at=time.time(), ) self._wish_repository.add(wish) add_drink_wish_input_dto = AddDrinkWishInputDto( drink_id=input_dto.drink_id) add_drink_wish_output_dto = drink_application_service.add_drink_wish( input_dto=add_drink_wish_input_dto) if not add_drink_wish_output_dto.status: return add_drink_wish_output_dto return CreateWishOutputDto(id=str(wish.id), user_id=str(wish.user_id), drink_id=str(wish.drink_id), created_at=wish.created_at) except ResourceAlreadyExistError as e: return FailedOutputDto.build_resource_conflict_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def delete_user( self, input_dto: DeleteUserInputDto ) -> Union[DeleteUserOutputDto, FailedOutputDto]: try: user_id = UserId(value=input_dto.user_id) self._user_repository.delete_by_user_id(user_id) return DeleteUserOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def add_drink_wish(self, input_dto: AddDrinkWishInputDto) -> Union[AddDrinkWishOutputDto, FailedOutputDto]: try: drink = self._drink_repository.find_by_drink_id(DrinkId.from_str(input_dto.drink_id)) drink.add_wish() self._drink_repository.update(drink) return AddDrinkWishOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error(message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def delete_drink(self, input_dto: DeleteDrinkInputDto) -> Union[DeleteDrinkOutputDto, FailedOutputDto]: try: drink_id = DrinkId.from_str(input_dto.drink_id) if self._drink_repository.find_by_drink_id(drink_id) is None: return FailedOutputDto.build_resource_not_found_error(f"{str(drink_id)}의 술을 찾을 수 없습니다.") self._drink_repository.delete_by_drink_id(drink_id) return DeleteDrinkOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error(message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def login( self, input_dto: LoginInputDto ) -> Union[LoginOutputDto, FailedOutputDto]: try: user_id = UserId(value=input_dto.user_id) user = self._user_repository.find_by_user_id(user_id=user_id) if not self._verify_password(input_dto.password, user.password): return FailedOutputDto.build_unauthorized_error( f"잘못된 비밀번호 입니다.") return LoginOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def test_get_token_fail(jwt_secret_key, jwt_algorithm): user_application_service_mock = mock.Mock(spec=UserApplicationService) user_application_service_mock.login.return_value = FailedOutputDto.build_resource_not_found_error( "heumsi의 유저를 찾지 못했습니다." ) auth_application_service = AuthApplicationService( user_application_service=user_application_service_mock, jwt_secret_key=jwt_secret_key, jwt_algorithm="jwt_algorithm", ) input_dto = GetTokenInputDto(user_id="heumsi", password="******") actual = auth_application_service.get_token(input_dto=input_dto) expected = FailedOutputDto.build_resource_error("heumsi의 유저를 찾지 못했습니다.") assert actual == expected
def delete_drink_review( self, input_dto: DeleteDrinkReviewInputDto ) -> Union[DeleteDrinkReviewOutputDto, FailedOutputDto]: try: drink = self._drink_repository.find_by_drink_id(DrinkId.from_str(input_dto.drink_id)) drink.delete_rating(input_dto.drink_rating) self._drink_repository.update(drink) return DeleteDrinkReviewOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error(message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def test_delete_review_fail( review_repository_mock, drink_application_service_mock, review_id, drink_id, user_id, rating, created_at, ): review_repository_mock.find_by_review_id.return_value = Review( id=review_id, drink_id=drink_id, user_id=UserId(value=user_id), rating=ReviewRating(value=rating), comment="hello", created_at=created_at, updated_at=created_at, ) review_repository_mock.delete_by_review_id.side_effect = ResourceNotFoundError( ) review_application_service = ReviewApplicationService( review_repository=review_repository_mock) drink_application_service_mock.delete_drink_review.return_value = DeleteDrinkReviewOutputDto( ) input_dto = DeleteReviewInputDto(review_id=str(review_id)) actual = review_application_service.delete_review( input_dto, drink_application_service_mock) expected = FailedOutputDto(type="Resource Not Found Error", message="") assert actual == expected
def test_create_review_fail( review_repository_mock, drink_application_service_mock, review_id, drink_id, user_id, rating, created_at, ): review_repository_mock.add.side_effect = ResourceAlreadyExistError() review_application_service = ReviewApplicationService( review_repository=review_repository_mock) drink_application_service_mock.add_drink_review.return_value = AddDrinkReviewOutputDto( ) input_dto = CreateReviewInputDto( drink_id=str(drink_id), user_id=str(UserId(value=user_id)), rating=int(ReviewRating(value=rating)), comment="", ) actual = review_application_service.create_review( input_dto, drink_application_service_mock) expected = FailedOutputDto(type="Resource Conflict Error", message="") assert actual == expected
def create_drink(self, input_dto: CreateDrinkInputDto) -> Union[CreateDrinkOutputDto, FailedOutputDto]: try: drink = Drink( id=DrinkId.build(drink_name=input_dto.drink_name, created_at=time.time()), name=input_dto.drink_name, image_url=input_dto.drink_image_url, type=DrinkType.from_str(input_dto.drink_type), ) self._drink_repository.add(drink) return CreateDrinkOutputDto() except ResourceAlreadyExistError as e: return FailedOutputDto.build_resource_conflict_error(message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def _create_access_token(self, data: dict) -> str: try: encoded_jwt = jwt.encode(data, self._JWT_SECRET_KEY, algorithm=self._JWT_ALGORITHM) return encoded_jwt except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def test_failed_json_response(): output_dto = FailedOutputDto.build_resource_error("리소스 에러 입니다.") actual = FailedJsonResponse.build_by_output_dto(output_dto) expected = JSONResponse(status_code=422, content={"message": "리소스 에러 입니다."}) assert actual.status_code == expected.status_code assert actual.body == expected.body
def find_user( self, input_dto: FindUserInputDto ) -> Union[FindUserOutputDto, FailedOutputDto]: try: user = self._user_repository.find_by_user_id(user_id=UserId( value=input_dto.user_id)) return FindUserOutputDto( user_id=str(user.id), user_name=str(user.name), description=user.description, image_url=user.image_url, ) except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def get_token( self, input_dto: GetTokenInputDto ) -> Union[GetTokenOutputDto, FailedOutputDto]: try: login_input_dto = LoginInputDto(user_id=input_dto.user_id, password=input_dto.password) login_output_dto = self._user_application_service.login( input_dto=login_input_dto) if login_output_dto.status is True: access_token = self._create_access_token(data=TokenPayload( user_id=input_dto.user_id).dict()) return GetTokenOutputDto(access_token=access_token) return FailedOutputDto.build_resource_error( message=login_output_dto.message) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def update_user( self, input_dto: UpdateUserInputDto ) -> Union[UpdateUserOutputDto, FailedOutputDto]: try: user = User( id=UserId(value=input_dto.user_id), name=UserName(value=input_dto.user_name), description=input_dto.description, password=input_dto.password, ) self._user_repository.update(user) return UpdateUserOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def test_find_user_fail(user_repository_mock, user_id, user_name, password): user_repository_mock.find_by_user_id.side_effect = ResourceNotFoundError() user_application_service = UserApplicationService( user_repository=user_repository_mock) input_dto = FindUserInputDto(user_id="not exist user") actual = user_application_service.find_user(input_dto) expected = FailedOutputDto(type="Resource Not Found Error", message="") assert actual == expected
def find_review( self, input_dto: FindReviewInputDto ) -> Union[FindReviewOutputDto, FailedOutputDto]: try: review_id = ReviewId.from_str(input_dto.review_id) review = self._review_repository.find_by_review_id(review_id) return FindReviewOutputDto( review_id=str(review.id), drink_id=str(review.drink_id), user_id=str(review.user_id), rating=int(review.rating), comment=review.comment, created_at=review.created_at, updated_at=review.updated_at, ) except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def test_get_token_data_fail(jwt_secret_key, jwt_algorithm): auth_application_service = AuthApplicationService( user_application_service=mock.Mock(spec=UserApplicationService), jwt_secret_key=jwt_secret_key, jwt_algorithm=jwt_algorithm, ) input_dto = GetTokenDataInputDto(access_token="wrong jwt token") actual = auth_application_service.get_token_data(input_dto) expected = FailedOutputDto.build_unauthorized_error(message="올바른 access-token이 아닙니다.") assert actual == expected
def test_create_user_fail(user_repository_mock, user_id, user_name, password): user_repository_mock.add.side_effect = ResourceAlreadyExistError() user_application_service = UserApplicationService( user_repository=user_repository_mock) input_dto = CreateUserInputDto(user_id=user_id, user_name=user_name, password=password) actual = user_application_service.create_user(input_dto) expected = FailedOutputDto(type="Resource Conflict Error", message="") assert actual == expected
def test_update_user_fail(user_repository_mock, user_id, user_name, password): user_repository_mock.update.side_effect = ResourceNotFoundError() user_application_service = UserApplicationService( user_repository=user_repository_mock) input_dto = UpdateUserInputDto(user_id=user_id, user_name="heumsi", description="", password="******") actual = user_application_service.update_user(input_dto) expected = FailedOutputDto(type="Resource Not Found Error", message="") assert actual == expected
def test_login_fail_with_wrong_id(user_repository_mock): wrong_user_id = "joon" user_repository_mock.find_by_user_id.side_effect = ResourceNotFoundError( f"{wrong_user_id}의 유저를 찾지 못했습니다.") user_application_service = UserApplicationService( user_repository=user_repository_mock) input_dto = LoginInputDto(user_id=wrong_user_id, password="******") actual = user_application_service.login(input_dto) expected = FailedOutputDto.build_resource_not_found_error( message=f"{str(input_dto.user_id)}의 유저를 찾지 못했습니다.") assert actual == expected
def test_get_users_fail(user_application_service_mock, client, app): user_application_service_mock.find_user.return_value = FailedOutputDto.build_resource_not_found_error( ) with app.container.user_application_service.override( user_application_service_mock): response = client.get("/users/heumsi") assert response.status_code == 404 assert response.json() == { "error_type": "Resource Not Found Error", "message": "", }
def find_reviews( self, input_dto: FindReviewsInputDto ) -> Union[FailedOutputDto, FindReviewsOutputDto]: try: reviews = self._review_repository.find_all( query_param=input_dto.query_param) return FindReviewsOutputDto(items=[ FindReviewsOutputDto.Item( review_id=str(review.id), drink_id=str(review.drink_id), user_id=str(review.user_id), rating=int(review.rating), comment=review.comment, created_at=review.created_at, updated_at=review.updated_at, ) for review in reviews ]) except InvalidParamInputError as e: return FailedOutputDto.build_parameters_error(message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def create_user( self, input_dto: CreateUserInputDto ) -> Union[CreateUserOutputDto, FailedOutputDto]: try: user = User( id=UserId(value=input_dto.user_id), name=UserName(value=input_dto.user_name), password=self._get_password_hash(input_dto.password), ) self._user_repository.add(user) return CreateUserOutputDto( user_id=str(user.id), user_name=str(user.name), description=user.description, image_url=user.image_url, ) except ResourceAlreadyExistError as e: return FailedOutputDto.build_resource_conflict_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def update_drink_review( self, input_dto: UpdateDrinkReviewInputDto ) -> Union[UpdateDrinkReviewOutputDto, FailedOutputDto]: try: drink = self._drink_repository.find_by_drink_id(DrinkId.from_str(input_dto.drink_id)) if drink is None: return FailedOutputDto.build_resource_not_found_error( message=f"{str(input_dto.drink_id)}의 술을 찾을 수 없습니다." ) drink.update_rating( old_rating=input_dto.old_drink_rating, new_rating=input_dto.new_drink_rating, ) self._drink_repository.update(drink) return UpdateDrinkReviewOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error(message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def delete_wish( self, input_dto: DeleteWishInputDto, drink_application_service: DrinkApplicationService, ) -> Union[DeleteWishOutputDto, FailedOutputDto]: try: wish = self._wish_repository.delete_by_wish_id( WishId.from_str(input_dto.wish_id)) delete_drink_wish_input_dto = DeleteDrinkWishInputDto( drink_id=str(wish.drink_id)) delete_drink_wish_output_dto = drink_application_service.delete_drink_wish( input_dto=delete_drink_wish_input_dto) if not delete_drink_wish_output_dto.status: return delete_drink_wish_output_dto return DeleteWishOutputDto() except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error( message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))
def find_drink(self, input_dto: FindDrinkInputDto) -> Union[FindDrinkOutputDto, FailedOutputDto]: try: drink = self._drink_repository.find_by_drink_id(drink_id=DrinkId.from_str(input_dto.drink_id)) if drink is None: return FailedOutputDto.build_resource_not_found_error( message=f"{str(input_dto.drink_id)}의 술을 찾을 수 없습니다." ) return FindDrinkOutputDto( drink_id=str(drink.id), drink_name=drink.name, drink_image_url=drink.image_url, drink_type=drink.type.value, avg_rating=float(drink.avg_rating), num_of_reviews=drink.num_of_reviews, num_of_wish=drink.num_of_wish, ) except ResourceNotFoundError as e: return FailedOutputDto.build_resource_not_found_error(message=str(e)) except Exception as e: return FailedOutputDto.build_system_error(message=str(e))