add settings tests
Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
This commit is contained in:
parent
35482f48d4
commit
b151e27689
|
@ -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
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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()
|
||||||
|
)
|
||||||
|
Domain.objects.create(tenant=self.tenant_2, domain="tenant_2.testserver")
|
||||||
|
with self.tenant_2:
|
||||||
|
self.admin_2 = create_test_admin_user()
|
||||||
|
|
||||||
|
def test_settings(self):
|
||||||
|
"""Test settings API"""
|
||||||
|
# We need those context managers here because the test client doesn't put itself
|
||||||
|
# in the tenant context as a real request would.
|
||||||
|
with self.tenant_1:
|
||||||
|
self.client.force_login(self.admin_1)
|
||||||
|
response = self.client.patch(
|
||||||
|
reverse("authentik_api:tenant_settings"),
|
||||||
|
data={
|
||||||
|
"avatars": "tenant_1_mode",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.tenant_1.refresh_from_db()
|
||||||
|
self.assertEqual(self.tenant_1.avatars, "tenant_1_mode")
|
||||||
|
with self.tenant_1:
|
||||||
|
self.client.logout()
|
||||||
|
|
||||||
|
# with self.tenant_2:
|
||||||
|
# self.client.force_login(self.admin_2)
|
||||||
|
# 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)
|
||||||
# @CONFIG.patch("outposts.disable_embedded_outpost", True)
|
# self.tenant_2.refresh_from_db()
|
||||||
# @CONFIG.patch("tenants.enabled", True)
|
# self.assertEqual(self.tenant_2.avatars, "tenant_2_mode")
|
||||||
# @CONFIG.patch("tenants.api_key", TENANTS_API_KEY)
|
# with self.tenant_2:
|
||||||
# def test_domain(self):
|
# self.client.logout()
|
||||||
# """Test domain creation"""
|
|
||||||
# response = self.client.post(
|
# with self.tenant_1:
|
||||||
# reverse("authentik_api:domain-list"),
|
# self.client.force_login(self.admin_1)
|
||||||
# headers=HEADERS,
|
# response_1 = self.client.get(
|
||||||
# data={"tenant": self.tenant.pk, "domain": "test.domain"},
|
# reverse("authentik_api:tenant_settings"),
|
||||||
# )
|
# )
|
||||||
# self.assertEqual(response.status_code, 201)
|
# self.assertEqual(response_1.status_code, 200)
|
||||||
# body = loads(response.content.decode())
|
# body_1 = loads(response_1.content.decode())
|
||||||
# self.assertEqual(self.tenant.domains.get(domain="test.domain").pk, body["id"])
|
# with self.tenant_1:
|
||||||
# self.assertEqual(self.tenant.domains.get(domain="test.domain").is_primary, True)
|
# self.client.logout()
|
||||||
#
|
# with self.tenant_2:
|
||||||
# response = self.client.post(
|
# self.client.force_login(self.admin_2)
|
||||||
# reverse("authentik_api:domain-list"),
|
# response_2 = self.client.get(
|
||||||
# headers=HEADERS,
|
# reverse("authentik_api:tenant_settings"),
|
||||||
# data={"tenant": self.tenant.pk, "domain": "newprimary.domain", "is_primary": True},
|
# headers={
|
||||||
# )
|
# "host": "tenant_2.testserver",
|
||||||
# self.assertEqual(response.status_code, 201)
|
# },
|
||||||
# self.assertEqual(
|
|
||||||
# Domain.objects.get(tenant=self.tenant, domain="newprimary.domain").is_primary, True
|
|
||||||
# )
|
|
||||||
# self.assertEqual(
|
|
||||||
# Domain.objects.get(tenant=self.tenant, domain="test.domain").is_primary, False
|
|
||||||
# )
|
# )
|
||||||
|
# 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"])
|
||||||
|
|
|
@ -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)
|
||||||
|
|
16
schema.yml
16
schema.yml
|
@ -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
|
||||||
|
|
Reference in New Issue