simplify and patch enterprise
Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
@ -1,10 +1,15 @@
|
|||||||
"""authentik e2e testing utilities"""
|
"""authentik e2e testing utilities"""
|
||||||
|
|
||||||
import traceback
|
from datetime import timedelta
|
||||||
|
from time import mktime
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
from daphne.testing import DaphneProcess, _reinstall_reactor
|
from daphne.testing import DaphneProcess
|
||||||
from django import setup as django_setup
|
from django import setup as django_setup
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.utils.timezone import now
|
||||||
|
|
||||||
|
from authentik.lib.generators import generate_id
|
||||||
|
|
||||||
|
|
||||||
class TestDatabaseProcess(DaphneProcess):
|
class TestDatabaseProcess(DaphneProcess):
|
||||||
@ -12,41 +17,24 @@ class TestDatabaseProcess(DaphneProcess):
|
|||||||
https://github.com/django/channels/issues/2048"""
|
https://github.com/django/channels/issues/2048"""
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
# OK, now we are in a forked child process, and want to use the reactor.
|
|
||||||
# However, FreeBSD systems like MacOS do not fork the underlying Kqueue,
|
|
||||||
# which asyncio (hence asyncioreactor) is built on.
|
|
||||||
# Therefore, we should uninstall the broken reactor and install a new one.
|
|
||||||
_reinstall_reactor()
|
|
||||||
|
|
||||||
if not settings.configured: # Fix For raise AppRegistryNotReady("Apps aren't loaded yet.")
|
if not settings.configured: # Fix For raise AppRegistryNotReady("Apps aren't loaded yet.")
|
||||||
django_setup() # Ensure Django is fully set up before using settings
|
django_setup() # Ensure Django is fully set up before using settings
|
||||||
|
|
||||||
from daphne.endpoints import build_endpoint_description_strings
|
|
||||||
from daphne.server import Server
|
|
||||||
from twisted.internet import reactor
|
|
||||||
|
|
||||||
application = self.get_application()
|
|
||||||
if not settings.DATABASES[list(settings.DATABASES.keys())[0]]["NAME"].startswith("test_"):
|
if not settings.DATABASES[list(settings.DATABASES.keys())[0]]["NAME"].startswith("test_"):
|
||||||
for _, db_settings in settings.DATABASES.items():
|
for _, db_settings in settings.DATABASES.items():
|
||||||
db_settings["NAME"] = f"test_{db_settings['NAME']}"
|
db_settings["NAME"] = f"test_{db_settings['NAME']}"
|
||||||
settings.TEST = True
|
settings.TEST = True
|
||||||
|
from authentik.enterprise.license import LicenseKey
|
||||||
|
|
||||||
try:
|
with patch(
|
||||||
# Create the server class
|
"authentik.enterprise.license.LicenseKey.validate",
|
||||||
endpoints = build_endpoint_description_strings(host=self.host, port=0)
|
MagicMock(
|
||||||
self.server = Server(
|
return_value=LicenseKey(
|
||||||
application=application, endpoints=endpoints, signal_handlers=False, **self.kwargs
|
aud="",
|
||||||
)
|
exp=int(mktime((now() + timedelta(days=3000)).timetuple())),
|
||||||
# Set up a poller to look for the port
|
name=generate_id(),
|
||||||
reactor.callLater(0.1, self.resolve_port)
|
internal_users=100,
|
||||||
# Run with setup/teardown
|
external_users=100,
|
||||||
if self.setup is not None:
|
)
|
||||||
self.setup()
|
),
|
||||||
try:
|
):
|
||||||
self.server.run()
|
return super().run()
|
||||||
finally:
|
|
||||||
if self.teardown is not None:
|
|
||||||
self.teardown()
|
|
||||||
except BaseException as e:
|
|
||||||
# Put the error on our queue so the parent gets it
|
|
||||||
self.errors.put((e, traceback.format_exc()))
|
|
||||||
|
|||||||
Reference in New Issue
Block a user