Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ORG-93] Application status endpoint #98

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions organizator_api/app/applications/domain/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ class UserIsNotStudent(Exception):

class UserIsTooYoung(Exception):
pass


class ApplicationNotFound(Exception):
pass
5 changes: 4 additions & 1 deletion organizator_api/app/applications/domain/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import List

from app.applications.domain.models.application import Application
from app.events.domain.models.event import Event
from app.users.domain.models.user import User


Expand All @@ -19,3 +18,7 @@ def get_by_user(self, user: User) -> List[Application]:
@abstractmethod
def get_by_event(self, event_id: uuid.UUID) -> List[Application]:
pass

@abstractmethod
def get_application(self, event_id: uuid.UUID, user_id: uuid.UUID) -> Application:
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import uuid

from app.applications.domain.models.application import ApplicationStatus
from app.applications.infrastructure.repository_factories import (
ApplicationRepositoryFactory,
)
from app.events.domain.usecases.get_event_use_case import GetEventUseCase
from app.users.domain.usecases.get_user_by_token_use_case import GetUserByTokenUseCase


class GetApplicationStatusByEventUseCase:
def __init__(self) -> None:
self.application_repository = ApplicationRepositoryFactory.create()

def execute(self, token: uuid.UUID, event_id: uuid.UUID) -> ApplicationStatus:
GetEventUseCase().execute(event_id=event_id)
user = GetUserByTokenUseCase().execute(token=token)

application = self.application_repository.get_application(
event_id=event_id, user_id=user.id
)

return application.status
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from app.applications.infrastructure.repository_factories import (
ApplicationRepositoryFactory,
)
from app.events.domain.models.event import Event
from app.events.domain.usecases.get_event_use_case import GetEventUseCase
from app.users.domain.exceptions import OnlyAuthorizedToOrganizer
from app.users.domain.models.user import UserRoles
Expand Down
2 changes: 2 additions & 0 deletions organizator_api/app/applications/infrastructure/http/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
create_new_application,
get_applications_by_token,
get_applications_by_event,
get_application_status,
)

urlpatterns = [
path("new", create_new_application),
path("myevents", get_applications_by_token),
path("status/<uuid:event_id>", get_application_status),
path("participants/<uuid:event_id>", get_applications_by_event),
]
29 changes: 29 additions & 0 deletions organizator_api/app/applications/infrastructure/http/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
UserIsNotAParticipant,
UserIsNotStudent,
UserIsTooYoung,
ApplicationNotFound,
)
from app.applications.domain.usecases.create_new_application_use_case import (
CreateNewApplicationUseCase,
)
from app.applications.domain.usecases.get_application_status_by_event_use_case import (
GetApplicationStatusByEventUseCase,
)
from app.applications.domain.usecases.get_applications_by_event_use_case import (
GetApplicationsByEventUseCase,
)
Expand Down Expand Up @@ -122,3 +126,28 @@ def get_applications_by_event(
)

return HttpResponse(status=200, content=json.dumps(applications_response))


@require_http_methods(["GET"])
def get_application_status(request: HttpRequest, event_id: uuid.UUID) -> HttpResponse:
token = request.headers.get("Authorization")
if not token:
return HttpResponse(status=401, content="Unauthorized")

try:
token_to_uuid = uuid.UUID(token)
except ValueError:
return HttpResponse(status=400, content="Invalid token")

try:
status = GetApplicationStatusByEventUseCase().execute(
token=token_to_uuid, event_id=event_id
)
except ApplicationNotFound:
return HttpResponse(status=206, content="Not applied")
except EventNotFound:
return HttpResponse(status=404, content="Event not found")
except UserNotFound:
return HttpResponse(status=404, content="User not found")

return HttpResponse(status=200, content=json.dumps({"status": status.value}))
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

from django.db import IntegrityError

from app.applications.domain.exceptions import ApplicationAlreadyExists
from app.applications.domain.exceptions import (
ApplicationAlreadyExists,
ApplicationNotFound,
)
from app.applications.domain.models.application import Application, ApplicationStatus
from app.applications.domain.repositories import ApplicationRepository
from app.applications.infrastructure.persistence.models.orm_application import (
Expand Down Expand Up @@ -47,6 +50,17 @@ def get_by_event(self, event_id: uuid.UUID) -> List[Application]:
for application in ORMEventApplication.objects.filter(event=event_orm)
]

def get_application(self, event_id: uuid.UUID, user_id: uuid.UUID) -> Application:
event_orm = ORMEvent.objects.get(id=event_id)
user_orm = ORMUser.objects.get(id=user_id)

try:
return self._to_domain_model(
ORMEventApplication.objects.get(event=event_orm, user=user_orm)
)
except ORMEventApplication.DoesNotExist:
raise ApplicationNotFound

def _to_domain_model(self, orm_application: ORMEventApplication) -> Application:
return Application(
id=orm_application.id,
Expand Down
1 change: 1 addition & 0 deletions organizator_api/tests/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from app.users.domain.repositories import UserRepository
from app.users.infrastructure.persistence.orm_user_repository import ORMUserRepository
from app.users.infrastructure.repository_factories import UserRepositoryFactory
from tests.applications.domain.ApplicationFactory import ApplicationFactory
from tests.applications.mocks.application_repository_mock import (
ApplicationRepositoryMock,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import uuid

from app.applications.domain.exceptions import ApplicationNotFound
from app.applications.domain.usecases.get_application_status_by_event_use_case import (
GetApplicationStatusByEventUseCase,
)
from app.events.domain.exceptions import EventNotFound
from app.users.domain.exceptions import UserNotFound
from tests.api_tests import ApiTests
from tests.applications.domain.ApplicationFactory import ApplicationFactory
from tests.events.domain.EventFactory import EventFactory
from tests.users.domain.UserFactory import UserFactory


class TestGetApplicationStatusByEventUseCase(ApiTests):
def setUp(self) -> None:
super().setUp()
self.application_repository.clear()
self.user_repository.clear()
self.event_repository.clear()

self.user_token_participant = uuid.UUID("eb41b762-5988-4fa3-8942-7a91ccb00686")

self.user_participant = UserFactory().create(
new_id=uuid.UUID("eb41b762-5988-4fa3-8942-7a91ccb00686"),
token=self.user_token_participant,
username="john",
email="john@test.com",
)
self.user_repository.create(self.user_participant)

self.event_id = uuid.UUID("eb41b762-5988-4fa3-8942-7a91ccb00686")
self.event = EventFactory().create(new_id=self.event_id, name="HackUPC 2024")
self.event_repository.create(self.event)

def test__given_a_event_id_for_a_non_existing_event__when_get_application_status_by_event__then_event_not_found_is_raised(
self,
) -> None:
# When / Then
with self.assertRaises(EventNotFound):
GetApplicationStatusByEventUseCase().execute(
event_id=uuid.uuid4(), token=self.user_token_participant
)

def test__given_a_token_for_a_non_existing_user__when_get_application_status_by_event__then_user_not_found_is_raised(
self,
) -> None:
# When / Then
with self.assertRaises(UserNotFound):
GetApplicationStatusByEventUseCase().execute(
event_id=self.event_id, token=uuid.uuid4()
)

def test__given_a_correct_token_and_event_id_for_a_non_existing_application__when_get_application_status_by_event__then_application_not_found_is_raised(
self,
) -> None:
# When / Then
with self.assertRaises(ApplicationNotFound):
GetApplicationStatusByEventUseCase().execute(
event_id=self.event_id, token=self.user_token_participant
)

def test__given_a_correct_token_and_event_id_for_a_existing_applications__when_get_application_status_by_event__then_application_status_is_returned(
self,
) -> None:
# Given
application = ApplicationFactory().create(
new_id=uuid.UUID("eb41b762-5988-4fa3-8942-7a91ccb00686"),
user=self.user_participant,
event=self.event,
)
self.application_repository.create(application)

# When
application_status = GetApplicationStatusByEventUseCase().execute(
event_id=self.event_id, token=self.user_token_participant
)

# Then
self.assertEqual(application_status, application.status)
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import uuid
from datetime import datetime, timezone

from tests.api_tests import ApiTests
from tests.applications.domain.ApplicationFactory import ApplicationFactory
from tests.events.domain.EventFactory import EventFactory
from tests.users.domain.UserFactory import UserFactory


class TestViewGetApplicationStatus(ApiTests):
def setUp(self) -> None:
super().setUp()
self.application_repository.clear()
self.user_repository.clear()
self.event_repository.clear()

self.user_token_participant = "eb41b762-5988-4fa3-8942-7a91ccb00686"
self.user_participant = UserFactory().create(
new_id=uuid.UUID("eb41b762-5988-4fa3-8942-7a91ccb00686"),
token=uuid.UUID(self.user_token_participant),
username="john",
email="john@test.com",
)
self.user_repository.create(self.user_participant)

self.event_id = uuid.UUID("eb41b762-5988-4fa3-8942-7a91ccb00686")
self.event = EventFactory().create(new_id=self.event_id, name="HackUPC 2024")
self.event_repository.create(self.event)

def test__when_get_application_status_without_header__then_unauthorized_is_returned(
self,
) -> None:
# When
response = self.client.get(
"/organizator-api/applications/status/eb41b762-5988-4fa3-8942-7a91ccb00686",
content_type="application/json",
)

# Then
self.assertEqual(response.status_code, 401)
self.assertEqual(response.content, b"Unauthorized")

def test__given_a_invalid_token__when_get_application_status__then_invalid_token_is_returned(
self,
) -> None:
# When
headers = {"HTTP_Authorization": "invalid_token"}
response = self.client.get(
"/organizator-api/applications/status/eb41b762-5988-4fa3-8942-7a91ccb00686",
content_type="application/json",
**headers # type: ignore
)

# Then
self.assertEqual(response.status_code, 400)
self.assertEqual(response.content, b"Invalid token")

def test__given_a_valid_token_and_a_non_existing_event__when_get_application_status__then_event_not_found_is_returned(
self,
) -> None:
# When
headers = {"HTTP_Authorization": self.user_token_participant}
response = self.client.get(
"/organizator-api/applications/status/eb41b762-5988-4fa3-8942-7a91ccb00687",
content_type="application/json",
**headers # type: ignore
)

# Then
self.assertEqual(response.status_code, 404)
self.assertEqual(response.content, b"Event not found")

def test__given_a_exiting_event_and_a_token_for_a_non_existing_user__when_get_application_status__then_event_not_found_is_returned(
self,
) -> None:
# When
headers = {"HTTP_Authorization": str(uuid.uuid4())}
response = self.client.get(
"/organizator-api/applications/status/eb41b762-5988-4fa3-8942-7a91ccb00686",
content_type="application/json",
**headers # type: ignore
)

# Then
self.assertEqual(response.status_code, 404)
self.assertEqual(response.content, b"User not found")

def test__given_a_existing_event_and_a_existing_user_but_any_application_for_that_relation__when_get_application_status__then_not_applied_is_returned(
self,
) -> None:
# When
headers = {"HTTP_Authorization": self.user_token_participant}
response = self.client.get(
"/organizator-api/applications/status/eb41b762-5988-4fa3-8942-7a91ccb00686",
content_type="application/json",
**headers # type: ignore
)

# Then
self.assertEqual(response.status_code, 206)
self.assertEqual(response.content, b"Not applied")

def test__given_a_existing_event_and_a_existing_user_and_a_application_for_that_relation__when_get_application_status__then_applied_is_returned(
self,
) -> None:
# Given
application = ApplicationFactory().create(
new_id=uuid.UUID("eb41b762-5988-4fa3-8942-7a91ccb00686"),
user=self.user_participant,
event=self.event,
created_at=datetime(2024, 1, 9, 10, 47, 0, tzinfo=timezone.utc),
updated_at=datetime(2024, 1, 9, 10, 47, 0, tzinfo=timezone.utc),
)
self.application_repository.create(application)

# When
headers = {"HTTP_Authorization": self.user_token_participant}
response = self.client.get(
"/organizator-api/applications/status/eb41b762-5988-4fa3-8942-7a91ccb00686",
content_type="application/json",
**headers # type: ignore
)

# Then
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'{"status": "Under review"}')
Loading
Loading