core: consider never consider expiring models with self.expiring set to false expired

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-04-10 23:42:42 +02:00
parent 5522c94b65
commit 32d88c3a49
4 changed files with 33 additions and 3 deletions

View file

@ -322,6 +322,8 @@ class ExpiringModel(models.Model):
@property @property
def is_expired(self) -> bool: def is_expired(self) -> bool:
"""Check if token is expired yet.""" """Check if token is expired yet."""
if not self.expiring:
return False
return now() > self.expires return now() > self.expires
class Meta: class Meta:

View file

@ -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)

View file

@ -1,4 +1,6 @@
"""authentik sentry integration""" """authentik sentry integration"""
from typing import Optional
from aioredis.errors import ConnectionClosedError, ReplyError from aioredis.errors import ConnectionClosedError, ReplyError
from billiard.exceptions import WorkerLostError from billiard.exceptions import WorkerLostError
from botocore.client import ClientError 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.""" """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""" """Check if error is database error, and ignore if so"""
ignored_classes = ( ignored_classes = (
# Inbuilt types # Inbuilt types

View file

@ -10,9 +10,9 @@ class TestSentry(TestCase):
def test_error_not_sent(self): def test_error_not_sent(self):
"""Test SentryIgnoredError not sent""" """Test SentryIgnoredError not sent"""
self.assertIsNone( self.assertIsNone(
before_send(None, {"exc_info": (0, SentryIgnoredException(), 0)}) before_send({}, {"exc_info": (0, SentryIgnoredException(), 0)})
) )
def test_error_sent(self): def test_error_sent(self):
"""Test error sent""" """Test error sent"""
self.assertIsNone(before_send(None, {"exc_info": (0, ValueError(), 0)})) self.assertEqual({}, before_send({}, {"exc_info": (0, ValueError(), 0)}))