From b8fcb5e60d23de6b46ce6feed4ae7ecefacadb75 Mon Sep 17 00:00:00 2001 From: Marc 'risson' Schmitt Date: Tue, 21 Nov 2023 18:18:20 +0100 Subject: [PATCH] fix avatar tests Signed-off-by: Marc 'risson' Schmitt --- authentik/core/tests/test_users_avatars.py | 17 ++++++++++++----- authentik/tenants/utils.py | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/authentik/core/tests/test_users_avatars.py b/authentik/core/tests/test_users_avatars.py index cce239354..98381db0f 100644 --- a/authentik/core/tests/test_users_avatars.py +++ b/authentik/core/tests/test_users_avatars.py @@ -8,6 +8,7 @@ from rest_framework.test import APITestCase from authentik.core.models import User from authentik.core.tests.utils import create_test_admin_user from authentik.lib.config import CONFIG +from authentik.tenants.utils import get_current_tenant class TestUsersAvatars(APITestCase): @@ -17,18 +18,24 @@ class TestUsersAvatars(APITestCase): self.admin = create_test_admin_user() self.user = User.objects.create(username="test-user") + def set_avatar_mode(self, mode: str): + tenant = get_current_tenant() + tenant.avatars = mode + tenant.save() + @CONFIG.patch("avatars", "none") def test_avatars_none(self): """Test avatars none""" + self.set_avatar_mode("none") self.client.force_login(self.admin) response = self.client.get(reverse("authentik_api:user-me")) self.assertEqual(response.status_code, 200) body = loads(response.content.decode()) self.assertEqual(body["user"]["avatar"], "/static/dist/assets/images/user_default.png") - @CONFIG.patch("avatars", "gravatar") def test_avatars_gravatar(self): """Test avatars gravatar""" + self.set_avatar_mode("gravatar") self.admin.email = "static@t.goauthentik.io" self.admin.save() self.client.force_login(self.admin) @@ -45,27 +52,27 @@ class TestUsersAvatars(APITestCase): body = loads(response.content.decode()) self.assertIn("gravatar", body["user"]["avatar"]) - @CONFIG.patch("avatars", "initials") def test_avatars_initials(self): """Test avatars initials""" + self.set_avatar_mode("initials") self.client.force_login(self.admin) response = self.client.get(reverse("authentik_api:user-me")) self.assertEqual(response.status_code, 200) body = loads(response.content.decode()) self.assertIn("data:image/svg+xml;base64,", body["user"]["avatar"]) - @CONFIG.patch("avatars", "foo://%(username)s") def test_avatars_custom(self): """Test avatars custom""" + self.set_avatar_mode("foo://%(username)s") self.client.force_login(self.admin) response = self.client.get(reverse("authentik_api:user-me")) self.assertEqual(response.status_code, 200) body = loads(response.content.decode()) self.assertEqual(body["user"]["avatar"], f"foo://{self.admin.username}") - @CONFIG.patch("avatars", "attributes.foo.avatar") def test_avatars_attributes(self): """Test avatars attributes""" + self.set_avatar_mode("attributes.foo.avatar") self.admin.attributes = {"foo": {"avatar": "bar"}} self.admin.save() self.client.force_login(self.admin) @@ -74,9 +81,9 @@ class TestUsersAvatars(APITestCase): body = loads(response.content.decode()) self.assertEqual(body["user"]["avatar"], "bar") - @CONFIG.patch("avatars", "attributes.foo.avatar,initials") def test_avatars_fallback(self): """Test fallback""" + self.set_avatar_mode("attributes.foo.avatar,initials") self.client.force_login(self.admin) response = self.client.get(reverse("authentik_api:user-me")) self.assertEqual(response.status_code, 200) diff --git a/authentik/tenants/utils.py b/authentik/tenants/utils.py index 49e453f29..aabc2e931 100644 --- a/authentik/tenants/utils.py +++ b/authentik/tenants/utils.py @@ -6,4 +6,4 @@ from authentik.tenants.models import Tenant def get_current_tenant() -> Tenant: """Get tenant for current request""" - return connection.tenant + return Tenant.objects.get(schema_name=connection.schema_name)