Skip to content

Commit

Permalink
refactored exception for order init error (#712)
Browse files Browse the repository at this point in the history
* refactored exception for order init error

* added error when trying to update a paid event to a free event
  • Loading branch information
MadsNyl authored Oct 13, 2023
1 parent 1c5952f commit 96ecf8d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
5 changes: 5 additions & 0 deletions app/content/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 2 additions & 4 deletions app/content/serializers/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions app/content/views/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
40 changes: 22 additions & 18 deletions app/tests/payment/test_paid_event_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 96ecf8d

Please sign in to comment.