78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
import {
|
|
findTenantForHost,
|
|
scoreTenantHost,
|
|
type TenantHostFields,
|
|
} from './tenant-host.util';
|
|
|
|
function fromDomain(
|
|
domain: string,
|
|
extra: Partial<TenantHostFields> = {},
|
|
): TenantHostFields {
|
|
return {
|
|
domain,
|
|
domains: [],
|
|
appUrl: `https://${domain}`,
|
|
adminUrl: null,
|
|
nodeServer: null,
|
|
...extra,
|
|
};
|
|
}
|
|
|
|
describe('findTenantForHost', () => {
|
|
it('hmac matches the tenant whose domain is the host', () => {
|
|
const host = 'shop.example.ir';
|
|
const mine = fromDomain(host);
|
|
const other = fromDomain('other.example.ir', {
|
|
adminUrl: `https://${host}/admin`,
|
|
});
|
|
|
|
expect(scoreTenantHost(other, host, 'hmac')).toBe(0);
|
|
expect(findTenantForHost([other, mine], host, 'hmac')?.domain).toBe(host);
|
|
});
|
|
|
|
it('hmac does not use another tenant adminUrl as identity', () => {
|
|
const host = 'panel.example.ir';
|
|
const other = fromDomain('app.other.ir', {
|
|
adminUrl: `https://${host}/admin`,
|
|
});
|
|
|
|
expect(findTenantForHost([other], host, 'hmac')).toBeUndefined();
|
|
});
|
|
|
|
it('full match uses primary domain when present', () => {
|
|
const host = 'shop.example.ir';
|
|
expect(findTenantForHost([fromDomain(host)], host)?.domain).toBe(host);
|
|
});
|
|
|
|
it('subdomain of the tenant domain beats a shared nodeServer', () => {
|
|
const root = 'shop.example.ir';
|
|
const api = `new.${root}`;
|
|
const mine = fromDomain(root, { nodeServer: `https://${api}/server` });
|
|
const roommate = fromDomain('roommate.ir', {
|
|
nodeServer: `https://${api}/server`,
|
|
});
|
|
|
|
expect(findTenantForHost([roommate, mine], api)?.domain).toBe(root);
|
|
expect(scoreTenantHost(mine, api)).toBe(75);
|
|
});
|
|
|
|
it('full match uses extra IP aliases like get_user', () => {
|
|
const tenant = fromDomain('k.montzar.ir', {
|
|
domains: ['192.168.70.191', '192.168.70.184', '192.168.70.158'],
|
|
});
|
|
|
|
expect(findTenantForHost([tenant], '192.168.70.191')?.domain).toBe(
|
|
'k.montzar.ir',
|
|
);
|
|
expect(scoreTenantHost(tenant, '192.168.70.191')).toBe(90);
|
|
});
|
|
|
|
it('does not pick a tenant when only a shared nodeServer matches', () => {
|
|
const api = 'box.example.ir';
|
|
const left = fromDomain('a.ir', { nodeServer: `https://${api}/server` });
|
|
const right = fromDomain('b.ir', { nodeServer: `https://${api}/server` });
|
|
|
|
expect(findTenantForHost([left, right], api)).toBeUndefined();
|
|
});
|
|
});
|