From 32d88c3a4968ccff61b6d0adf7ff18a9da40499a Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sat, 10 Apr 2021 23:42:42 +0200 Subject: [PATCH] core: consider never consider expiring models with self.expiring set to false expired Signed-off-by: Jens Langhammer --- authentik/core/models.py | 2 ++ authentik/core/tests/test_models.py | 26 ++++++++++++++++++++++++++ authentik/lib/sentry.py | 4 +++- authentik/lib/tests/test_sentry.py | 4 ++-- 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 authentik/core/tests/test_models.py diff --git a/authentik/core/models.py b/authentik/core/models.py index efd7ec686..501b556c7 100644 --- a/authentik/core/models.py +++ b/authentik/core/models.py @@ -322,6 +322,8 @@ class ExpiringModel(models.Model): @property def is_expired(self) -> bool: """Check if token is expired yet.""" + if not self.expiring: + return False return now() > self.expires class Meta: diff --git a/authentik/core/tests/test_models.py b/authentik/core/tests/test_models.py new file mode 100644 index 000000000..cb0444632 --- /dev/null +++ b/authentik/core/tests/test_models.py @@ -0,0 +1,26 @@ +"""authentik core models tests""" +from time import sleep + +from django.test import TestCase +from django.utils.timezone import now +from guardian.shortcuts import get_anonymous_user + +from authentik.core.models import Token + + +class TestModels(TestCase): + """Test Models""" + + def test_token_expire(self): + """Test token expiring""" + token = Token.objects.create(expires=now(), user=get_anonymous_user()) + sleep(0.5) + self.assertTrue(token.is_expired) + + def test_token_expire_no_expire(self): + """Test token expiring with "expiring" set """ + token = Token.objects.create( + expires=now(), user=get_anonymous_user(), expiring=False + ) + sleep(0.5) + self.assertFalse(token.is_expired) diff --git a/authentik/lib/sentry.py b/authentik/lib/sentry.py index 189f0d264..0decc5f6c 100644 --- a/authentik/lib/sentry.py +++ b/authentik/lib/sentry.py @@ -1,4 +1,6 @@ """authentik sentry integration""" +from typing import Optional + from aioredis.errors import ConnectionClosedError, ReplyError from billiard.exceptions import WorkerLostError from botocore.client import ClientError @@ -22,7 +24,7 @@ class SentryIgnoredException(Exception): """Base Class for all errors that are suppressed, and not sent to sentry.""" -def before_send(event: dict, hint: dict) -> dict: +def before_send(event: dict, hint: dict) -> Optional[dict]: """Check if error is database error, and ignore if so""" ignored_classes = ( # Inbuilt types diff --git a/authentik/lib/tests/test_sentry.py b/authentik/lib/tests/test_sentry.py index 023c82ba1..ba899dabb 100644 --- a/authentik/lib/tests/test_sentry.py +++ b/authentik/lib/tests/test_sentry.py @@ -10,9 +10,9 @@ class TestSentry(TestCase): def test_error_not_sent(self): """Test SentryIgnoredError not sent""" self.assertIsNone( - before_send(None, {"exc_info": (0, SentryIgnoredException(), 0)}) + before_send({}, {"exc_info": (0, SentryIgnoredException(), 0)}) ) def test_error_sent(self): """Test error sent""" - self.assertIsNone(before_send(None, {"exc_info": (0, ValueError(), 0)})) + self.assertEqual({}, before_send({}, {"exc_info": (0, ValueError(), 0)}))