From 96ecf8d5cbd3d663c573342b8b28a42cee87ac9b Mon Sep 17 00:00:00 2001 From: Mads Nylund <73914541+MadsNyl@users.noreply.github.com> Date: Fri, 13 Oct 2023 17:10:49 +0200 Subject: [PATCH] refactored exception for order init error (#712) * refactored exception for order init error * added error when trying to update a paid event to a free event --- app/content/exceptions.py | 5 +++ app/content/serializers/event.py | 6 +-- app/content/views/registration.py | 12 +++++- .../payment/test_paid_event_integration.py | 40 ++++++++++--------- 4 files changed, 39 insertions(+), 24 deletions(-) diff --git a/app/content/exceptions.py b/app/content/exceptions.py index ff83e3f28..d53592d30 100644 --- a/app/content/exceptions.py +++ b/app/content/exceptions.py @@ -2,6 +2,11 @@ from rest_framework.exceptions import APIException +class APIEventIsPaidEventException(APIException): + status_code = status.HTTP_400_BAD_REQUEST + default_detail = "Arrangementet er et betalt arrangement, og kan ikke endres til et gratis arrangement" + + class APIUserAlreadyAttendedEvent(APIException): status_code = status.HTTP_400_BAD_REQUEST default_detail = "Brukeren har allerede ankommet" diff --git a/app/content/serializers/event.py b/app/content/serializers/event.py index b18230a8e..ebe9d72ef 100644 --- a/app/content/serializers/event.py +++ b/app/content/serializers/event.py @@ -5,6 +5,7 @@ from app.common.enums import GroupType from app.common.serializers import BaseModelSerializer +from app.content.exceptions import APIEventIsPaidEventException from app.content.models import Event, PriorityPool from app.content.serializers.priority_pool import ( PriorityPoolCreateSerializer, @@ -178,10 +179,7 @@ def update(self, instance, validated_data): ) if event.is_paid_event and not len(paid_information_data): - paid_event = PaidEvent.objects.get(event=event) - if paid_event: - paid_event.delete() - event.paid_information = None + raise APIEventIsPaidEventException() if len(paid_information_data): self.update_paid_information(event, paid_information_data) diff --git a/app/content/views/registration.py b/app/content/views/registration.py index d899e2031..4dfd3437b 100644 --- a/app/content/views/registration.py +++ b/app/content/views/registration.py @@ -3,6 +3,8 @@ from rest_framework.exceptions import PermissionDenied from rest_framework.response import Response +from sentry_sdk import capture_exception + from app.common.pagination import BasePagination from app.common.permissions import BasicViewPermission, is_admin_user from app.common.viewsets import BaseViewSet @@ -67,9 +69,15 @@ def create(self, request, *args, **kwargs): try: create_payment_order(event, request, registration) - except Exception as e: + except Exception as order_error: + capture_exception(order_error) registration.delete() - raise e + return Response( + { + "detail": "Det skjedde en feil med opprettelse av betalingsordre. Påmeldingen ble ikke fullført." + }, + status=status.HTTP_400_BAD_REQUEST, + ) registration_serializer = RegistrationSerializer( registration, context={"user": registration.user} diff --git a/app/tests/payment/test_paid_event_integration.py b/app/tests/payment/test_paid_event_integration.py index d33370828..a759cdfa5 100644 --- a/app/tests/payment/test_paid_event_integration.py +++ b/app/tests/payment/test_paid_event_integration.py @@ -28,6 +28,7 @@ def get_paid_event_data( organizer=None, price=100.00, paytime="01:00:00", + is_paid_event=True, ): start_date = timezone.now() + timedelta(days=10) end_date = timezone.now() + timedelta(days=11) @@ -36,7 +37,7 @@ def get_paid_event_data( "location": location, "start_date": start_date, "end_date": end_date, - "is_paid_event": True, + "is_paid_event": is_paid_event, "paid_information": {"price": price, "paytime": paytime}, } if organizer: @@ -52,7 +53,7 @@ def _get_registration_post_data(user, event): def get_paid_event_without_price_data( - title="New Title", location="New Location", organizer=None + title="New Title", location="New Location", organizer=None, is_paid_event=True ): start_date = timezone.now() + timedelta(days=10) end_date = timezone.now() + timedelta(days=11) @@ -61,7 +62,7 @@ def get_paid_event_without_price_data( "location": location, "start_date": start_date, "end_date": end_date, - "is_paid_event": True, + "is_paid_event": is_paid_event, } if organizer: data["organizer"] = organizer @@ -134,21 +135,24 @@ def test_update_paid_event_as_admin(admin_user): assert float(response.data["paid_information"]["price"]) == new_event_price -# @pytest.mark.django_db -# def test_delete_paid_event_as_admin(admin_user, paid_event): -# client = get_api_client(user=admin_user) -# event = paid_event.event +@pytest.mark.django_db +def test_update_paid_event_to_free_event_as_admin(admin_user): + """ + HS and Index members should not be able to update a paid event to a free event. + Other subgroup members can update paid events where event.organizer is their group or None. + Leaders of committees and interest groups should be able to + update events where event.organizer is their group or None. + """ -# url = _get_registration_url(event=event) -# data = _get_registration_post_data(admin_user, event) -# response = client.post(url, data=data) -# assert response.status_code == 201 + paid_event = PaidEventFactory(price=100.00) + event = paid_event.event + organizer = Group.objects.get_or_create(name="HS", type=GroupType.BOARD)[0] + client = get_api_client(user=admin_user) + url = get_events_url_detail(event) + data = get_paid_event_data(organizer=organizer.slug, is_paid_event=False) -# url = get_events_url_detail(event) -# event_response = client.delete(url) -# paid_events = PaidEvent.objects.all() -# orders = Order.objects.all() + response = client.put(url, data) + event.refresh_from_db() -# assert event_response.status_code == 200 -# assert len(paid_events) == 0 -# assert len(orders) == 0 + assert response.status_code == 400 + assert event.is_paid_event