add tests for settings api

Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
This commit is contained in:
Marc 'risson' Schmitt 2024-01-11 20:04:04 +01:00
parent f5be985807
commit 6e07029f1a
No known key found for this signature in database
GPG Key ID: 9C3FA22FABF1AA8D
1 changed files with 20 additions and 41 deletions

View File

@ -1,4 +1,5 @@
"""Test Settings API""" """Test Settings API"""
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
@ -21,12 +22,13 @@ class TestSettingsAPI(TenantAPITestCase):
self.tenant_2 = Tenant.objects.create( self.tenant_2 = Tenant.objects.create(
name=generate_id(), schema_name="t_" + generate_id().lower() name=generate_id(), schema_name="t_" + generate_id().lower()
) )
Domain.objects.create(tenant=self.tenant_2, domain="tenant_2.testserver") Domain.objects.create(tenant=self.tenant_2, domain="tenant2.testserver")
with self.tenant_2: with self.tenant_2:
self.admin_2 = create_test_admin_user() self.admin_2 = create_test_admin_user()
def test_settings(self): def test_settings(self):
"""Test settings API""" """Test settings API"""
# First edit settings to different values in two different tenants
# We need those context managers here because the test client doesn't put itself # We need those context managers here because the test client doesn't put itself
# in the tenant context as a real request would. # in the tenant context as a real request would.
with self.tenant_1: with self.tenant_1:
@ -38,47 +40,24 @@ class TestSettingsAPI(TenantAPITestCase):
}, },
) )
self.assertEqual(response.status_code, 200) 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: with self.tenant_1:
self.client.logout() self.client.logout()
# with self.tenant_2: with self.tenant_2:
# self.client.force_login(self.admin_2) self.client.force_login(self.admin_2)
# response = self.client.patch( response = self.client.patch(
# reverse("authentik_api:tenant_settings"), reverse("authentik_api:tenant_settings"),
# data={ data={
# "avatars": "tenant_2_mode", "avatars": "tenant_2_mode",
# }, },
# headers={ HTTP_HOST="tenant2.testserver",
# "Host": "tenant_2.testserver", )
# }, self.assertEqual(response.status_code, 200)
# ) with self.tenant_2:
# self.assertEqual(response.status_code, 200) self.client.logout()
# 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: # Assert that the settings have changed and are different
# self.client.force_login(self.admin_1) self.tenant_1.refresh_from_db()
# response_1 = self.client.get( self.tenant_2.refresh_from_db()
# reverse("authentik_api:tenant_settings"), self.assertEqual(self.tenant_1.avatars, "tenant_1_mode")
# ) self.assertEqual(self.tenant_2.avatars, "tenant_2_mode")
# 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"])