2023-03-30 02:46:25 -06:00
|
|
|
import { getKcContext } from "keycloakify/login/kcContext/getKcContext";
|
|
|
|
import type { ExtendKcContext } from "keycloakify/login/kcContext/getKcContextFromWindow";
|
|
|
|
import type { KcContext } from "keycloakify/login/kcContext";
|
2021-06-28 04:04:48 +02:00
|
|
|
import { same } from "evt/tools/inDepth";
|
|
|
|
import { assert } from "tsafe/assert";
|
2021-10-11 21:35:40 +02:00
|
|
|
import type { Equals } from "tsafe";
|
2024-05-20 15:48:51 +02:00
|
|
|
import {
|
|
|
|
kcContextMocks,
|
|
|
|
kcContextCommonMock
|
|
|
|
} from "keycloakify/login/kcContext/kcContextMocks";
|
2023-03-30 02:46:25 -06:00
|
|
|
import { deepClone } from "keycloakify/tools/deepClone";
|
|
|
|
import { expect, it, describe } from "vitest";
|
2021-06-28 04:04:48 +02:00
|
|
|
|
2023-03-30 02:46:25 -06:00
|
|
|
describe("getKcContext", () => {
|
2024-05-20 15:48:51 +02:00
|
|
|
const authorizedMailDomains = [
|
|
|
|
"example.com",
|
|
|
|
"another-example.com",
|
|
|
|
"*.yet-another-example.com",
|
|
|
|
"*.example.com",
|
|
|
|
"hello-world.com"
|
|
|
|
];
|
2021-10-11 21:35:40 +02:00
|
|
|
|
|
|
|
const displayName = "this is an overwritten common value";
|
|
|
|
|
|
|
|
const aNonStandardValue1 = "a non standard value 1";
|
|
|
|
const aNonStandardValue2 = "a non standard value 2";
|
|
|
|
|
2023-03-19 14:48:01 +01:00
|
|
|
type KcContextExtension =
|
2021-10-11 21:35:40 +02:00
|
|
|
| {
|
|
|
|
pageId: "register.ftl";
|
|
|
|
authorizedMailDomains: string[];
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
pageId: "info.ftl";
|
|
|
|
aNonStandardValue1: string;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
pageId: "my-extra-page-1.ftl";
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
pageId: "my-extra-page-2.ftl";
|
|
|
|
aNonStandardValue2: string;
|
|
|
|
};
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
const getKcContextProxy = (params: {
|
|
|
|
mockPageId: ExtendKcContext<KcContextExtension>["pageId"];
|
|
|
|
}) => {
|
2021-10-11 21:35:40 +02:00
|
|
|
const { mockPageId } = params;
|
|
|
|
|
2023-03-19 14:48:01 +01:00
|
|
|
const { kcContext } = getKcContext<KcContextExtension>({
|
2021-10-11 21:35:40 +02:00
|
|
|
mockPageId,
|
2024-05-20 15:48:51 +02:00
|
|
|
mockData: [
|
2021-10-11 21:35:40 +02:00
|
|
|
{
|
2024-05-20 15:48:51 +02:00
|
|
|
pageId: "login.ftl",
|
|
|
|
realm: { displayName }
|
2021-10-11 21:35:40 +02:00
|
|
|
},
|
|
|
|
{
|
2024-05-20 15:48:51 +02:00
|
|
|
pageId: "info.ftl",
|
2022-08-20 11:44:48 +07:00
|
|
|
aNonStandardValue1
|
2021-10-11 21:35:40 +02:00
|
|
|
},
|
|
|
|
{
|
2024-05-20 15:48:51 +02:00
|
|
|
pageId: "register.ftl",
|
2022-08-20 11:44:48 +07:00
|
|
|
authorizedMailDomains
|
2021-10-11 21:35:40 +02:00
|
|
|
},
|
|
|
|
{
|
2024-05-20 15:48:51 +02:00
|
|
|
pageId: "my-extra-page-2.ftl",
|
2022-08-20 11:44:48 +07:00
|
|
|
aNonStandardValue2
|
|
|
|
}
|
|
|
|
]
|
2021-10-11 21:35:40 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return { kcContext };
|
|
|
|
};
|
2023-03-30 02:46:25 -06:00
|
|
|
it("has proper API for login.ftl", () => {
|
2021-10-11 21:35:40 +02:00
|
|
|
const pageId = "login.ftl";
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
const { kcContext } = getKcContextProxy({ mockPageId: pageId });
|
2021-10-11 21:35:40 +02:00
|
|
|
|
|
|
|
assert(kcContext?.pageId === pageId);
|
|
|
|
|
2023-03-18 18:27:50 +01:00
|
|
|
assert<Equals<typeof kcContext, KcContext.Login>>();
|
2021-10-11 21:35:40 +02:00
|
|
|
|
2023-03-30 02:46:25 -06:00
|
|
|
expect(
|
2021-10-11 21:35:40 +02:00
|
|
|
same(
|
|
|
|
//NOTE: deepClone for printIfExists or other functions...
|
|
|
|
deepClone(kcContext),
|
|
|
|
(() => {
|
2024-05-20 15:48:51 +02:00
|
|
|
const mock = deepClone(
|
|
|
|
kcContextMocks.find(
|
|
|
|
({ pageId: pageId_i }) => pageId_i === pageId
|
|
|
|
)!
|
|
|
|
);
|
2021-10-11 21:35:40 +02:00
|
|
|
|
|
|
|
mock.realm.displayName = displayName;
|
|
|
|
|
|
|
|
return mock;
|
2022-08-20 11:44:48 +07:00
|
|
|
})()
|
|
|
|
)
|
2023-03-30 02:46:25 -06:00
|
|
|
).toBe(true);
|
|
|
|
});
|
2021-10-11 21:35:40 +02:00
|
|
|
|
2023-03-30 02:46:25 -06:00
|
|
|
it("has a proper API for info.ftl", () => {
|
2021-10-11 21:35:40 +02:00
|
|
|
const pageId = "info.ftl";
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
const { kcContext } = getKcContextProxy({ mockPageId: pageId });
|
2021-10-11 21:35:40 +02:00
|
|
|
|
|
|
|
assert(kcContext?.pageId === pageId);
|
|
|
|
|
|
|
|
//NOTE: I don't understand the need to add: pageId: typeof pageId; ...
|
|
|
|
assert<
|
|
|
|
Equals<
|
|
|
|
typeof kcContext,
|
2023-03-18 18:27:50 +01:00
|
|
|
KcContext.Info & {
|
2021-10-11 21:35:40 +02:00
|
|
|
pageId: typeof pageId;
|
|
|
|
aNonStandardValue1: string;
|
|
|
|
}
|
|
|
|
>
|
|
|
|
>();
|
|
|
|
|
2023-03-30 02:46:25 -06:00
|
|
|
expect(
|
2021-10-11 21:35:40 +02:00
|
|
|
same(
|
|
|
|
deepClone(kcContext),
|
|
|
|
(() => {
|
2024-05-20 15:48:51 +02:00
|
|
|
const mock = deepClone(
|
|
|
|
kcContextMocks.find(
|
|
|
|
({ pageId: pageId_i }) => pageId_i === pageId
|
|
|
|
)!
|
|
|
|
);
|
2021-10-11 21:35:40 +02:00
|
|
|
|
|
|
|
Object.assign(mock, { aNonStandardValue1 });
|
|
|
|
|
|
|
|
return mock;
|
2022-08-20 11:44:48 +07:00
|
|
|
})()
|
|
|
|
)
|
2023-03-30 02:46:25 -06:00
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
it("has a proper API for register.ftl", () => {
|
2021-10-11 21:35:40 +02:00
|
|
|
const pageId = "register.ftl";
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
const { kcContext } = getKcContextProxy({ mockPageId: pageId });
|
2021-10-11 21:35:40 +02:00
|
|
|
|
|
|
|
assert(kcContext?.pageId === pageId);
|
|
|
|
|
|
|
|
//NOTE: I don't understand the need to add: pageId: typeof pageId; ...
|
|
|
|
assert<
|
|
|
|
Equals<
|
|
|
|
typeof kcContext,
|
2023-03-18 18:27:50 +01:00
|
|
|
KcContext.Register & {
|
2021-10-11 21:35:40 +02:00
|
|
|
pageId: typeof pageId;
|
|
|
|
authorizedMailDomains: string[];
|
|
|
|
}
|
|
|
|
>
|
|
|
|
>();
|
|
|
|
|
2023-03-30 02:46:25 -06:00
|
|
|
expect(
|
2021-10-11 21:35:40 +02:00
|
|
|
same(
|
|
|
|
deepClone(kcContext),
|
|
|
|
(() => {
|
2024-05-20 15:48:51 +02:00
|
|
|
const mock = deepClone(
|
|
|
|
kcContextMocks.find(
|
|
|
|
({ pageId: pageId_i }) => pageId_i === pageId
|
|
|
|
)!
|
|
|
|
);
|
2021-10-11 21:35:40 +02:00
|
|
|
|
|
|
|
Object.assign(mock, { authorizedMailDomains });
|
|
|
|
|
|
|
|
return mock;
|
2022-08-20 11:44:48 +07:00
|
|
|
})()
|
|
|
|
)
|
2023-03-30 02:46:25 -06:00
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
it("has a proper API for my-extra-page-2.ftl", () => {
|
2021-10-11 21:35:40 +02:00
|
|
|
const pageId = "my-extra-page-2.ftl";
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
const { kcContext } = getKcContextProxy({ mockPageId: pageId });
|
2021-10-11 21:35:40 +02:00
|
|
|
|
|
|
|
assert(kcContext?.pageId === pageId);
|
|
|
|
|
|
|
|
assert<
|
|
|
|
Equals<
|
|
|
|
typeof kcContext,
|
2023-03-18 18:27:50 +01:00
|
|
|
KcContext.Common & {
|
2021-10-11 21:35:40 +02:00
|
|
|
pageId: typeof pageId;
|
|
|
|
aNonStandardValue2: string;
|
|
|
|
}
|
|
|
|
>
|
|
|
|
>();
|
|
|
|
|
|
|
|
kcContext.aNonStandardValue2;
|
|
|
|
|
2023-03-30 02:46:25 -06:00
|
|
|
expect(
|
2021-10-11 21:35:40 +02:00
|
|
|
same(
|
|
|
|
deepClone(kcContext),
|
|
|
|
(() => {
|
|
|
|
const mock = deepClone(kcContextCommonMock);
|
|
|
|
|
|
|
|
Object.assign(mock, { pageId, aNonStandardValue2 });
|
|
|
|
|
|
|
|
return mock;
|
2022-08-20 11:44:48 +07:00
|
|
|
})()
|
|
|
|
)
|
2023-03-30 02:46:25 -06:00
|
|
|
).toBe(true);
|
|
|
|
});
|
|
|
|
it("has a proper API for my-extra-page-1.ftl", () => {
|
2021-10-11 21:35:40 +02:00
|
|
|
const pageId = "my-extra-page-1.ftl";
|
|
|
|
|
|
|
|
console.log("We expect a warning here =>");
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
const { kcContext } = getKcContextProxy({ mockPageId: pageId });
|
2021-10-11 21:35:40 +02:00
|
|
|
|
|
|
|
assert(kcContext?.pageId === pageId);
|
|
|
|
|
2023-03-18 18:27:50 +01:00
|
|
|
assert<Equals<typeof kcContext, KcContext.Common & { pageId: typeof pageId }>>();
|
2021-06-28 04:04:48 +02:00
|
|
|
|
2023-03-30 02:46:25 -06:00
|
|
|
expect(
|
2021-10-11 21:35:40 +02:00
|
|
|
same(
|
|
|
|
deepClone(kcContext),
|
|
|
|
(() => {
|
|
|
|
const mock = deepClone(kcContextCommonMock);
|
2021-06-28 04:04:48 +02:00
|
|
|
|
2021-10-11 21:35:40 +02:00
|
|
|
Object.assign(mock, { pageId });
|
2021-06-28 04:04:48 +02:00
|
|
|
|
2021-10-11 21:35:40 +02:00
|
|
|
return mock;
|
2022-08-20 11:44:48 +07:00
|
|
|
})()
|
|
|
|
)
|
2023-03-30 02:46:25 -06:00
|
|
|
).toBe(true);
|
2021-10-11 21:35:40 +02:00
|
|
|
});
|
2023-03-30 02:46:25 -06:00
|
|
|
it("returns the proper mock for login.ftl", () => {
|
|
|
|
const pageId = "login.ftl";
|
2021-10-11 21:35:40 +02:00
|
|
|
|
2023-03-30 02:46:25 -06:00
|
|
|
const { kcContext } = getKcContext({
|
2024-05-20 15:48:51 +02:00
|
|
|
mockPageId: pageId
|
2023-03-30 02:46:25 -06:00
|
|
|
});
|
2021-06-28 04:04:48 +02:00
|
|
|
|
2023-03-30 02:46:25 -06:00
|
|
|
assert<Equals<typeof kcContext, KcContext | undefined>>();
|
2021-06-28 04:04:48 +02:00
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
assert(
|
|
|
|
same(
|
|
|
|
deepClone(kcContext),
|
|
|
|
deepClone(
|
|
|
|
kcContextMocks.find(({ pageId: pageId_i }) => pageId_i === pageId)!
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2023-03-30 02:46:25 -06:00
|
|
|
});
|
2023-04-16 02:09:26 +02:00
|
|
|
it("returns undefined when no mock is specified", () => {
|
2023-03-30 02:46:25 -06:00
|
|
|
const { kcContext } = getKcContext();
|
2021-07-03 02:39:39 +02:00
|
|
|
|
2023-03-30 02:46:25 -06:00
|
|
|
assert<Equals<typeof kcContext, KcContext | undefined>>();
|
2021-07-03 02:39:39 +02:00
|
|
|
|
2023-03-30 02:46:25 -06:00
|
|
|
assert(kcContext === undefined);
|
|
|
|
});
|
|
|
|
});
|