UNPKG

3.41 kBPlain TextView Raw
1/*
2 * Copyright 2021 Inrupt Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal in
6 * the Software without restriction, including without limitation the rights to use,
7 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 * Software, and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22import "reflect-metadata";
23import { mockStorageUtility } from "@inrupt/solid-client-authn-core";
24import { Response as NodeResponse } from "node-fetch";
25import IssuerConfigFetcher from "../../../src/login/oidc/IssuerConfigFetcher";
26
27/**
28 * Test for IssuerConfigFetcher
29 */
30describe("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 // eslint-disable-next-line camelcase
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 // eslint-disable-next-line @typescript-eslint/no-explicit-any
63 expect((fetchedConfig as any).claim_types_supported).toBeUndefined();
64 expect(fetchedConfig.claimTypesSupported).toBe("oidc");
65 // eslint-disable-next-line @typescript-eslint/no-explicit-any
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});