keycloak_theme/src/test/lib/getKcContext.ts

250 lines
4.8 KiB
TypeScript
Raw Normal View History

import { getKcContext } from "../../lib/getKcContext";
import type { KcContextBase } from "../../lib/getKcContext";
import type { ExtendsKcContextBase } from "../../lib/getKcContext/getKcContext";
import { same } from "evt/tools/inDepth";
import { assert } from "tsafe/assert";
2021-10-11 03:41:05 +02:00
import type { Equals } from "tsafe";
import { kcContextMocks, kcContextCommonMock } from "../../lib/getKcContext/kcContextMocks";
2021-07-03 02:39:39 +02:00
import { deepClone } from "../../lib/tools/deepClone";
2021-07-03 02:39:39 +02:00
{
2021-07-03 02:39:39 +02:00
const authorizedMailDomains = [
"example.com",
"another-example.com",
"*.yet-another-example.com",
"*.example.com",
"hello-world.com"
];
2021-07-03 02:39:39 +02:00
const displayName = "this is an overwritten common value";
2021-07-03 02:39:39 +02:00
const aNonStandardValue1 = "a non standard value 1";
const aNonStandardValue2 = "a non standard value 2";
2021-07-03 02:39:39 +02:00
type KcContextExtended = {
pageId: "register.ftl";
authorizedMailDomains: string[];
} | {
pageId: "info.ftl";
aNonStandardValue1: string;
} | {
pageId: "my-extra-page-1.ftl";
} | {
pageId: "my-extra-page-2.ftl";
aNonStandardValue2: string;
};
2021-07-03 02:39:39 +02:00
const getKcContextProxy = (
params: {
mockPageId: ExtendsKcContextBase<KcContextExtended>["pageId"];
}
) => {
2021-07-03 02:39:39 +02:00
const { mockPageId } = params;
2021-07-03 02:39:39 +02:00
const { kcContext } = getKcContext<KcContextExtended>({
mockPageId,
"mockData": [
{
"pageId": "login.ftl",
"realm": { displayName }
},
{
"pageId": "info.ftl",
aNonStandardValue1
},
{
"pageId": "register.ftl",
authorizedMailDomains
},
{
"pageId": "my-extra-page-2.ftl",
aNonStandardValue2
}
]
});
2021-07-03 02:39:39 +02:00
return { kcContext };
2021-07-03 02:39:39 +02:00
};
2021-07-03 02:39:39 +02:00
{
2021-07-03 02:39:39 +02:00
const pageId = "login.ftl";
2021-07-03 02:39:39 +02:00
const { kcContext } = getKcContextProxy({ "mockPageId": pageId });
2021-07-03 02:39:39 +02:00
assert(kcContext?.pageId === pageId);
2021-10-11 03:41:05 +02:00
assert<Equals<typeof kcContext, KcContextBase.Login>>();
2021-07-03 02:39:39 +02:00
assert(same(
//NOTE: deepClone for printIfExists or other functions...
deepClone(kcContext),
(() => {
2021-07-03 02:39:39 +02:00
const mock = deepClone(kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!);
2021-07-03 02:39:39 +02:00
mock.realm.displayName = displayName;
2021-07-03 02:39:39 +02:00
return mock;
2021-07-03 02:39:39 +02:00
})()
));
2021-07-03 02:39:39 +02:00
console.log(`PASS ${pageId}`);
2021-07-03 02:39:39 +02:00
}
2021-07-03 02:39:39 +02:00
{
const pageId = "info.ftl";
2021-07-03 02:39:39 +02:00
const { kcContext } = getKcContextProxy({ "mockPageId": pageId });
2021-07-03 02:39:39 +02:00
assert(kcContext?.pageId === pageId);
2021-07-03 02:39:39 +02:00
//NOTE: I don't understand the need to add: pageId: typeof pageId; ...
2021-10-11 03:41:05 +02:00
assert<Equals<typeof kcContext, KcContextBase.Info & { pageId: typeof pageId; aNonStandardValue1: string; }>>();
2021-07-03 02:39:39 +02:00
assert(same(
deepClone(kcContext),
(() => {
2021-07-03 02:39:39 +02:00
const mock = deepClone(kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!);
2021-07-03 02:39:39 +02:00
Object.assign(mock, { aNonStandardValue1 });
2021-07-03 02:39:39 +02:00
return mock;
2021-07-03 02:39:39 +02:00
})()
));
2021-07-03 02:39:39 +02:00
console.log(`PASS ${pageId}`);
2021-07-03 02:39:39 +02:00
}
2021-07-03 02:39:39 +02:00
{
const pageId = "register.ftl";
2021-07-03 02:39:39 +02:00
const { kcContext } = getKcContextProxy({ "mockPageId": pageId });
2021-07-03 02:39:39 +02:00
assert(kcContext?.pageId === pageId);
2021-07-03 02:39:39 +02:00
//NOTE: I don't understand the need to add: pageId: typeof pageId; ...
2021-10-11 03:41:05 +02:00
assert<Equals<typeof kcContext, KcContextBase.Register & { pageId: typeof pageId; authorizedMailDomains: string[]; }>>();
2021-07-03 02:39:39 +02:00
assert(same(
deepClone(kcContext),
(() => {
2021-07-03 02:39:39 +02:00
const mock = deepClone(kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!);
2021-07-03 02:39:39 +02:00
Object.assign(mock, { authorizedMailDomains });
2021-07-03 02:39:39 +02:00
return mock;
2021-07-03 02:39:39 +02:00
})()
));
2021-07-03 02:39:39 +02:00
console.log(`PASS ${pageId}`);
2021-07-03 02:39:39 +02:00
}
2021-07-03 02:39:39 +02:00
{
2021-07-03 02:39:39 +02:00
const pageId = "my-extra-page-2.ftl";
2021-07-03 02:39:39 +02:00
const { kcContext } = getKcContextProxy({ "mockPageId": pageId });
2021-07-03 02:39:39 +02:00
assert(kcContext?.pageId === pageId);
2021-10-11 03:41:05 +02:00
assert<Equals<typeof kcContext, KcContextBase.Common & { pageId: typeof pageId; aNonStandardValue2: string; }>>();
2021-07-03 02:39:39 +02:00
kcContext.aNonStandardValue2;
2021-07-03 02:39:39 +02:00
assert(same(
deepClone(kcContext),
(() => {
2021-07-03 02:39:39 +02:00
const mock = deepClone(kcContextCommonMock);
2021-07-03 02:39:39 +02:00
Object.assign(mock, { pageId, aNonStandardValue2 });
2021-07-03 02:39:39 +02:00
return mock;
2021-07-03 02:39:39 +02:00
})()
));
2021-07-03 02:39:39 +02:00
console.log(`PASS ${pageId}`);
2021-07-03 02:39:39 +02:00
}
2021-07-03 02:39:39 +02:00
{
2021-07-03 02:39:39 +02:00
const pageId = "my-extra-page-1.ftl";
2021-07-03 02:39:39 +02:00
console.log("We expect a warning here =>");
const { kcContext } = getKcContextProxy({ "mockPageId": pageId });
assert(kcContext?.pageId === pageId);
2021-10-11 03:41:05 +02:00
assert<Equals<typeof kcContext, KcContextBase.Common & { pageId: typeof pageId; }>>();
2021-07-03 02:39:39 +02:00
assert(same(
deepClone(kcContext),
(() => {
const mock = deepClone(kcContextCommonMock);
Object.assign(mock, { pageId });
return mock;
})()
));
console.log(`PASS ${pageId}`);
}
}
{
2021-07-03 02:39:39 +02:00
const pageId = "login.ftl";
2021-07-03 02:39:39 +02:00
const { kcContext } = getKcContext({
"mockPageId": pageId
});
2021-10-11 03:41:05 +02:00
assert<Equals<typeof kcContext, KcContextBase | undefined>>();
2021-07-03 02:39:39 +02:00
assert(same(
deepClone(kcContext),
deepClone(kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!)
));
console.log("PASS no extension");
}
2021-07-03 02:39:39 +02:00
{
2021-07-03 02:39:39 +02:00
const { kcContext } = getKcContext();
2021-10-11 03:41:05 +02:00
assert<Equals<typeof kcContext, KcContextBase | undefined>>();
2021-07-03 02:39:39 +02:00
assert(kcContext === undefined);
console.log("PASS no extension, no mock");
}