1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | import "reflect-metadata";
|
23 | import { mockStorageUtility } from "@inrupt/solid-client-authn-core";
|
24 | import { Response as NodeResponse } from "node-fetch";
|
25 | import IssuerConfigFetcher from "../../../src/login/oidc/IssuerConfigFetcher";
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | describe("IssuerConfigFetcher", () => {
|
31 | const defaultMocks = {
|
32 | storageUtility: mockStorageUtility({}),
|
33 | };
|
34 |
|
35 | function getIssuerConfigFetcher(
|
36 | mocks: Partial<typeof defaultMocks> = defaultMocks
|
37 | ): IssuerConfigFetcher {
|
38 | return new IssuerConfigFetcher(
|
39 | mocks.storageUtility ?? defaultMocks.storageUtility
|
40 | );
|
41 | }
|
42 |
|
43 | it("should return a config based on the fetched config if none was stored in the storage", async () => {
|
44 | const fetchResponse = (new NodeResponse(
|
45 | JSON.stringify({
|
46 | issuer: "https://example.com",
|
47 |
|
48 | claim_types_supported: "oidc",
|
49 | bleepBloop: "Meep Moop",
|
50 | })
|
51 | ) as unknown) as Response;
|
52 | const configFetcher = getIssuerConfigFetcher({
|
53 | storageUtility: mockStorageUtility({}),
|
54 | });
|
55 | const mockFetch = jest.fn().mockResolvedValueOnce(fetchResponse);
|
56 | window.fetch = mockFetch;
|
57 | const fetchedConfig = await configFetcher.fetchConfig(
|
58 | "https://arbitrary.url"
|
59 | );
|
60 | expect(fetchedConfig.issuer.startsWith("https:")).toBeTruthy();
|
61 | expect(fetchedConfig.issuer).toBe("https://example.com");
|
62 |
|
63 | expect((fetchedConfig as any).claim_types_supported).toBeUndefined();
|
64 | expect(fetchedConfig.claimTypesSupported).toBe("oidc");
|
65 |
|
66 | expect((fetchedConfig as any).bleepBloop).toBeUndefined();
|
67 | });
|
68 |
|
69 | it("should throw an error if the fetched config could not be converted to JSON", async () => {
|
70 | const mockFetch = (): Promise<Response> =>
|
71 | Promise.resolve(({
|
72 | json: () => {
|
73 | throw new Error("Some error");
|
74 | },
|
75 | } as unknown) as Response);
|
76 | window.fetch = mockFetch;
|
77 | const configFetcher = new IssuerConfigFetcher(mockStorageUtility({}));
|
78 |
|
79 | await expect(configFetcher.fetchConfig("https://some.url")).rejects.toThrow(
|
80 | "[https://some.url] has an invalid configuration: Some error"
|
81 | );
|
82 | });
|
83 | });
|