""" Author: Michel Peltriaux Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany Contact: ksp-servicestelle@sgdnord.rlp.de Created on: 17.08.23 """ from datetime import timedelta from django.utils.timezone import now from api.models import APIUserToken from konova.tests.test_views import BaseTestCase class APIUserTokenTestCase(BaseTestCase): def setUp(self) -> None: super().setUp() self.token = APIUserToken.objects.create() self.superuser.api_token = self.token self.superuser.save() def test_str(self): self.assertEqual(str(self.token), self.token.token) def test_get_user_from_token(self): a_day = timedelta(days=1) today = now().date() self.assertFalse(self.token.is_active) self.assertIsNone(self.token.valid_until) try: #Token not existing --> fail token_user = APIUserToken.get_user_from_token(self.token.token[::-1]) self.fail("There should not have been any token") except PermissionError: pass try: # Token not active --> fail token_user = APIUserToken.get_user_from_token(self.token.token) self.fail("Token is unverified but token user has been fetchable.") except PermissionError: pass self.token.is_active = True self.token.valid_until = today - a_day self.token.save() try: # Token valid until yesterday --> fail token_user = APIUserToken.get_user_from_token(self.token.token) self.fail("Token reached end of lifetime but token user has been fetchable.") except PermissionError: pass # Token valid until tomorrow --> success self.token.valid_until = today + a_day self.token.save() token_user = APIUserToken.get_user_from_token(self.token.token) self.assertEqual(token_user, self.superuser) del token_user # Token valid forever --> success self.token.valid_until = None self.token.save() token_user = APIUserToken.get_user_from_token(self.token.token) self.assertEqual(token_user, self.superuser)