Skip to content

Commit

Permalink
Fix smaller changes requested in PR review.
Browse files Browse the repository at this point in the history
Fix create_mockdata for PR.

Save mail in start_payment and get ticket before looping in reserve_ticket.

Split tests of negative tickets and zero tickets in reservation.

Fix testing of test_reserve ticket conflicting with max_ticket_per_payment when testing reserving more tickets than there are seats left.
  • Loading branch information
Gabrimari committed Dec 11, 2024
2 parents 71e14a4 + 21e0442 commit ee6e078
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,31 @@ def test_reserve_ticket(self):


def test_out_of_tickets(self):
prep_res = self.client.post(
"/reserve_ticket/",
{
"chapter_event": str(self.test_event.pk),
"tickets": [
{
"ticket_type": "Test Ticket",
"count": 5
}
]
},
content_type="application/json"
)

if prep_res.status_code != 201:
raise Exception("Failed to perform reservation of tickets in preparation for testing test_expired_session_out_of_tickets.")

response = self.client.post(
"/reserve_ticket/",
{
"chapter_event": str(self.test_event.pk),
"tickets": [
{
"ticket_type": "Test Ticket",
"count": 11
"count": 6
}
]
},
Expand All @@ -64,6 +81,18 @@ def test_negative_tickets(self):
"ticket_type": "Test Ticket",
"count": -1
},
]
},
content_type="application/json"
)
self.assertEqual(response.status_code, 400)

def test_zero_tickets(self):
response = self.client.post(
"/reserve_ticket/",
{
"chapter_event": str(self.test_event.pk),
"tickets": [
{
"ticket_type": "Test Ticket 2",
"count": 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_expired_session_out_of_tickets(self):
payment.save()

client2 = Client()
_ = client2.post(
prep_res = client2.post(
"/reserve_ticket/",
{
"chapter_event": str(self.test_event.pk),
Expand All @@ -95,6 +95,9 @@ def test_expired_session_out_of_tickets(self):
content_type="application/json"
)

if prep_res.status_code != 201:
raise Exception("Failed to perform reservation of tickets in preparation for testing test_expired_session_out_of_tickets.")

response = self.client.post(
"/start_payment/",
{
Expand Down
2 changes: 1 addition & 1 deletion backend/bittan/bittan/tests/views/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_filled(self):
raise Exception("No corresponding ticket_type found")
self.assertEqual(tt['title'], corresponding_tt_db.title)
self.assertEqual(tt['description'], corresponding_tt_db.description)
self.assertEqual(type(tt['price']), str) # We don't bother checking Money too closely because we're gonna change it anyway
self.assertEqual(type(tt['price']), int) # We don't bother checking Money too closely because we're gonna change it anyway

def test_sales_stop_at_past(self):
now = timezone.now()
Expand Down
17 changes: 10 additions & 7 deletions backend/bittan/bittan/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class TicketsSerializer(serializers.Serializer):
ticket_type = serializers.CharField(required=True)
count = serializers.IntegerField(required=True, min_value=0)
count = serializers.IntegerField(required=True, min_value=1)

class ReserveTicketRequestSerializer(serializers.Serializer):
chapter_event = serializers.CharField(required = True)
Expand Down Expand Up @@ -70,6 +70,13 @@ def reserve_ticket(request: Request) -> Response:
)

for ticket in tickets:
try:
ticket_type = TicketType.objects.get(title=ticket["ticket_type"], is_visible=True)
except ObjectDoesNotExist:
return Response(
"TicketTypeDoesNotExist",
status=status.HTTP_404_NOT_FOUND
)
for _ in range(ticket["count"]):
# Attempts to create a ticket a maximum of 1000 times. This is to ensure that the external_id of the ticket is guaranteed to be unique.
for _ in range(1000):
Expand All @@ -78,13 +85,8 @@ def reserve_ticket(request: Request) -> Response:
external_id=''.join(random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZ") for _ in range(6)),
time_created=timezone.now(),
payment=payment,
ticket_type=TicketType.objects.get(title=ticket["ticket_type"], is_visible=True)
ticket_type=ticket_type
)
except ObjectDoesNotExist:
return Response(
"TicketTypeDoesNotExist",
status=status.HTTP_404_NOT_FOUND
)
except IntegrityError as e:
continue
break
Expand Down Expand Up @@ -145,6 +147,7 @@ def start_payment(request):
payment.payment_started = True
payment.save()
payment.email = response_data["email_address"]
payment.save()

total_price = tickets.aggregate(Sum("ticket_type__price"))["ticket_type__price__sum"]

Expand Down
5 changes: 1 addition & 4 deletions backend/bittan/scripts/create_mockdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,12 @@
chapter_event1 = ChapterEvent.objects.create(title="Fysikalen Dag 1", description="Första dagen av Fysikalen.", total_seats=10, sales_stop_at=NOW+datetime.timedelta(days=365), event_at=NOW+datetime.timedelta(days=365))
chapter_event1.ticket_types.add(standardbiljett, studentbiljett)

chapter_event2 = ChapterEvent.objects.create(title="Fysikalen Dag 2", description="Andra dagen av Fysikalen.", max_tickets=10, sales_stop_at=NOW+datetime.timedelta(days=365), event_at=NOW+datetime.timedelta(days=366), swish_message="Hej på dig du. ")


payment1 = Payment.objects.create(
expires_at = NOW + datetime.timedelta(hours=1),
swish_id = "Hej",
status = PaymentStatus.RESERVED,
email = "[email protected]",
sent_email = True
sent_email = False
)

ticket1 = Ticket.objects.create(
Expand Down

0 comments on commit ee6e078

Please sign in to comment.