Skip to content

Commit

Permalink
added tests a few tests for ticketing
Browse files Browse the repository at this point in the history
  • Loading branch information
Porcupine1 committed Jun 3, 2024
1 parent ee7f005 commit 5f526b3
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 7 deletions.
15 changes: 8 additions & 7 deletions backend/clubs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down
66 changes: 66 additions & 0 deletions backend/tests/clubs/test_ticketing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 5f526b3

Please sign in to comment.