From 7c148274fffb0ea615b1a70f1133d8c34fa9064c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?=
 <benjamin.haettasch@fachschaft.informatik.tu-darmstadt.de>
Date: Mon, 26 Dec 2022 16:12:07 +0100
Subject: [PATCH] Add test for interest counter API endpoint

Fix issue detected while creating test
---
 AKSubmission/api.py   |  7 ++++---
 AKSubmission/tests.py | 38 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/AKSubmission/api.py b/AKSubmission/api.py
index 29a6f42a..db46d40c 100644
--- a/AKSubmission/api.py
+++ b/AKSubmission/api.py
@@ -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)
diff --git a/AKSubmission/tests.py b/AKSubmission/tests.py
index de0ede7e..4cff0830 100644
--- a/AKSubmission/tests.py
+++ b/AKSubmission/tests.py
@@ -1,7 +1,10 @@
+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})")
+
+
-- 
GitLab