Skip to content
Snippets Groups Projects
Commit 7c148274 authored by Benjamin Hättasch's avatar Benjamin Hättasch
Browse files

Add test for interest counter API endpoint

Fix issue detected while creating test
parent dfd48ae0
No related branches found
No related tags found
1 merge request!141Add view tests
Pipeline #126994 passed
......@@ -26,8 +26,8 @@ def increment_interest_counter(request, event_slug, pk, **kwargs):
"""
Increment interest counter for AK
"""
ak = AK.objects.get(pk=pk)
if ak:
try:
ak = AK.objects.get(pk=pk)
# Check whether interest indication is currently allowed
current_timestamp = datetime.now().astimezone(ak.event.timezone)
if ak_interest_indication_active(ak.event, current_timestamp):
......@@ -35,4 +35,5 @@ def increment_interest_counter(request, event_slug, pk, **kwargs):
ak.save()
return Response({'interest_counter': ak.interest_counter}, status=status.HTTP_200_OK)
return Response(status=status.HTTP_403_FORBIDDEN)
return Response(status=status.HTTP_404_NOT_FOUND)
except AK.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
from datetime import timedelta
from django.test import TestCase
from django.urls import reverse_lazy
from django.utils.datetime_safe import datetime
from AKModel.models import AK, AKSlot
from AKModel.models import AK, AKSlot, Event
from AKModel.tests import BasicViewTests
......@@ -124,3 +127,36 @@ class ModelViewTests(BasicViewTests, TestCase):
self._assert_message(response, "Message to organizers successfully saved")
self.assertEqual(AK.objects.get(pk=1).akorgamessage_set.count(), count_messages + 1,
msg="Message was not correctly saved")
def test_interest_api(self):
interest_api_url = "/kif42/api/ak/1/indicate-interest/"
ak = AK.objects.get(pk=1)
event = Event.objects.get(slug='kif42')
ak_interest_counter = ak.interest_counter
response = self.client.get(interest_api_url)
self.assertEqual(response.status_code, 405, "Should not be accessible via GET")
event.interest_start = datetime.now().astimezone(event.timezone) + timedelta(minutes=-10)
event.interest_end = datetime.now().astimezone(event.timezone) + timedelta(minutes=+10)
event.save()
response = self.client.post(interest_api_url)
self.assertEqual(response.status_code, 200, f"API end point not working ({interest_api_url})")
self.assertEqual(AK.objects.get(pk=1).interest_counter, ak_interest_counter + 1, "Counter was not increased")
event.interest_end = datetime.now().astimezone(event.timezone) + timedelta(minutes=-2)
event.save()
response = self.client.post(interest_api_url)
self.assertEqual(response.status_code, 403,
"API end point still reachable even though interest indication window ended ({interest_api_url})")
self.assertEqual(AK.objects.get(pk=1).interest_counter, ak_interest_counter + 1,
"Counter was increased even though interest indication window ended")
invalid_interest_api_url = "/kif42/api/ak/-1/indicate-interest/"
response = self.client.post(invalid_interest_api_url)
self.assertEqual(response.status_code, 404, f"Invalid URL reachable ({interest_api_url})")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment