2021-11-07 20:17:14 +01:00
|
|
|
import type { KcContextBase, Attribute } from "./KcContextBase";
|
2021-06-23 08:16:51 +02:00
|
|
|
import { kcContextMocks, kcContextCommonMock } from "./kcContextMocks";
|
2021-06-28 04:04:48 +02:00
|
|
|
import type { DeepPartial } from "../tools/DeepPartial";
|
|
|
|
import { deepAssign } from "../tools/deepAssign";
|
2021-11-07 20:17:14 +01:00
|
|
|
import { id } from "tsafe/id";
|
|
|
|
import { exclude } from "tsafe/exclude";
|
|
|
|
import { assert } from "tsafe/assert";
|
|
|
|
import type { ExtendsKcContextBase } from "./getKcContextFromWindow";
|
|
|
|
import { getKcContextFromWindow } from "./getKcContextFromWindow";
|
2022-07-29 23:10:35 +02:00
|
|
|
import { pathJoin } from "../../bin/tools/pathJoin";
|
2022-01-20 01:34:26 +01:00
|
|
|
import { pathBasename } from "../tools/pathBasename";
|
2022-07-30 00:51:11 +02:00
|
|
|
import { mockTestingResourcesCommonPath } from "../../bin/mockTestingResourcesPath";
|
2022-10-06 00:59:46 +02:00
|
|
|
import { symToStr } from "tsafe/symToStr";
|
2021-10-11 21:35:40 +02:00
|
|
|
|
2021-10-12 00:26:29 +02:00
|
|
|
export function getKcContext<KcContextExtended extends { pageId: string } = never>(params?: {
|
2021-10-11 21:35:40 +02:00
|
|
|
mockPageId?: ExtendsKcContextBase<KcContextExtended>["pageId"];
|
|
|
|
mockData?: readonly DeepPartial<ExtendsKcContextBase<KcContextExtended>>[];
|
|
|
|
}): { kcContext: ExtendsKcContextBase<KcContextExtended> | undefined } {
|
|
|
|
const { mockPageId, mockData } = params ?? {};
|
|
|
|
|
2022-10-06 00:36:46 +02:00
|
|
|
const realKcContext = getKcContextFromWindow<KcContextExtended>();
|
|
|
|
|
|
|
|
if (mockPageId !== undefined && realKcContext === undefined) {
|
2021-10-11 21:35:40 +02:00
|
|
|
//TODO maybe trow if no mock fo custom page
|
|
|
|
|
2022-10-06 00:59:46 +02:00
|
|
|
console.log(
|
|
|
|
[
|
|
|
|
`%cKeycloakify: ${symToStr({ mockPageId })} set to ${mockPageId}.`,
|
|
|
|
`If assets are missing make sure you have built your Keycloak theme at least once.`
|
|
|
|
].join(" "),
|
|
|
|
"background: red; color: yellow; font-size: medium"
|
|
|
|
);
|
|
|
|
|
2021-10-12 00:26:29 +02:00
|
|
|
const kcContextDefaultMock = kcContextMocks.find(({ pageId }) => pageId === mockPageId);
|
2021-10-11 21:35:40 +02:00
|
|
|
|
2021-10-12 00:26:29 +02:00
|
|
|
const partialKcContextCustomMock = mockData?.find(({ pageId }) => pageId === mockPageId);
|
2021-10-11 21:35:40 +02:00
|
|
|
|
2021-10-12 00:26:29 +02:00
|
|
|
if (kcContextDefaultMock === undefined && partialKcContextCustomMock === undefined) {
|
2021-10-11 21:35:40 +02:00
|
|
|
console.warn(
|
|
|
|
[
|
|
|
|
`WARNING: You declared the non build in page ${mockPageId} but you didn't `,
|
|
|
|
`provide mock data needed to debug the page outside of Keycloak as you are trying to do now.`,
|
2022-08-20 11:44:48 +07:00
|
|
|
`Please check the documentation of the getKcContext function`
|
|
|
|
].join("\n")
|
2021-10-11 21:35:40 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const kcContext: any = {};
|
|
|
|
|
|
|
|
deepAssign({
|
|
|
|
"target": kcContext,
|
2022-08-20 11:44:48 +07:00
|
|
|
"source": kcContextDefaultMock !== undefined ? kcContextDefaultMock : { "pageId": mockPageId, ...kcContextCommonMock }
|
2021-10-11 21:35:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (partialKcContextCustomMock !== undefined) {
|
|
|
|
deepAssign({
|
|
|
|
"target": kcContext,
|
2022-08-20 11:44:48 +07:00
|
|
|
"source": partialKcContextCustomMock
|
2021-10-11 21:35:40 +02:00
|
|
|
});
|
2021-11-07 20:17:14 +01:00
|
|
|
|
2022-09-09 12:55:57 +02:00
|
|
|
if (
|
|
|
|
partialKcContextCustomMock.pageId === "register-user-profile.ftl" ||
|
|
|
|
partialKcContextCustomMock.pageId === "update-user-profile.ftl" ||
|
|
|
|
partialKcContextCustomMock.pageId === "idp-review-user-profile.ftl"
|
|
|
|
) {
|
|
|
|
assert(
|
|
|
|
kcContextDefaultMock?.pageId === "register-user-profile.ftl" ||
|
|
|
|
kcContextDefaultMock?.pageId === "update-user-profile.ftl" ||
|
|
|
|
kcContextDefaultMock?.pageId === "idp-review-user-profile.ftl"
|
|
|
|
);
|
2021-11-07 20:17:14 +01:00
|
|
|
|
|
|
|
const { attributes } = kcContextDefaultMock.profile;
|
|
|
|
|
|
|
|
id<KcContextBase.RegisterUserProfile>(kcContext).profile.attributes = [];
|
|
|
|
id<KcContextBase.RegisterUserProfile>(kcContext).profile.attributesByName = {};
|
|
|
|
|
|
|
|
const partialAttributes = [
|
2022-08-20 11:44:48 +07:00
|
|
|
...((partialKcContextCustomMock as DeepPartial<KcContextBase.RegisterUserProfile>).profile?.attributes ?? [])
|
2021-11-07 20:17:14 +01:00
|
|
|
].filter(exclude(undefined));
|
|
|
|
|
|
|
|
attributes.forEach(attribute => {
|
|
|
|
const partialAttribute = partialAttributes.find(({ name }) => name === attribute.name);
|
|
|
|
|
|
|
|
const augmentedAttribute: Attribute = {} as any;
|
|
|
|
|
|
|
|
deepAssign({
|
|
|
|
"target": augmentedAttribute,
|
2022-08-20 11:44:48 +07:00
|
|
|
"source": attribute
|
2021-11-07 20:17:14 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (partialAttribute !== undefined) {
|
|
|
|
partialAttributes.splice(partialAttributes.indexOf(partialAttribute), 1);
|
|
|
|
|
|
|
|
deepAssign({
|
|
|
|
"target": augmentedAttribute,
|
2022-08-20 11:44:48 +07:00
|
|
|
"source": partialAttribute
|
2021-11-07 20:17:14 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
id<KcContextBase.RegisterUserProfile>(kcContext).profile.attributes.push(augmentedAttribute);
|
|
|
|
id<KcContextBase.RegisterUserProfile>(kcContext).profile.attributesByName[augmentedAttribute.name] = augmentedAttribute;
|
|
|
|
});
|
|
|
|
|
2022-09-04 23:19:33 +02:00
|
|
|
partialAttributes
|
|
|
|
.map(partialAttribute => ({ "validators": {}, ...partialAttribute }))
|
|
|
|
.forEach(partialAttribute => {
|
|
|
|
const { name } = partialAttribute;
|
2021-11-07 20:17:14 +01:00
|
|
|
|
2022-09-04 23:19:33 +02:00
|
|
|
assert(name !== undefined, "If you define a mock attribute it must have at least a name");
|
2021-11-07 20:17:14 +01:00
|
|
|
|
2022-09-04 23:19:33 +02:00
|
|
|
id<KcContextBase.RegisterUserProfile>(kcContext).profile.attributes.push(partialAttribute as any);
|
|
|
|
id<KcContextBase.RegisterUserProfile>(kcContext).profile.attributesByName[name] = partialAttribute as any;
|
|
|
|
});
|
2021-11-07 20:17:14 +01:00
|
|
|
}
|
2021-10-11 21:35:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return { kcContext };
|
|
|
|
}
|
|
|
|
|
2022-10-06 00:36:46 +02:00
|
|
|
if (realKcContext === undefined) {
|
|
|
|
return { "kcContext": undefined };
|
|
|
|
}
|
2022-01-20 01:34:26 +01:00
|
|
|
|
2022-10-06 00:36:46 +02:00
|
|
|
{
|
|
|
|
const { url } = realKcContext;
|
2022-01-20 01:34:26 +01:00
|
|
|
|
2022-07-30 00:51:11 +02:00
|
|
|
url.resourcesCommonPath = pathJoin(url.resourcesPath, pathBasename(mockTestingResourcesCommonPath));
|
2022-01-20 01:34:26 +01:00
|
|
|
}
|
|
|
|
|
2022-10-06 00:36:46 +02:00
|
|
|
return { "kcContext": realKcContext };
|
2021-06-23 08:16:51 +02:00
|
|
|
}
|