-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
555dd7a
commit 7d05899
Showing
12 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from dataclasses import dataclass | ||
from datetime import datetime | ||
|
||
from amdb.domain.entities.user import UserId | ||
from amdb.domain.entities.movie import MovieId | ||
from amdb.domain.entities.review import ReviewId, ReviewType | ||
|
||
|
||
@dataclass(frozen=True, slots=True) | ||
class GetMyReviewsQuery: | ||
limit: int | ||
offset: int | ||
|
||
|
||
@dataclass(frozen=True, slots=True) | ||
class Review: | ||
id: ReviewId | ||
user_id: UserId | ||
movie_id: MovieId | ||
title: str | ||
content: str | ||
type: ReviewType | ||
created_at: datetime | ||
|
||
|
||
@dataclass(frozen=True, slots=True) | ||
class GetMyReviewsResult: | ||
reviews: list[Review] | ||
review_count: int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from amdb.domain.services.access_concern import AccessConcern | ||
from amdb.application.common.interfaces.permissions_gateway import PermissionsGateway | ||
from amdb.application.common.interfaces.review_gateway import ReviewGateway | ||
from amdb.application.common.interfaces.identity_provider import IdentityProvider | ||
from amdb.application.common.constants.exceptions import GET_MY_REVIEWS_ACCESS_DENIED | ||
from amdb.application.common.exception import ApplicationError | ||
from amdb.application.queries.get_my_reviews import GetMyReviewsQuery, GetMyReviewsResult | ||
|
||
|
||
class GetMyReviewsHandler: | ||
def __init__( | ||
self, | ||
*, | ||
access_concern: AccessConcern, | ||
permissions_gateway: PermissionsGateway, | ||
review_gateway: ReviewGateway, | ||
identity_provider: IdentityProvider, | ||
) -> None: | ||
self._access_concern = access_concern | ||
self._permissions_gateway = permissions_gateway | ||
self._review_gateway = review_gateway | ||
self._identity_provider = identity_provider | ||
|
||
def execute(self, query: GetMyReviewsQuery) -> GetMyReviewsResult: | ||
current_permissions = self._identity_provider.get_permissions() | ||
required_permissions = self._permissions_gateway.for_get_my_reviews() | ||
access = self._access_concern.authorize( | ||
current_permissions=current_permissions, | ||
required_permissions=required_permissions, | ||
) | ||
if not access: | ||
raise ApplicationError(GET_MY_REVIEWS_ACCESS_DENIED) | ||
|
||
current_user_id = self._identity_provider.get_user_id() | ||
|
||
reviews = self._review_gateway.list_with_user_id( | ||
user_id=current_user_id, | ||
limit=query.limit, | ||
offset=query.offset, | ||
) | ||
get_my_reviews_result = GetMyReviewsResult( | ||
reviews=reviews, # type: ignore | ||
review_count=len(reviews), | ||
) | ||
|
||
return get_my_reviews_result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/amdb/presentation/web_api/routers/reviews/get_my_reviews.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import Depends | ||
|
||
from amdb.application.common.interfaces.identity_provider import IdentityProvider | ||
from amdb.application.queries.get_my_reviews import GetMyReviewsQuery, GetMyReviewsResult | ||
from amdb.presentation.handler_factory import HandlerFactory | ||
from amdb.presentation.web_api.dependencies.identity_provider import get_identity_provider | ||
|
||
|
||
async def get_my_reviews( | ||
ioc: Annotated[HandlerFactory, Depends()], | ||
identity_provider: Annotated[IdentityProvider, Depends(get_identity_provider)], | ||
limit: int = 100, | ||
offset: int = 0, | ||
) -> GetMyReviewsResult: | ||
with ioc.get_my_reviews(identity_provider) as get_my_reviews_handler: | ||
get_my_reviews_query = GetMyReviewsQuery( | ||
limit=limit, | ||
offset=offset, | ||
) | ||
get_my_reviews_result = get_my_reviews_handler.execute(get_my_reviews_query) | ||
|
||
return get_my_reviews_result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
tests/unit/application/query_handlers/test_get_my_reviews.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
from unittest.mock import Mock | ||
from datetime import date, datetime, timezone | ||
|
||
import pytest | ||
from uuid_extensions import uuid7 | ||
|
||
from amdb.domain.entities.user import UserId, User | ||
from amdb.domain.entities.movie import MovieId, Movie | ||
from amdb.domain.entities.review import ReviewId, ReviewType, Review | ||
from amdb.domain.services.access_concern import AccessConcern | ||
from amdb.application.common.interfaces.permissions_gateway import PermissionsGateway | ||
from amdb.application.common.interfaces.user_gateway import UserGateway | ||
from amdb.application.common.interfaces.movie_gateway import MovieGateway | ||
from amdb.application.common.interfaces.review_gateway import ReviewGateway | ||
from amdb.application.common.interfaces.unit_of_work import UnitOfWork | ||
from amdb.application.common.interfaces.identity_provider import IdentityProvider | ||
from amdb.application.queries.get_my_reviews import GetMyReviewsQuery, GetMyReviewsResult | ||
from amdb.application.query_handlers.get_my_reviews import GetMyReviewsHandler | ||
from amdb.application.common.constants.exceptions import GET_MY_REVIEWS_ACCESS_DENIED | ||
from amdb.application.common.exception import ApplicationError | ||
|
||
|
||
@pytest.fixture | ||
def identity_provider_with_correct_permissions( | ||
permissions_gateway: PermissionsGateway, | ||
) -> IdentityProvider: | ||
identity_provider = Mock() | ||
|
||
correct_permissions = permissions_gateway.for_get_my_reviews() | ||
identity_provider.get_permissions = Mock(return_value=correct_permissions) | ||
|
||
return identity_provider | ||
|
||
|
||
def test_get_my_reviews( | ||
permissions_gateway: PermissionsGateway, | ||
user_gateway: UserGateway, | ||
movie_gateway: MovieGateway, | ||
review_gateway: ReviewGateway, | ||
unit_of_work: UnitOfWork, | ||
identity_provider_with_correct_permissions: IdentityProvider, | ||
): | ||
user = User( | ||
id=UserId(uuid7()), | ||
name="John Doe", | ||
) | ||
user_gateway.save(user) | ||
|
||
movie = Movie( | ||
id=MovieId(uuid7()), | ||
title="Gone girl", | ||
release_date=date(2014, 10, 3), | ||
rating=0, | ||
rating_count=0, | ||
) | ||
movie_gateway.save(movie) | ||
|
||
review = Review( | ||
id=ReviewId(uuid7()), | ||
user_id=user.id, | ||
movie_id=movie.id, | ||
title="Masterpice", | ||
content="Extremely underrated", | ||
type=ReviewType.POSITIVE, | ||
created_at=datetime.now(timezone.utc), | ||
) | ||
review_gateway.save(review) | ||
|
||
unit_of_work.commit() | ||
|
||
identity_provider_with_correct_permissions.get_user_id = Mock( | ||
return_value=user.id, | ||
) | ||
|
||
get_my_reviews_query = GetMyReviewsQuery( | ||
limit=10, | ||
offset=0, | ||
) | ||
get_my_reviews_handler = GetMyReviewsHandler( | ||
access_concern=AccessConcern(), | ||
permissions_gateway=permissions_gateway, | ||
review_gateway=review_gateway, | ||
identity_provider=identity_provider_with_correct_permissions, | ||
) | ||
|
||
get_my_reviews_result = get_my_reviews_handler.execute(get_my_reviews_query) | ||
expected_get_my_reviews_result = GetMyReviewsResult( | ||
reviews=[review], | ||
review_count=1, | ||
) | ||
|
||
assert get_my_reviews_result == expected_get_my_reviews_result | ||
|
||
|
||
def test_get_my_reviews_should_raise_error_when_access_is_denied( | ||
permissions_gateway: PermissionsGateway, | ||
review_gateway: ReviewGateway, | ||
identity_provider_with_incorrect_permissions: IdentityProvider, | ||
): | ||
get_my_reviews_query = GetMyReviewsQuery( | ||
limit=10, | ||
offset=0, | ||
) | ||
get_my_reviews_handler = GetMyReviewsHandler( | ||
access_concern=AccessConcern(), | ||
permissions_gateway=permissions_gateway, | ||
review_gateway=review_gateway, | ||
identity_provider=identity_provider_with_incorrect_permissions, | ||
) | ||
|
||
identity_provider_with_incorrect_permissions.get_user_id = Mock( | ||
return_value=UserId(uuid7()), | ||
) | ||
|
||
with pytest.raises(ApplicationError) as error: | ||
get_my_reviews_handler.execute(get_my_reviews_query) | ||
|
||
assert error.value.message == GET_MY_REVIEWS_ACCESS_DENIED |