From 8427fb87f6f60633efce75b1c3562c371e465938 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sat, 29 May 2021 19:47:55 +0200 Subject: [PATCH] tenants: add tests Signed-off-by: Jens Langhammer --- authentik/tenants/api.py | 1 + authentik/tenants/tests.py | 36 +++++++++++++++++++ authentik/tenants/utils.py | 2 +- schema.yml | 3 ++ web/src/api/Config.ts | 4 ++- web/src/elements/sidebar/SidebarBrand.ts | 1 + web/src/pages/tenants/TenantForm.ts | 8 ++--- .../pages/user-settings/UserDetailsPage.ts | 1 - 8 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 authentik/tenants/tests.py diff --git a/authentik/tenants/api.py b/authentik/tenants/api.py index 90898cbed..74acbc57a 100644 --- a/authentik/tenants/api.py +++ b/authentik/tenants/api.py @@ -42,6 +42,7 @@ class TenantSerializer(ModelSerializer): class CurrentTenantSerializer(PassiveSerializer): """Partial tenant information for styling""" + matched_domain = CharField(source="domain") branding_title = CharField() branding_logo = CharField() ui_footer_links = ListField( diff --git a/authentik/tenants/tests.py b/authentik/tenants/tests.py new file mode 100644 index 000000000..a9b1be7df --- /dev/null +++ b/authentik/tenants/tests.py @@ -0,0 +1,36 @@ +"""Test tenants""" +from django.test import TestCase +from django.urls import reverse +from django.utils.encoding import force_str + +from authentik.lib.config import CONFIG +from authentik.tenants.models import Tenant + + +class TestTenants(TestCase): + """Test tenants""" + + def test_current_tenant(self): + """Test Current tenant API""" + self.assertJSONEqual( + force_str(self.client.get(reverse("authentik_api:tenant-current")).content), + { + "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg", + "branding_title": "authentik", + "matched_domain": "authentik-default", + "ui_footer_links": CONFIG.y("authentik.footer_links"), + }, + ) + + def test_fallback(self): + """Test fallback tenant""" + Tenant.objects.all().delete() + self.assertJSONEqual( + force_str(self.client.get(reverse("authentik_api:tenant-current")).content), + { + "branding_logo": "/static/dist/assets/icons/icon_left_brand.svg", + "branding_title": "authentik", + "matched_domain": "fallback", + "ui_footer_links": CONFIG.y("authentik.footer_links"), + }, + ) diff --git a/authentik/tenants/utils.py b/authentik/tenants/utils.py index 00367cab6..6be7548f1 100644 --- a/authentik/tenants/utils.py +++ b/authentik/tenants/utils.py @@ -17,7 +17,7 @@ def get_tenant_for_request(request: HttpRequest) -> Tenant: Q(domain__iendswith=request.get_host()) | _q_default ) if not db_tenants.exists(): - return Tenant() + return Tenant(domain="fallback") return db_tenants.first() diff --git a/schema.yml b/schema.yml index e3b308944..e583d1d34 100644 --- a/schema.yml +++ b/schema.yml @@ -16514,6 +16514,8 @@ components: type: object description: Partial tenant information for styling properties: + matched_domain: + type: string branding_title: type: string branding_logo: @@ -16533,6 +16535,7 @@ components: required: - branding_logo - branding_title + - matched_domain - ui_footer_links DenyStage: type: object diff --git a/web/src/api/Config.ts b/web/src/api/Config.ts index 5190778b7..bbb578aba 100644 --- a/web/src/api/Config.ts +++ b/web/src/api/Config.ts @@ -6,7 +6,9 @@ import { MessageMiddleware } from "../elements/messages/Middleware"; export class LoggingMiddleware implements Middleware { post(context: ResponseContext): Promise { - console.debug(`authentik/api: ${context.response.status} ${context.init.method} ${context.url}`); + tenant().then(tenant => { + console.debug(`authentik/api[${tenant.matchedDomain}]: ${context.response.status} ${context.init.method} ${context.url}`); + }); return Promise.resolve(context.response); } diff --git a/web/src/elements/sidebar/SidebarBrand.ts b/web/src/elements/sidebar/SidebarBrand.ts index a9392cf6c..2aeb132f6 100644 --- a/web/src/elements/sidebar/SidebarBrand.ts +++ b/web/src/elements/sidebar/SidebarBrand.ts @@ -19,6 +19,7 @@ export const DefaultTenant: CurrentTenant = { brandingLogo: " /static/dist/assets/icons/icon_left_brand.svg", brandingTitle: "authentik", uiFooterLinks: [], + matchedDomain: "", }; @customElement("ak-sidebar-brand") diff --git a/web/src/pages/tenants/TenantForm.ts b/web/src/pages/tenants/TenantForm.ts index b90513044..a6a094b1d 100644 --- a/web/src/pages/tenants/TenantForm.ts +++ b/web/src/pages/tenants/TenantForm.ts @@ -92,7 +92,7 @@ export class TenantForm extends ModelForm { designation: FlowsInstancesListDesignationEnum.Authentication, }).then(flows => { return flows.results.map(flow => { - let selected = this.instance?.flowAuthentication === flow.pk; + const selected = this.instance?.flowAuthentication === flow.pk; return html``; }); }), html``)} @@ -109,7 +109,7 @@ export class TenantForm extends ModelForm { designation: FlowsInstancesListDesignationEnum.Invalidation, }).then(flows => { return flows.results.map(flow => { - let selected = this.instance?.flowInvalidation === flow.pk; + const selected = this.instance?.flowInvalidation === flow.pk; return html``; }); }), html``)} @@ -126,7 +126,7 @@ export class TenantForm extends ModelForm { designation: FlowsInstancesListDesignationEnum.Recovery, }).then(flows => { return flows.results.map(flow => { - let selected = this.instance?.flowRecovery === flow.pk; + const selected = this.instance?.flowRecovery === flow.pk; return html``; }); }), html``)} @@ -143,7 +143,7 @@ export class TenantForm extends ModelForm { designation: FlowsInstancesListDesignationEnum.Recovery, }).then(flows => { return flows.results.map(flow => { - let selected = this.instance?.flowUnenrollment === flow.pk; + const selected = this.instance?.flowUnenrollment === flow.pk; return html``; }); }), html``)} diff --git a/web/src/pages/user-settings/UserDetailsPage.ts b/web/src/pages/user-settings/UserDetailsPage.ts index f729758ba..6a2049cd7 100644 --- a/web/src/pages/user-settings/UserDetailsPage.ts +++ b/web/src/pages/user-settings/UserDetailsPage.ts @@ -8,7 +8,6 @@ import PFForm from "@patternfly/patternfly/components/Form/form.css"; import PFFormControl from "@patternfly/patternfly/components/FormControl/form-control.css"; import { CoreApi, User } from "authentik-api"; import { me } from "../../api/Users"; -import { FlowURLManager } from "../../api/legacy"; import { ifDefined } from "lit-html/directives/if-defined"; import { DEFAULT_CONFIG, tenant } from "../../api/Config"; import "../../elements/forms/FormElement";