Skip to content

Commit

Permalink
[ORG-131] Application can not be creation in a already started event …
Browse files Browse the repository at this point in the history
…use case (#116)

* usecase updated with the new exception

* exception added in view

* black

* test

* black
  • Loading branch information
carlotacb authored Jan 14, 2024
1 parent 09702c0 commit 812aeaa
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 8 deletions.
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 @@ -44,3 +44,7 @@ class ApplicationCanNotBeConfirmed(Exception):

class ApplicationCanNotBeAttended(Exception):
pass


class EventAlreadyStarted(Exception):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
UserIsNotAParticipant,
UserIsNotStudent,
UserIsTooYoung,
EventAlreadyStarted,
)
from app.applications.domain.models.application import Application, ApplicationStatus
from app.applications.infrastructure.repository_factories import (
Expand Down Expand Up @@ -50,6 +51,9 @@ def execute(self, token: uuid.UUID, event_id: uuid.UUID) -> None:
if age < event.age_restrictions:
raise UserIsTooYoung

if event.start_date < datetime.now():
raise EventAlreadyStarted

application = Application(
id=uuid.uuid4(),
user=user,
Expand Down
3 changes: 3 additions & 0 deletions organizator_api/app/applications/infrastructure/http/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
ApplicationCanNotBeCancelled,
ApplicationCanNotBeConfirmed,
ApplicationCanNotBeAttended,
EventAlreadyStarted,
)
from app.applications.domain.models.application import ApplicationStatus
from app.applications.domain.usecases.attended_application_use_case import (
Expand Down Expand Up @@ -90,6 +91,8 @@ def create_new_application(request: HttpRequest) -> HttpResponse:
return HttpResponse(status=401, content="You should be student to apply")
except UserIsTooYoung:
return HttpResponse(status=401, content="You are too young to apply")
except EventAlreadyStarted:
return HttpResponse(status=403, content="Event already started")

return HttpResponse(status=201, content="Application created correctly")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import uuid
from datetime import datetime
from datetime import datetime, timezone

from app.applications.domain.exceptions import (
ProfileNotComplete,
ApplicationAlreadyExists,
UserIsNotAParticipant,
UserIsTooYoung,
UserIsNotStudent,
EventAlreadyStarted,
)
from app.applications.domain.usecases.create_new_application_use_case import (
CreateNewApplicationUseCase,
Expand Down Expand Up @@ -148,6 +149,22 @@ def test__given_a_user_with_all_the_information_and_a_existing_event__when_creat
token=self.user_complete_token, event_id=self.event_id
)

def test__given_a_user_with_all_the_information_and_a_existing_event__when_create_application_to_a_already_started_event__then_event_already_started_is_raised(
self,
) -> None:
# Given
event = EventFactory().create(
name="HackUPC 2024",
start_date=datetime(2023, 5, 12, 16, 0),
)
self.event_repository.create(event)

# When / Then
with self.assertRaises(EventAlreadyStarted):
CreateNewApplicationUseCase().execute(
token=self.user_complete_token, event_id=event.id
)

def test__given_user_with_all_the_information_and_existing_event__when_create_application__then_the_application_is_created(
self,
) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,33 @@ def test__given_a_correct_event_id_and_a_user_too_young__when_create_application
self.assertEqual(response.status_code, 401)
self.assertEqual(response.content, b"You are too young to apply")

def test__given_a_correct_id_for_a_already_started_event_and_a_complete_user__when_create_application__then_event_already_started_is_returned(
self,
) -> None:
# Given
event_id = "fbce7302-68b2-48d3-9030-f6c56fcacf30"
event = EventFactory().create(
new_id=uuid.UUID(event_id),
start_date=datetime(2020, 1, 1),
name="HackUPC 2024",
)
self.event_repository.create(event)

body = {"event_id": event_id}

# When
headers = {"HTTP_Authorization": self.user_token_participant}
response = self.client.post(
"/organizator-api/applications/new",
json.dumps(body),
content_type="application/json",
**headers # type: ignore
)

# Then
self.assertEqual(response.status_code, 403)
self.assertEqual(response.content, b"Event already started")

def test__given_a_correct_event_id_and_a_complete_user__when_create_application__then_application_created_correctly_is_returned(
self,
) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ def test__given_a_valid_token_and_some_applications_for_a_user__when_get_user_ev
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.content,
b'[{"id": "ef6f6fb3-ba12-43dd-a0da-95de8125b1cc", "event": {"id": "eb41b762-5988-4fa3-8942-7a91ccb00686", "name": "HackUPC 2024", "url": "https://www.hackupc.com/", "description": "The biggest student hackathon in Europe", "start_date": "2023-05-12T16:00:00Z", "end_date": "2023-05-14T18:00:00Z", "location": "UPC Campus Nord", "header_image": "https://hackupc.com/ogimage.png", "deleted": false, "open_for_participants": true, "max_participants": 100, "expected_attrition_rate": 0.1, "students_only": true, "age_restrictions": 16}, "status": "Under review", "created_at": "2024-01-09T00:52:29Z", "updated_at": "2024-01-09T00:52:29Z"}, {"id": "eb41b762-5988-4fa3-8942-7a91ccb00687", "event": {"id": "ef6f6fb3-ba12-43dd-a0da-95de8125b1cc", "name": "HackUPC 2023", "url": "https://www.hackupc.com/", "description": "The biggest student hackathon in Europe", "start_date": "2023-05-12T16:00:00Z", "end_date": "2023-05-14T18:00:00Z", "location": "UPC Campus Nord", "header_image": "https://hackupc.com/ogimage.png", "deleted": false, "open_for_participants": true, "max_participants": 100, "expected_attrition_rate": 0.1, "students_only": true, "age_restrictions": 16}, "status": "Under review", "created_at": "2024-01-09T00:52:29Z", "updated_at": "2024-01-09T00:52:29Z"}]',
b'[{"id": "ef6f6fb3-ba12-43dd-a0da-95de8125b1cc", "event": {"id": "eb41b762-5988-4fa3-8942-7a91ccb00686", "name": "HackUPC 2024", "url": "https://www.hackupc.com/", "description": "The biggest student hackathon in Europe", "start_date": "2028-05-12T16:00:00Z", "end_date": "2028-05-14T18:00:00Z", "location": "UPC Campus Nord", "header_image": "https://hackupc.com/ogimage.png", "deleted": false, "open_for_participants": true, "max_participants": 100, "expected_attrition_rate": 0.1, "students_only": true, "age_restrictions": 16}, "status": "Under review", "created_at": "2024-01-09T00:52:29Z", "updated_at": "2024-01-09T00:52:29Z"}, {"id": "eb41b762-5988-4fa3-8942-7a91ccb00687", "event": {"id": "ef6f6fb3-ba12-43dd-a0da-95de8125b1cc", "name": "HackUPC 2023", "url": "https://www.hackupc.com/", "description": "The biggest student hackathon in Europe", "start_date": "2028-05-12T16:00:00Z", "end_date": "2028-05-14T18:00:00Z", "location": "UPC Campus Nord", "header_image": "https://hackupc.com/ogimage.png", "deleted": false, "open_for_participants": true, "max_participants": 100, "expected_attrition_rate": 0.1, "students_only": true, "age_restrictions": 16}, "status": "Under review", "created_at": "2024-01-09T00:52:29Z", "updated_at": "2024-01-09T00:52:29Z"}]',
)
4 changes: 2 additions & 2 deletions organizator_api/tests/events/domain/EventFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def create(
name: str = "HackUPC 2023",
description: str = "The biggest student hackathon in Europe",
url: str = "https://www.hackupc.com/",
start_date: datetime = datetime(2023, 5, 12, 16, 0),
end_date: datetime = datetime(2023, 5, 14, 18, 0),
start_date: datetime = datetime(2028, 5, 12, 16, 0),
end_date: datetime = datetime(2028, 5, 14, 18, 0),
location: str = "UPC Campus Nord",
header_image: str = "https://hackupc.com/ogimage.png",
created_at: datetime = datetime.now(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def test__given_events_in_db__when_get_all_events__then_returns_the_events_list(
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.content,
b'[{"id": "ef6f6fb3-ba12-43dd-a0da-95de8125b1cc", "name": "HackUPC 2023", "url": "https://www.hackupc.com/", "description": "The biggest student hackathon in Europe", "start_date": "2023-05-12T16:00:00Z", "end_date": "2023-05-14T18:00:00Z", "location": "UPC Campus Nord", "header_image": "https://hackupc.com/ogimage.png", "deleted": false, "open_for_participants": true, "max_participants": 100, "expected_attrition_rate": 0.1, "students_only": true, "age_restrictions": 16}, {"id": "be0f4c18-4a7c-4c1e-8a62-fc50916b6c88", "name": "HackUPC 2022", "url": "https://www.hackupc.com/", "description": "The biggest student hackathon in Europe", "start_date": "2023-05-12T16:00:00Z", "end_date": "2023-05-14T18:00:00Z", "location": "UPC Campus Nord", "header_image": "https://hackupc.com/ogimage.png", "deleted": false, "open_for_participants": true, "max_participants": 100, "expected_attrition_rate": 0.1, "students_only": true, "age_restrictions": 16}]',
b'[{"id": "ef6f6fb3-ba12-43dd-a0da-95de8125b1cc", "name": "HackUPC 2023", "url": "https://www.hackupc.com/", "description": "The biggest student hackathon in Europe", "start_date": "2028-05-12T16:00:00Z", "end_date": "2028-05-14T18:00:00Z", "location": "UPC Campus Nord", "header_image": "https://hackupc.com/ogimage.png", "deleted": false, "open_for_participants": true, "max_participants": 100, "expected_attrition_rate": 0.1, "students_only": true, "age_restrictions": 16}, {"id": "be0f4c18-4a7c-4c1e-8a62-fc50916b6c88", "name": "HackUPC 2022", "url": "https://www.hackupc.com/", "description": "The biggest student hackathon in Europe", "start_date": "2028-05-12T16:00:00Z", "end_date": "2028-05-14T18:00:00Z", "location": "UPC Campus Nord", "header_image": "https://hackupc.com/ogimage.png", "deleted": false, "open_for_participants": true, "max_participants": 100, "expected_attrition_rate": 0.1, "students_only": true, "age_restrictions": 16}]',
)

def test__given_event_deleted_in_db__when_get_all_events__then_it_returns_empty_list(
Expand Down Expand Up @@ -227,7 +227,7 @@ def test__given_events_in_db__when_get_all_upcoming_events__then_returns_the_eve
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.content,
b'[{"id": "be0f4c18-4a7c-4c1e-8a62-fc50916b6c88", "name": "HackUPC 2025", "url": "https://www.hackupc.com/", "description": "The biggest student hackathon in Europe", "start_date": "2025-05-12T16:00:00Z", "end_date": "2025-05-14T18:00:00Z", "location": "UPC Campus Nord", "header_image": "https://hackupc.com/ogimage.png", "deleted": false, "open_for_participants": true, "max_participants": 100, "expected_attrition_rate": 0.1, "students_only": true, "age_restrictions": 16}]',
b'[{"id": "ef6f6fb3-ba12-43dd-a0da-95de8125b1cc", "name": "HackUPC 2023", "url": "https://www.hackupc.com/", "description": "The biggest student hackathon in Europe", "start_date": "2028-05-12T16:00:00Z", "end_date": "2028-05-14T18:00:00Z", "location": "UPC Campus Nord", "header_image": "https://hackupc.com/ogimage.png", "deleted": false, "open_for_participants": true, "max_participants": 100, "expected_attrition_rate": 0.1, "students_only": true, "age_restrictions": 16}, {"id": "be0f4c18-4a7c-4c1e-8a62-fc50916b6c88", "name": "HackUPC 2025", "url": "https://www.hackupc.com/", "description": "The biggest student hackathon in Europe", "start_date": "2025-05-12T16:00:00Z", "end_date": "2025-05-14T18:00:00Z", "location": "UPC Campus Nord", "header_image": "https://hackupc.com/ogimage.png", "deleted": false, "open_for_participants": true, "max_participants": 100, "expected_attrition_rate": 0.1, "students_only": true, "age_restrictions": 16}]',
)

def test__given_events_in_db__when_get_event_by_id__then_returns_the_event(
Expand All @@ -246,7 +246,7 @@ def test__given_events_in_db__when_get_event_by_id__then_returns_the_event(
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.content,
b'{"id": "ef6f6fb3-ba12-43dd-a0da-95de8125b1cc", "name": "HackUPC 2023", "url": "https://www.hackupc.com/", "description": "The biggest student hackathon in Europe", "start_date": "2023-05-12T16:00:00Z", "end_date": "2023-05-14T18:00:00Z", "location": "UPC Campus Nord", "header_image": "https://hackupc.com/ogimage.png", "deleted": false, "open_for_participants": true, "max_participants": 100, "expected_attrition_rate": 0.1, "students_only": true, "age_restrictions": 16}',
b'{"id": "ef6f6fb3-ba12-43dd-a0da-95de8125b1cc", "name": "HackUPC 2023", "url": "https://www.hackupc.com/", "description": "The biggest student hackathon in Europe", "start_date": "2028-05-12T16:00:00Z", "end_date": "2028-05-14T18:00:00Z", "location": "UPC Campus Nord", "header_image": "https://hackupc.com/ogimage.png", "deleted": false, "open_for_participants": true, "max_participants": 100, "expected_attrition_rate": 0.1, "students_only": true, "age_restrictions": 16}',
)

def test__when_get_event_by_nonexistent_id__then_returns_the_event(
Expand Down Expand Up @@ -343,7 +343,7 @@ def test__given_only_some_information_to_update_an_event__when_update_event__the
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.content,
b'{"id": "ef6f6fb3-ba12-43dd-a0da-95de8125b1cc", "name": "HackUPC 2023", "url": "https://2023.hackupc.com/", "description": "The biggest student hackathon in Europe taking place in Barcelona", "start_date": "2023-05-12T16:00:00Z", "end_date": "2023-05-14T18:00:00Z", "location": "UPC Campus Nord", "header_image": "https://hackupc.com/ogimage.png", "deleted": false, "open_for_participants": true, "max_participants": 100, "expected_attrition_rate": 0.1, "students_only": true, "age_restrictions": 16}',
b'{"id": "ef6f6fb3-ba12-43dd-a0da-95de8125b1cc", "name": "HackUPC 2023", "url": "https://2023.hackupc.com/", "description": "The biggest student hackathon in Europe taking place in Barcelona", "start_date": "2028-05-12T16:00:00Z", "end_date": "2028-05-14T18:00:00Z", "location": "UPC Campus Nord", "header_image": "https://hackupc.com/ogimage.png", "deleted": false, "open_for_participants": true, "max_participants": 100, "expected_attrition_rate": 0.1, "students_only": true, "age_restrictions": 16}',
)

events = self.event_repository.get_all()
Expand Down

0 comments on commit 812aeaa

Please sign in to comment.