2022-05-27 21:09:27 +08:00
|
|
|
/*
|
2024-09-06 22:44:31 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-05-27 21:09:27 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-06 22:44:31 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-05-27 21:09:27 +08:00
|
|
|
*/
|
|
|
|
|
2022-10-13 01:59:10 +08:00
|
|
|
import fetchMock from "fetch-mock-jest";
|
2022-05-27 21:09:27 +08:00
|
|
|
|
|
|
|
import { getVectorConfig } from "../../../src/vector/getconfig";
|
|
|
|
|
2022-10-13 01:59:10 +08:00
|
|
|
fetchMock.config.overwriteRoutes = true;
|
2022-05-27 21:09:27 +08:00
|
|
|
|
2022-10-13 01:59:10 +08:00
|
|
|
describe("getVectorConfig()", () => {
|
2022-05-27 21:09:27 +08:00
|
|
|
const elementDomain = "app.element.io";
|
|
|
|
const now = 1234567890;
|
|
|
|
const specificConfig = {
|
|
|
|
brand: "specific",
|
2022-10-13 16:22:34 +08:00
|
|
|
};
|
2022-05-27 21:09:27 +08:00
|
|
|
const generalConfig = {
|
|
|
|
brand: "general",
|
2022-10-13 16:22:34 +08:00
|
|
|
};
|
2022-05-27 21:09:27 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-04-25 16:36:17 +08:00
|
|
|
Object.defineProperty(window, "location", {
|
|
|
|
value: { href: `https://${elementDomain}`, hostname: elementDomain },
|
|
|
|
writable: true,
|
|
|
|
});
|
2022-05-27 21:09:27 +08:00
|
|
|
|
|
|
|
// stable value for cachebuster
|
|
|
|
jest.spyOn(Date, "now").mockReturnValue(now);
|
|
|
|
jest.clearAllMocks();
|
2022-10-13 01:59:10 +08:00
|
|
|
fetchMock.mockClear();
|
2022-05-27 21:09:27 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
jest.spyOn(Date, "now").mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("requests specific config for document domain", async () => {
|
2022-10-13 01:59:10 +08:00
|
|
|
fetchMock.getOnce("express:/config.app.element.io.json", specificConfig);
|
|
|
|
fetchMock.getOnce("express:/config.json", generalConfig);
|
2022-07-11 20:22:37 +08:00
|
|
|
|
2022-10-13 01:59:10 +08:00
|
|
|
await expect(getVectorConfig()).resolves.toEqual(specificConfig);
|
2022-05-27 21:09:27 +08:00
|
|
|
});
|
2022-07-11 20:22:37 +08:00
|
|
|
|
2022-05-27 21:09:27 +08:00
|
|
|
it("adds trailing slash to relativeLocation when not an empty string", async () => {
|
2022-10-13 01:59:10 +08:00
|
|
|
fetchMock.getOnce("express:../config.app.element.io.json", specificConfig);
|
|
|
|
fetchMock.getOnce("express:../config.json", generalConfig);
|
2022-07-11 20:22:37 +08:00
|
|
|
|
2022-10-13 01:59:10 +08:00
|
|
|
await expect(getVectorConfig("..")).resolves.toEqual(specificConfig);
|
2022-05-27 21:09:27 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("returns general config when specific config succeeds but is empty", async () => {
|
2022-10-13 01:59:10 +08:00
|
|
|
fetchMock.getOnce("express:/config.app.element.io.json", {});
|
|
|
|
fetchMock.getOnce("express:/config.json", generalConfig);
|
2022-07-11 20:22:37 +08:00
|
|
|
|
2022-10-13 01:59:10 +08:00
|
|
|
await expect(getVectorConfig()).resolves.toEqual(generalConfig);
|
2022-05-27 21:09:27 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("returns general config when specific config 404s", async () => {
|
2022-10-13 01:59:10 +08:00
|
|
|
fetchMock.getOnce("express:/config.app.element.io.json", { status: 404 });
|
|
|
|
fetchMock.getOnce("express:/config.json", generalConfig);
|
2022-07-11 20:22:37 +08:00
|
|
|
|
2022-10-13 01:59:10 +08:00
|
|
|
await expect(getVectorConfig()).resolves.toEqual(generalConfig);
|
2022-05-27 21:09:27 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("returns general config when specific config is fetched from a file and is empty", async () => {
|
2022-10-13 01:59:10 +08:00
|
|
|
fetchMock.getOnce("express:/config.app.element.io.json", 0);
|
|
|
|
fetchMock.getOnce("express:/config.json", generalConfig);
|
2022-07-11 20:22:37 +08:00
|
|
|
|
2022-10-13 01:59:10 +08:00
|
|
|
await expect(getVectorConfig()).resolves.toEqual(generalConfig);
|
2022-05-27 21:09:27 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("returns general config when specific config returns a non-200 status", async () => {
|
2022-10-13 01:59:10 +08:00
|
|
|
fetchMock.getOnce("express:/config.app.element.io.json", { status: 401 });
|
|
|
|
fetchMock.getOnce("express:/config.json", generalConfig);
|
2022-07-11 20:22:37 +08:00
|
|
|
|
2022-10-13 01:59:10 +08:00
|
|
|
await expect(getVectorConfig()).resolves.toEqual(generalConfig);
|
2022-05-27 21:09:27 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("returns general config when specific config returns an error", async () => {
|
2022-10-13 01:59:10 +08:00
|
|
|
fetchMock.getOnce("express:/config.app.element.io.json", { throws: "err1" });
|
|
|
|
fetchMock.getOnce("express:/config.json", generalConfig);
|
2022-07-11 20:22:37 +08:00
|
|
|
|
2022-10-13 01:59:10 +08:00
|
|
|
await expect(getVectorConfig()).resolves.toEqual(generalConfig);
|
2022-05-27 21:09:27 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("rejects with an error when general config rejects", async () => {
|
2022-10-13 01:59:10 +08:00
|
|
|
fetchMock.getOnce("express:/config.app.element.io.json", { throws: "err-specific" });
|
|
|
|
fetchMock.getOnce("express:/config.json", { throws: "err-general" });
|
2022-05-27 21:09:27 +08:00
|
|
|
|
2022-10-13 01:59:10 +08:00
|
|
|
await expect(getVectorConfig()).rejects.toBe("err-general");
|
2022-05-27 21:09:27 +08:00
|
|
|
});
|
|
|
|
|
2022-07-11 20:22:37 +08:00
|
|
|
it("rejects with an error when config is invalid JSON", async () => {
|
2022-10-13 01:59:10 +08:00
|
|
|
fetchMock.getOnce("express:/config.app.element.io.json", { throws: "err-specific" });
|
|
|
|
fetchMock.getOnce("express:/config.json", '{"invalid": "json",}');
|
2022-07-11 20:22:37 +08:00
|
|
|
|
2022-10-13 01:59:10 +08:00
|
|
|
// We can't assert it'll be a SyntaxError as node-fetch behaves differently
|
|
|
|
// https://github.com/wheresrhys/fetch-mock/issues/270
|
2023-04-25 16:36:17 +08:00
|
|
|
await expect(getVectorConfig()).rejects.toThrow("in JSON at position 19");
|
2022-07-11 20:22:37 +08:00
|
|
|
});
|
2022-05-27 21:09:27 +08:00
|
|
|
});
|