authentik.enterprise.tests
1from collections.abc import Callable 2from datetime import timedelta 3from functools import wraps 4from time import mktime 5from unittest.mock import MagicMock, patch 6 7from django.utils.timezone import now 8 9from authentik.enterprise.license import LicenseKey 10from authentik.enterprise.models import THRESHOLD_READ_ONLY_WEEKS, License 11from authentik.lib.generators import generate_id 12 13# Valid license expiry 14expiry_valid = int(mktime((now() + timedelta(days=3000)).timetuple())) 15# Valid license expiry, expires soon 16expiry_soon = int(mktime((now() + timedelta(hours=10)).timetuple())) 17# Invalid license expiry, recently expired 18expiry_expired = int(mktime((now() - timedelta(hours=10)).timetuple())) 19# Invalid license expiry, expired longer ago 20expiry_expired_read_only = int( 21 mktime((now() - timedelta(weeks=THRESHOLD_READ_ONLY_WEEKS + 1)).timetuple()) 22) 23 24 25def enterprise_test( 26 expiry: int = expiry_valid, 27 internal_users: int = 100, 28 external_users: int = 100, 29 create_key=True, 30): 31 """Install testing enterprise license""" 32 33 def wrapper_outer(func: Callable): 34 # Mirror `unittest.mock.patch`'s class decoration: wrap each test method 35 # individually. Wrapping the class itself would replace it with a function, 36 # and pytest would silently collect no tests from it. 37 if isinstance(func, type): 38 for name in dir(func): 39 if not name.startswith("test"): 40 continue 41 attr = getattr(func, name) 42 if callable(attr): 43 setattr(func, name, wrapper_outer(attr)) 44 return func 45 46 @wraps(func) 47 def wrapper(*args, **kwargs): 48 with ( 49 patch( 50 "authentik.enterprise.license.LicenseKey.validate", 51 MagicMock( 52 return_value=LicenseKey( 53 aud="", 54 exp=expiry, 55 name=generate_id(), 56 internal_users=internal_users, 57 external_users=external_users, 58 ) 59 ), 60 ), 61 # `License.save()`/`.delete()` synchronously dispatch `enterprise_update_usage` 62 # in tests (see `authentik.tasks.test.TestBroker`), which would otherwise call 63 # the real `record_usage()` here, before the caller's own mocks are active, and 64 # cache a bogus license status for the rest of the test. 65 patch("authentik.enterprise.license.LicenseKey.record_usage", MagicMock()), 66 ): 67 if create_key: 68 License.objects.all().delete() 69 License.objects.create(key=generate_id()) 70 return func(*args, **kwargs) 71 72 return wrapper 73 74 return wrapper_outer
expiry_valid =
2055609410
expiry_soon =
1796445410
expiry_expired =
1796373410
expiry_expired_read_only =
1792175810
def
enterprise_test( expiry: int = 2055609410, internal_users: int = 100, external_users: int = 100, create_key=True):
26def enterprise_test( 27 expiry: int = expiry_valid, 28 internal_users: int = 100, 29 external_users: int = 100, 30 create_key=True, 31): 32 """Install testing enterprise license""" 33 34 def wrapper_outer(func: Callable): 35 # Mirror `unittest.mock.patch`'s class decoration: wrap each test method 36 # individually. Wrapping the class itself would replace it with a function, 37 # and pytest would silently collect no tests from it. 38 if isinstance(func, type): 39 for name in dir(func): 40 if not name.startswith("test"): 41 continue 42 attr = getattr(func, name) 43 if callable(attr): 44 setattr(func, name, wrapper_outer(attr)) 45 return func 46 47 @wraps(func) 48 def wrapper(*args, **kwargs): 49 with ( 50 patch( 51 "authentik.enterprise.license.LicenseKey.validate", 52 MagicMock( 53 return_value=LicenseKey( 54 aud="", 55 exp=expiry, 56 name=generate_id(), 57 internal_users=internal_users, 58 external_users=external_users, 59 ) 60 ), 61 ), 62 # `License.save()`/`.delete()` synchronously dispatch `enterprise_update_usage` 63 # in tests (see `authentik.tasks.test.TestBroker`), which would otherwise call 64 # the real `record_usage()` here, before the caller's own mocks are active, and 65 # cache a bogus license status for the rest of the test. 66 patch("authentik.enterprise.license.LicenseKey.record_usage", MagicMock()), 67 ): 68 if create_key: 69 License.objects.all().delete() 70 License.objects.create(key=generate_id()) 71 return func(*args, **kwargs) 72 73 return wrapper 74 75 return wrapper_outer
Install testing enterprise license