fix avatar tests

Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
This commit is contained in:
Marc 'risson' Schmitt 2023-11-21 18:18:20 +01:00
parent d492c48657
commit b8fcb5e60d
No known key found for this signature in database
GPG Key ID: 9C3FA22FABF1AA8D
2 changed files with 13 additions and 6 deletions

View File

@ -8,6 +8,7 @@ from rest_framework.test import APITestCase
from authentik.core.models import User from authentik.core.models import User
from authentik.core.tests.utils import create_test_admin_user from authentik.core.tests.utils import create_test_admin_user
from authentik.lib.config import CONFIG from authentik.lib.config import CONFIG
from authentik.tenants.utils import get_current_tenant
class TestUsersAvatars(APITestCase): class TestUsersAvatars(APITestCase):
@ -17,18 +18,24 @@ class TestUsersAvatars(APITestCase):
self.admin = create_test_admin_user() self.admin = create_test_admin_user()
self.user = User.objects.create(username="test-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") @CONFIG.patch("avatars", "none")
def test_avatars_none(self): def test_avatars_none(self):
"""Test avatars none""" """Test avatars none"""
self.set_avatar_mode("none")
self.client.force_login(self.admin) self.client.force_login(self.admin)
response = self.client.get(reverse("authentik_api:user-me")) response = self.client.get(reverse("authentik_api:user-me"))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
body = loads(response.content.decode()) body = loads(response.content.decode())
self.assertEqual(body["user"]["avatar"], "/static/dist/assets/images/user_default.png") self.assertEqual(body["user"]["avatar"], "/static/dist/assets/images/user_default.png")
@CONFIG.patch("avatars", "gravatar")
def test_avatars_gravatar(self): def test_avatars_gravatar(self):
"""Test avatars gravatar""" """Test avatars gravatar"""
self.set_avatar_mode("gravatar")
self.admin.email = "static@t.goauthentik.io" self.admin.email = "static@t.goauthentik.io"
self.admin.save() self.admin.save()
self.client.force_login(self.admin) self.client.force_login(self.admin)
@ -45,27 +52,27 @@ class TestUsersAvatars(APITestCase):
body = loads(response.content.decode()) body = loads(response.content.decode())
self.assertIn("gravatar", body["user"]["avatar"]) self.assertIn("gravatar", body["user"]["avatar"])
@CONFIG.patch("avatars", "initials")
def test_avatars_initials(self): def test_avatars_initials(self):
"""Test avatars initials""" """Test avatars initials"""
self.set_avatar_mode("initials")
self.client.force_login(self.admin) self.client.force_login(self.admin)
response = self.client.get(reverse("authentik_api:user-me")) response = self.client.get(reverse("authentik_api:user-me"))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
body = loads(response.content.decode()) body = loads(response.content.decode())
self.assertIn("data:image/svg+xml;base64,", body["user"]["avatar"]) self.assertIn("data:image/svg+xml;base64,", body["user"]["avatar"])
@CONFIG.patch("avatars", "foo://%(username)s")
def test_avatars_custom(self): def test_avatars_custom(self):
"""Test avatars custom""" """Test avatars custom"""
self.set_avatar_mode("foo://%(username)s")
self.client.force_login(self.admin) self.client.force_login(self.admin)
response = self.client.get(reverse("authentik_api:user-me")) response = self.client.get(reverse("authentik_api:user-me"))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
body = loads(response.content.decode()) body = loads(response.content.decode())
self.assertEqual(body["user"]["avatar"], f"foo://{self.admin.username}") self.assertEqual(body["user"]["avatar"], f"foo://{self.admin.username}")
@CONFIG.patch("avatars", "attributes.foo.avatar")
def test_avatars_attributes(self): def test_avatars_attributes(self):
"""Test avatars attributes""" """Test avatars attributes"""
self.set_avatar_mode("attributes.foo.avatar")
self.admin.attributes = {"foo": {"avatar": "bar"}} self.admin.attributes = {"foo": {"avatar": "bar"}}
self.admin.save() self.admin.save()
self.client.force_login(self.admin) self.client.force_login(self.admin)
@ -74,9 +81,9 @@ class TestUsersAvatars(APITestCase):
body = loads(response.content.decode()) body = loads(response.content.decode())
self.assertEqual(body["user"]["avatar"], "bar") self.assertEqual(body["user"]["avatar"], "bar")
@CONFIG.patch("avatars", "attributes.foo.avatar,initials")
def test_avatars_fallback(self): def test_avatars_fallback(self):
"""Test fallback""" """Test fallback"""
self.set_avatar_mode("attributes.foo.avatar,initials")
self.client.force_login(self.admin) self.client.force_login(self.admin)
response = self.client.get(reverse("authentik_api:user-me")) response = self.client.get(reverse("authentik_api:user-me"))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)

View File

@ -6,4 +6,4 @@ from authentik.tenants.models import Tenant
def get_current_tenant() -> Tenant: def get_current_tenant() -> Tenant:
"""Get tenant for current request""" """Get tenant for current request"""
return connection.tenant return Tenant.objects.get(schema_name=connection.schema_name)