-
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
5423d2c
commit 312d2b4
Showing
10 changed files
with
318 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from dataclasses import dataclass | ||
from datetime import datetime | ||
|
||
from amdb.domain.entities.movie import MovieId | ||
|
||
|
||
@dataclass(frozen=True, slots=True) | ||
class GetRatingQuery: | ||
movie_id: MovieId | ||
|
||
|
||
@dataclass(frozen=True, slots=True) | ||
class GetRatingResult: | ||
value: float | ||
created_at: datetime |
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,59 @@ | ||
from amdb.domain.services.access_concern import AccessConcern | ||
from amdb.application.common.interfaces.permissions_gateway import PermissionsGateway | ||
from amdb.application.common.interfaces.movie_gateway import MovieGateway | ||
from amdb.application.common.interfaces.rating_gateway import RatingGateway | ||
from amdb.application.common.interfaces.identity_provider import IdentityProvider | ||
from amdb.application.queries.get_rating import GetRatingQuery, GetRatingResult | ||
from amdb.application.common.constants.exceptions import ( | ||
GET_RATING_ACCESS_DENIED, | ||
MOVIE_DOES_NOT_EXIST, | ||
MOVIE_NOT_RATED, | ||
) | ||
from amdb.application.common.exception import ApplicationError | ||
|
||
|
||
class GetRatingHandler: | ||
def __init__( | ||
self, | ||
*, | ||
access_concern: AccessConcern, | ||
permissions_gateway: PermissionsGateway, | ||
movie_gateway: MovieGateway, | ||
rating_gateway: RatingGateway, | ||
identity_provider: IdentityProvider, | ||
) -> None: | ||
self._access_concern = access_concern | ||
self._permissions_gateway = permissions_gateway | ||
self._movie_gateway = movie_gateway | ||
self._rating_gateway = rating_gateway | ||
self._identity_provider = identity_provider | ||
|
||
def execute(self, query: GetRatingQuery) -> GetRatingResult: | ||
current_permissions = self._identity_provider.get_permissions() | ||
required_permissions = self._permissions_gateway.for_get_rating() | ||
access = self._access_concern.authorize( | ||
current_permissions=current_permissions, | ||
required_permissions=required_permissions, | ||
) | ||
if not access: | ||
raise ApplicationError(GET_RATING_ACCESS_DENIED) | ||
|
||
movie = self._movie_gateway.with_id(query.movie_id) | ||
if not movie: | ||
raise ApplicationError(MOVIE_DOES_NOT_EXIST) | ||
|
||
current_user_id = self._identity_provider.get_user_id() | ||
|
||
rating = self._rating_gateway.with_user_id_and_movie_id( | ||
user_id=current_user_id, | ||
movie_id=movie.id, | ||
) | ||
if not rating: | ||
raise ApplicationError(MOVIE_NOT_RATED) | ||
|
||
get_rating_result = GetRatingResult( | ||
value=rating.value, | ||
created_at=rating.created_at, | ||
) | ||
|
||
return get_rating_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
23 changes: 23 additions & 0 deletions
23
src/amdb/presentation/web_api/routers/ratings/get_rating.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,23 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import Depends | ||
|
||
from amdb.domain.entities.movie import MovieId | ||
from amdb.application.common.interfaces.identity_provider import IdentityProvider | ||
from amdb.application.queries.get_rating import GetRatingQuery, GetRatingResult | ||
from amdb.presentation.handler_factory import HandlerFactory | ||
from amdb.presentation.web_api.dependencies.identity_provider import get_identity_provider | ||
|
||
|
||
async def get_rating( | ||
ioc: Annotated[HandlerFactory, Depends()], | ||
identity_provider: Annotated[IdentityProvider, Depends(get_identity_provider)], | ||
movie_id: MovieId, | ||
) -> GetRatingResult: | ||
with ioc.get_rating(identity_provider) as get_rating_handler: | ||
get_rating_query = GetRatingQuery( | ||
movie_id=movie_id, | ||
) | ||
get_rating_result = get_rating_handler.execute(get_rating_query) | ||
|
||
return get_rating_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
183 changes: 183 additions & 0 deletions
183
tests/unit/application/query_handlers/test_get_rating.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,183 @@ | ||
from unittest.mock import Mock | ||
from datetime import 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.rating import Rating | ||
from amdb.domain.services.access_concern import AccessConcern | ||
from amdb.application.common.interfaces.user_gateway import UserGateway | ||
from amdb.application.common.interfaces.movie_gateway import MovieGateway | ||
from amdb.application.common.interfaces.rating_gateway import RatingGateway | ||
from amdb.application.common.interfaces.permissions_gateway import PermissionsGateway | ||
from amdb.application.common.interfaces.unit_of_work import UnitOfWork | ||
from amdb.application.common.interfaces.identity_provider import IdentityProvider | ||
from amdb.application.queries.get_rating import GetRatingQuery, GetRatingResult | ||
from amdb.application.query_handlers.get_rating import GetRatingHandler | ||
from amdb.application.common.constants.exceptions import ( | ||
GET_RATING_ACCESS_DENIED, | ||
MOVIE_DOES_NOT_EXIST, | ||
MOVIE_NOT_RATED, | ||
) | ||
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_rating() | ||
identity_provider.get_permissions = Mock(return_value=correct_permissions) | ||
|
||
return identity_provider | ||
|
||
|
||
def test_get_rating( | ||
user_gateway: UserGateway, | ||
movie_gateway: MovieGateway, | ||
rating_gateway: RatingGateway, | ||
permissions_gateway: PermissionsGateway, | ||
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="Matrix", | ||
rating=10, | ||
rating_count=1, | ||
) | ||
movie_gateway.save(movie) | ||
|
||
rating = Rating( | ||
movie_id=movie.id, | ||
user_id=user.id, | ||
value=10, | ||
created_at=datetime.now(timezone.utc), | ||
) | ||
rating_gateway.save(rating) | ||
|
||
unit_of_work.commit() | ||
|
||
identity_provider_with_correct_permissions.get_user_id = Mock( | ||
return_value=user.id, | ||
) | ||
|
||
get_rating_query = GetRatingQuery( | ||
movie_id=movie.id, | ||
) | ||
get_rating_handler = GetRatingHandler( | ||
access_concern=AccessConcern(), | ||
permissions_gateway=permissions_gateway, | ||
movie_gateway=movie_gateway, | ||
rating_gateway=rating_gateway, | ||
identity_provider=identity_provider_with_correct_permissions, | ||
) | ||
|
||
get_rating_result = get_rating_handler.execute(get_rating_query) | ||
expected_get_rating_result = GetRatingResult( | ||
value=rating.value, | ||
created_at=rating.created_at, | ||
) | ||
|
||
assert get_rating_result == expected_get_rating_result | ||
|
||
|
||
def test_get_rating_should_raise_error_when_access_is_denied( | ||
movie_gateway: MovieGateway, | ||
rating_gateway: RatingGateway, | ||
permissions_gateway: PermissionsGateway, | ||
identity_provider_with_incorrect_permissions: IdentityProvider, | ||
): | ||
get_rating_query = GetRatingQuery( | ||
movie_id=MovieId(uuid7()), | ||
) | ||
get_rating_handler = GetRatingHandler( | ||
access_concern=AccessConcern(), | ||
permissions_gateway=permissions_gateway, | ||
movie_gateway=movie_gateway, | ||
rating_gateway=rating_gateway, | ||
identity_provider=identity_provider_with_incorrect_permissions, | ||
) | ||
|
||
with pytest.raises(ApplicationError) as error: | ||
get_rating_handler.execute(get_rating_query) | ||
|
||
assert error.value.message == GET_RATING_ACCESS_DENIED | ||
|
||
|
||
def test_get_rating_should_raise_error_when_movie_does_not_exist( | ||
movie_gateway: MovieGateway, | ||
rating_gateway: RatingGateway, | ||
permissions_gateway: PermissionsGateway, | ||
identity_provider_with_correct_permissions: IdentityProvider, | ||
): | ||
get_rating_query = GetRatingQuery( | ||
movie_id=MovieId(uuid7()), | ||
) | ||
get_rating_handler = GetRatingHandler( | ||
access_concern=AccessConcern(), | ||
permissions_gateway=permissions_gateway, | ||
movie_gateway=movie_gateway, | ||
rating_gateway=rating_gateway, | ||
identity_provider=identity_provider_with_correct_permissions, | ||
) | ||
|
||
with pytest.raises(ApplicationError) as error: | ||
get_rating_handler.execute(get_rating_query) | ||
|
||
assert error.value.message == MOVIE_DOES_NOT_EXIST | ||
|
||
|
||
def test_get_rating_should_raise_error_when_movie_is_not_rated( | ||
user_gateway: UserGateway, | ||
movie_gateway: MovieGateway, | ||
rating_gateway: RatingGateway, | ||
permissions_gateway: PermissionsGateway, | ||
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="Matrix", | ||
rating=0, | ||
rating_count=0, | ||
) | ||
movie_gateway.save(movie) | ||
|
||
unit_of_work.commit() | ||
|
||
identity_provider_with_correct_permissions.get_user_id = Mock( | ||
return_value=user.id, | ||
) | ||
|
||
get_rating_query = GetRatingQuery( | ||
movie_id=movie.id, | ||
) | ||
get_rating_handler = GetRatingHandler( | ||
access_concern=AccessConcern(), | ||
permissions_gateway=permissions_gateway, | ||
movie_gateway=movie_gateway, | ||
rating_gateway=rating_gateway, | ||
identity_provider=identity_provider_with_correct_permissions, | ||
) | ||
|
||
with pytest.raises(ApplicationError) as error: | ||
get_rating_handler.execute(get_rating_query) | ||
|
||
assert error.value.message == MOVIE_NOT_RATED |