add settings tests

Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
This commit is contained in:
Marc 'risson' Schmitt 2023-12-07 09:01:11 +01:00
parent 35482f48d4
commit b151e27689
No known key found for this signature in database
GPG Key ID: 9C3FA22FABF1AA8D
5 changed files with 74 additions and 60 deletions

View File

@ -97,15 +97,9 @@ class DomainViewSet(ModelViewSet):
class SettingsSerializer(ModelSerializer): class SettingsSerializer(ModelSerializer):
"""Settings Serializer""" """Settings Serializer"""
name = ReadOnlyField()
domains = DomainSerializer(read_only=True, many=True)
class Meta: class Meta:
model = Tenant model = Tenant
fields = [ fields = [
"tenant_uuid",
"name",
"domains",
"avatars", "avatars",
"default_user_change_name", "default_user_change_name",
"default_user_change_email", "default_user_change_email",
@ -125,6 +119,6 @@ class SettingsView(RetrieveUpdateAPIView):
filter_backends = [] filter_backends = []
def get_object(self): def get_object(self):
obj = get_tenant(self.request) obj = self.request.tenant
self.check_object_permissions(self.request, obj) self.check_object_permissions(self.request, obj)
return obj return obj

View File

@ -26,7 +26,7 @@ class TestDomainAPI(TenantAPITestCase):
@CONFIG.patch("tenants.enabled", True) @CONFIG.patch("tenants.enabled", True)
@CONFIG.patch("tenants.api_key", TENANTS_API_KEY) @CONFIG.patch("tenants.api_key", TENANTS_API_KEY)
def test_domain(self): def test_domain(self):
"""Test domain creation""" """Test domain"""
response = self.client.post( response = self.client.post(
reverse("authentik_api:domain-list"), reverse("authentik_api:domain-list"),
headers=HEADERS, headers=HEADERS,

View File

@ -4,6 +4,7 @@ from json import loads
from django.urls import reverse from django.urls import reverse
from django_tenants.utils import get_public_schema_name from django_tenants.utils import get_public_schema_name
from authentik.core.tests.utils import create_test_admin_user
from authentik.lib.config import CONFIG from authentik.lib.config import CONFIG
from authentik.lib.generators import generate_id from authentik.lib.generators import generate_id
from authentik.tenants.models import Domain, Tenant from authentik.tenants.models import Domain, Tenant
@ -13,38 +14,73 @@ TENANTS_API_KEY = generate_id()
HEADERS = {"Authorization": f"Bearer {TENANTS_API_KEY}"} HEADERS = {"Authorization": f"Bearer {TENANTS_API_KEY}"}
# class TestSettingsAPI(TenantAPITestCase): class TestSettingsAPI(TenantAPITestCase):
# def setUp(self): def setUp(self):
# super().setUp() super().setUp()
# self.tenant_1 = Tenant.objects.get(schema_name=get_public_schema_name()) self.tenant_1 = Tenant.objects.get(schema_name=get_public_schema_name())
# self.tenant_2 = Tenant.objects.create( with self.tenant_1:
# name=generate_id(), schema_name="t_" + generate_id().lower() self.admin_1 = create_test_admin_user()
# ) self.tenant_2 = Tenant.objects.create(
# name=generate_id(), schema_name="t_" + generate_id().lower()
# @CONFIG.patch("outposts.disable_embedded_outpost", True) )
# @CONFIG.patch("tenants.enabled", True) Domain.objects.create(tenant=self.tenant_2, domain="tenant_2.testserver")
# @CONFIG.patch("tenants.api_key", TENANTS_API_KEY) with self.tenant_2:
# def test_domain(self): self.admin_2 = create_test_admin_user()
# """Test domain creation"""
# response = self.client.post( def test_settings(self):
# reverse("authentik_api:domain-list"), """Test settings API"""
# headers=HEADERS, # We need those context managers here because the test client doesn't put itself
# data={"tenant": self.tenant.pk, "domain": "test.domain"}, # in the tenant context as a real request would.
# ) with self.tenant_1:
# self.assertEqual(response.status_code, 201) self.client.force_login(self.admin_1)
# body = loads(response.content.decode()) response = self.client.patch(
# self.assertEqual(self.tenant.domains.get(domain="test.domain").pk, body["id"]) reverse("authentik_api:tenant_settings"),
# self.assertEqual(self.tenant.domains.get(domain="test.domain").is_primary, True) data={
# "avatars": "tenant_1_mode",
# response = self.client.post( },
# reverse("authentik_api:domain-list"), )
# headers=HEADERS, self.assertEqual(response.status_code, 200)
# data={"tenant": self.tenant.pk, "domain": "newprimary.domain", "is_primary": True}, self.tenant_1.refresh_from_db()
# ) self.assertEqual(self.tenant_1.avatars, "tenant_1_mode")
# self.assertEqual(response.status_code, 201) with self.tenant_1:
# self.assertEqual( self.client.logout()
# Domain.objects.get(tenant=self.tenant, domain="newprimary.domain").is_primary, True
# ) # with self.tenant_2:
# self.assertEqual( # self.client.force_login(self.admin_2)
# Domain.objects.get(tenant=self.tenant, domain="test.domain").is_primary, False # response = self.client.patch(
# ) # reverse("authentik_api:tenant_settings"),
# data={
# "avatars": "tenant_2_mode",
# },
# headers={
# "Host": "tenant_2.testserver",
# },
# )
# self.assertEqual(response.status_code, 200)
# self.tenant_2.refresh_from_db()
# self.assertEqual(self.tenant_2.avatars, "tenant_2_mode")
# with self.tenant_2:
# self.client.logout()
# with self.tenant_1:
# self.client.force_login(self.admin_1)
# response_1 = self.client.get(
# reverse("authentik_api:tenant_settings"),
# )
# self.assertEqual(response_1.status_code, 200)
# body_1 = loads(response_1.content.decode())
# with self.tenant_1:
# self.client.logout()
# with self.tenant_2:
# self.client.force_login(self.admin_2)
# response_2 = self.client.get(
# reverse("authentik_api:tenant_settings"),
# headers={
# "host": "tenant_2.testserver",
# },
# )
# self.assertEqual(response_2.status_code, 200)
# body_2 = loads(response_2.content.decode())
# with self.tenant_2:
# self.client.logout()
# self.assertNotEqual(body_1["avatars"], body_2["avatars"])

View File

@ -24,7 +24,7 @@ class TenantAPITestCase(APITransactionTestCase):
schemas = cursor.fetchall() schemas = cursor.fetchall()
for row in schemas: for row in schemas:
schema = row[0] schema = row[0]
cursor.execute(f"DROP SCHEMA {schema}") cursor.execute(f"DROP SCHEMA {schema} CASCADE")
def setUp(self): def setUp(self):
call_command("migrate_schemas", schema="template", tenant=True) call_command("migrate_schemas", schema="template", tenant=True)

View File

@ -41528,18 +41528,6 @@ components:
type: object type: object
description: Settings Serializer description: Settings Serializer
properties: properties:
tenant_uuid:
type: string
format: uuid
readOnly: true
name:
type: string
readOnly: true
domains:
type: array
items:
$ref: '#/components/schemas/Domain'
readOnly: true
avatars: avatars:
type: string type: string
description: Configure how authentik should show avatars for users. description: Configure how authentik should show avatars for users.
@ -41564,10 +41552,6 @@ components:
additionalProperties: {} additionalProperties: {}
description: The option configures the footer links on the flow executor description: The option configures the footer links on the flow executor
pages. pages.
required:
- domains
- name
- tenant_uuid
SettingsRequest: SettingsRequest:
type: object type: object
description: Settings Serializer description: Settings Serializer