tenants: add tests

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-05-29 19:47:55 +02:00
parent e3578eb7ae
commit 8427fb87f6
8 changed files with 49 additions and 7 deletions

View File

@ -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(

View File

@ -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"),
},
)

View File

@ -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()

View File

@ -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

View File

@ -6,7 +6,9 @@ import { MessageMiddleware } from "../elements/messages/Middleware";
export class LoggingMiddleware implements Middleware {
post(context: ResponseContext): Promise<Response | void> {
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);
}

View File

@ -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")

View File

@ -92,7 +92,7 @@ export class TenantForm extends ModelForm<Tenant, string> {
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`<option value=${flow.pk} ?selected=${selected}>${flow.name} (${flow.slug})</option>`;
});
}), html`<option>${t`Loading...`}</option>`)}
@ -109,7 +109,7 @@ export class TenantForm extends ModelForm<Tenant, string> {
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`<option value=${flow.pk} ?selected=${selected}>${flow.name} (${flow.slug})</option>`;
});
}), html`<option>${t`Loading...`}</option>`)}
@ -126,7 +126,7 @@ export class TenantForm extends ModelForm<Tenant, string> {
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`<option value=${flow.pk} ?selected=${selected}>${flow.name} (${flow.slug})</option>`;
});
}), html`<option>${t`Loading...`}</option>`)}
@ -143,7 +143,7 @@ export class TenantForm extends ModelForm<Tenant, string> {
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`<option value=${flow.pk} ?selected=${selected}>${flow.name} (${flow.slug})</option>`;
});
}), html`<option>${t`Loading...`}</option>`)}

View File

@ -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";