From 0a15cf7452efa518837e60244715886b570dc4ab Mon Sep 17 00:00:00 2001 From: Marc 'risson' Schmitt Date: Fri, 12 Jan 2024 03:54:33 +0100 Subject: [PATCH] fix tenants tests Signed-off-by: Marc 'risson' Schmitt --- authentik/tenants/api.py | 6 ++++++ authentik/tenants/tests/test_recovery.py | 4 ++-- authentik/tenants/tests/test_settings.py | 8 ++++++-- authentik/tenants/tests/utils.py | 4 +++- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/authentik/tenants/api.py b/authentik/tenants/api.py index 8bab2eb92..53d681426 100644 --- a/authentik/tenants/api.py +++ b/authentik/tenants/api.py @@ -5,6 +5,7 @@ from hmac import compare_digest from django.http import HttpResponseNotFound from django.http.request import urljoin from django.utils.timezone import now +from django_tenants.utils import get_public_schema_name from drf_spectacular.utils import OpenApiResponse, extend_schema from rest_framework import permissions from rest_framework.authentication import get_authorization_header @@ -217,3 +218,8 @@ class SettingsView(RetrieveUpdateAPIView): obj = self.request.tenant self.check_object_permissions(self.request, obj) return obj + + def perform_update(self, serializer): + # We need to be in the public schema to actually modify a tenant + with Tenant.objects.get(schema_name=get_public_schema_name()): + super().perform_update(serializer) diff --git a/authentik/tenants/tests/test_recovery.py b/authentik/tenants/tests/test_recovery.py index fb9422812..572753b89 100644 --- a/authentik/tenants/tests/test_recovery.py +++ b/authentik/tenants/tests/test_recovery.py @@ -14,8 +14,8 @@ TENANTS_API_KEY = generate_id() HEADERS = {"Authorization": f"Bearer {TENANTS_API_KEY}"} -class TestAPI(TenantAPITestCase): - """Test api view""" +class TestRecovery(TenantAPITestCase): + """Test recovery endpoints""" def setUp(self): super().setUp() diff --git a/authentik/tenants/tests/test_settings.py b/authentik/tenants/tests/test_settings.py index fb0e5fc55..32a4bbf7f 100644 --- a/authentik/tenants/tests/test_settings.py +++ b/authentik/tenants/tests/test_settings.py @@ -1,7 +1,6 @@ """Test Settings API""" from django.urls import reverse -from django_tenants.utils import get_public_schema_name from authentik.core.tests.utils import create_test_admin_user from authentik.lib.generators import generate_id @@ -13,9 +12,13 @@ HEADERS = {"Authorization": f"Bearer {TENANTS_API_KEY}"} class TestSettingsAPI(TenantAPITestCase): + """Test settings API""" def setUp(self): super().setUp() - self.tenant_1 = Tenant.objects.get(schema_name=get_public_schema_name()) + self.tenant_1 = Tenant.objects.create( + name=generate_id(), schema_name="t_" + generate_id().lower() + ) + Domain.objects.create(tenant=self.tenant_1, domain="tenant1.testserver") with self.tenant_1: self.admin_1 = create_test_admin_user() self.tenant_2 = Tenant.objects.create( @@ -37,6 +40,7 @@ class TestSettingsAPI(TenantAPITestCase): data={ "avatars": "tenant_1_mode", }, + HTTP_HOST="tenant1.testserver", ) self.assertEqual(response.status_code, 200) with self.tenant_1: diff --git a/authentik/tenants/tests/utils.py b/authentik/tenants/tests/utils.py index a92635ac0..655c7605d 100644 --- a/authentik/tenants/tests/utils.py +++ b/authentik/tenants/tests/utils.py @@ -1,13 +1,14 @@ from django.core.management import call_command from django.db import connection, connections +from django_tenants.utils import get_public_schema_name from rest_framework.test import APITransactionTestCase class TenantAPITestCase(APITransactionTestCase): # Overridden to also remove additional schemas we may have created def _fixture_teardown(self): - super()._fixture_teardown() for db_name in self._databases_names(include_mirrors=False): + connections[db_name].set_schema_to_public() with connections[db_name].cursor() as cursor: cursor.execute( "SELECT nspname FROM pg_catalog.pg_namespace WHERE nspname ~ 't_.*'" @@ -16,6 +17,7 @@ class TenantAPITestCase(APITransactionTestCase): for row in schemas: schema = row[0] cursor.execute(f"DROP SCHEMA {schema} CASCADE") + super()._fixture_teardown() def setUp(self): call_command("migrate_schemas", schema="template", tenant=True)