From 5f526b31db5bd4d0fdb1b7e69d78a2993e7266b0 Mon Sep 17 00:00:00 2001 From: Thomas Ngulube <47449914+Porcupine1@users.noreply.github.com> Date: Mon, 3 Jun 2024 17:11:10 -0400 Subject: [PATCH] added tests a few tests for ticketing --- backend/clubs/views.py | 15 +++--- backend/tests/clubs/test_ticketing.py | 66 +++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/backend/clubs/views.py b/backend/clubs/views.py index 8fa7c3545..94ba647fb 100644 --- a/backend/clubs/views.py +++ b/backend/clubs/views.py @@ -5047,7 +5047,6 @@ def initiate_checkout(self, request, *args, **kwargs): order_info = { "amountDetails": {"totalAmount": "0.00"}, "billTo": { - "reconciliationId": None, "firstName": self.request.user.first_name, "lastName": self.request.user.last_name, "phoneNumber": None, @@ -5056,7 +5055,7 @@ def initiate_checkout(self, request, *args, **kwargs): } # Place hold on tickets for 10 mins - self._place_hold_on_tickets(tickets) + self._place_hold_on_tickets(self.request.user, tickets) # Skip payment process and give tickets to user/buyer self._give_tickets(self.request.user, order_info, cart, None) @@ -5112,7 +5111,7 @@ def initiate_checkout(self, request, *args, **kwargs): cart.save() # Place hold on tickets for 10 mins - self._place_hold_on_tickets(tickets) + self._place_hold_on_tickets(self.request.user, tickets) return Response( { @@ -5354,7 +5353,8 @@ def transfer(self, request, *args, **kwargs): def get_queryset(self): return Ticket.objects.filter(owner=self.request.user.id) - def _give_tickets(self, user, order_info, cart, reconciliation_id): + @staticmethod + def _give_tickets(user, order_info, cart, reconciliation_id): """ Helper function that gives user/buyer their held tickets and archives the transaction data @@ -5382,7 +5382,7 @@ def _give_tickets(self, user, order_info, cart, reconciliation_id): ) ) - tickets.update(owner=self.request.user, holder=None) + tickets.update(owner=user, holder=None) TicketTransactionRecord.objects.bulk_create(transaction_records) cart.tickets.clear() for ticket in tickets: @@ -5393,12 +5393,13 @@ def _give_tickets(self, user, order_info, cart, reconciliation_id): cart.checkout_context = None cart.save() - def _place_hold_on_tickets(self, tickets): + @staticmethod + def _place_hold_on_tickets(user, tickets): """ Helper function that places a 10 minute hold on tickets for a user """ holding_expiration = timezone.now() + datetime.timedelta(minutes=10) - tickets.update(holder=self.request.user, holding_expiration=holding_expiration) + tickets.update(holder=user, holding_expiration=holding_expiration) class MemberInviteViewSet(viewsets.ModelViewSet): diff --git a/backend/tests/clubs/test_ticketing.py b/backend/tests/clubs/test_ticketing.py index 74895c3be..6a250f921 100644 --- a/backend/tests/clubs/test_ticketing.py +++ b/backend/tests/clubs/test_ticketing.py @@ -824,6 +824,72 @@ def test_get_cart_replacement_required_sold_out(self): to_add = set(map(lambda t: str(t.id), tickets_to_add)) self.assertEqual(len(in_cart & to_add), 0, in_cart | to_add) + def test_place_hold_on_tickets(self): + from clubs.views import TicketViewSet + + self.client.login(username=self.user1.username, password="test") + + # Add a few tickets + cart, _ = Cart.objects.get_or_create(owner=self.user1) + tickets_to_add = self.tickets1[:3] + for ticket in tickets_to_add: + cart.tickets.add(ticket) + cart.save() + + TicketViewSet._place_hold_on_tickets(self.user1, cart.tickets) + holding_expiration = timezone.now() + timezone.timedelta(minutes=10) + + for ticket in cart.tickets.all(): + self.assertIsNone(ticket.owner) + self.assertEqual(self.user1, ticket.holder) + self.assertAlmostEqual( + holding_expiration, + ticket.holding_expiration, + delta=timedelta(seconds=10), + ) + + # Move Django's internal clock 10 minutes forward + with freezegun.freeze_time(holding_expiration): + Ticket.objects.update_holds() + for ticket in cart.tickets.all(): + self.assertIsNone(ticket.owner) + self.assertIsNone(ticket.holder) + # TODO: Ask whether to reset holding expiration to None + + def test_give_tickets(self): + from clubs.views import TicketViewSet + + self.client.login(username=self.user1.username, password="test") + # Add a few tickets + cart, _ = Cart.objects.get_or_create(owner=self.user1) + tickets_to_add = self.tickets1[:3] + for ticket in tickets_to_add: + cart.tickets.add(ticket) + cart.save() + + order_info = { + "amountDetails": {"totalAmount": TicketViewSet._calculate_cart_total(cart)}, + "billTo": { + "firstName": self.user1.first_name, + "lastName": self.user1.last_name, + "phoneNumber": None, + "email": self.user1.email, + }, + } + + TicketViewSet._place_hold_on_tickets(self.user1, cart.tickets) + TicketViewSet._give_tickets( + self.user1, order_info, cart, reconciliation_id=None + ) + + # Check that tickets are assigned their owner + for ticket in cart.tickets.all(): + self.assertEqual(self.user1, ticket.owner) + self.assertIsNone(ticket.holder) + + # Check that the cart is empty + self.assertEqual(0, cart.tickets.count()) + def test_initiate_checkout_non_free_tickets(self): self.client.login(username=self.user1.username, password="test")