2022-05-27 15:09:27 +02:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-10-12 19:59:10 +02:00
|
|
|
import fetchMock from "fetch-mock-jest";
|
2022-05-27 15:09:27 +02:00
|
|
|
|
|
|
|
import { getVectorConfig } from "../../../src/vector/getconfig";
|
|
|
|
|
2022-10-12 19:59:10 +02:00
|
|
|
fetchMock.config.overwriteRoutes = true;
|
2022-05-27 15:09:27 +02:00
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
describe("getVectorConfig()", () => {
|
2022-05-27 15:09:27 +02:00
|
|
|
const prevDocumentDomain = document.domain;
|
2022-12-09 13:28:29 +01:00
|
|
|
const elementDomain = "app.element.io";
|
2022-05-27 15:09:27 +02:00
|
|
|
const now = 1234567890;
|
|
|
|
const specificConfig = {
|
2022-12-09 13:28:29 +01:00
|
|
|
brand: "specific",
|
2022-10-13 10:22:34 +02:00
|
|
|
};
|
2022-05-27 15:09:27 +02:00
|
|
|
const generalConfig = {
|
2022-12-09 13:28:29 +01:00
|
|
|
brand: "general",
|
2022-10-13 10:22:34 +02:00
|
|
|
};
|
2022-05-27 15:09:27 +02:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
document.domain = elementDomain;
|
|
|
|
|
|
|
|
// stable value for cachebuster
|
2022-12-09 13:28:29 +01:00
|
|
|
jest.spyOn(Date, "now").mockReturnValue(now);
|
2022-05-27 15:09:27 +02:00
|
|
|
jest.clearAllMocks();
|
2022-10-12 19:59:10 +02:00
|
|
|
fetchMock.mockClear();
|
2022-05-27 15:09:27 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
document.domain = prevDocumentDomain;
|
2022-12-09 13:28:29 +01:00
|
|
|
jest.spyOn(Date, "now").mockRestore();
|
2022-05-27 15:09:27 +02:00
|
|
|
});
|
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
it("requests specific config for document domain", async () => {
|
2022-10-12 19:59:10 +02:00
|
|
|
fetchMock.getOnce("express:/config.app.element.io.json", specificConfig);
|
|
|
|
fetchMock.getOnce("express:/config.json", generalConfig);
|
2022-07-11 14:22:37 +02:00
|
|
|
|
2022-10-12 19:59:10 +02:00
|
|
|
await expect(getVectorConfig()).resolves.toEqual(specificConfig);
|
2022-05-27 15:09:27 +02:00
|
|
|
});
|
2022-07-11 14:22:37 +02:00
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
it("adds trailing slash to relativeLocation when not an empty string", async () => {
|
2022-10-12 19:59:10 +02:00
|
|
|
fetchMock.getOnce("express:../config.app.element.io.json", specificConfig);
|
|
|
|
fetchMock.getOnce("express:../config.json", generalConfig);
|
2022-07-11 14:22:37 +02:00
|
|
|
|
2022-10-12 19:59:10 +02:00
|
|
|
await expect(getVectorConfig("..")).resolves.toEqual(specificConfig);
|
2022-05-27 15:09:27 +02:00
|
|
|
});
|
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
it("returns general config when specific config succeeds but is empty", async () => {
|
2022-10-12 19:59:10 +02:00
|
|
|
fetchMock.getOnce("express:/config.app.element.io.json", {});
|
|
|
|
fetchMock.getOnce("express:/config.json", generalConfig);
|
2022-07-11 14:22:37 +02:00
|
|
|
|
2022-10-12 19:59:10 +02:00
|
|
|
await expect(getVectorConfig()).resolves.toEqual(generalConfig);
|
2022-05-27 15:09:27 +02:00
|
|
|
});
|
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
it("returns general config when specific config 404s", async () => {
|
2022-10-12 19:59:10 +02:00
|
|
|
fetchMock.getOnce("express:/config.app.element.io.json", { status: 404 });
|
|
|
|
fetchMock.getOnce("express:/config.json", generalConfig);
|
2022-07-11 14:22:37 +02:00
|
|
|
|
2022-10-12 19:59:10 +02:00
|
|
|
await expect(getVectorConfig()).resolves.toEqual(generalConfig);
|
2022-05-27 15:09:27 +02:00
|
|
|
});
|
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
it("returns general config when specific config is fetched from a file and is empty", async () => {
|
2022-10-12 19:59:10 +02:00
|
|
|
fetchMock.getOnce("express:/config.app.element.io.json", 0);
|
|
|
|
fetchMock.getOnce("express:/config.json", generalConfig);
|
2022-07-11 14:22:37 +02:00
|
|
|
|
2022-10-12 19:59:10 +02:00
|
|
|
await expect(getVectorConfig()).resolves.toEqual(generalConfig);
|
2022-05-27 15:09:27 +02:00
|
|
|
});
|
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
it("returns general config when specific config returns a non-200 status", async () => {
|
2022-10-12 19:59:10 +02:00
|
|
|
fetchMock.getOnce("express:/config.app.element.io.json", { status: 401 });
|
|
|
|
fetchMock.getOnce("express:/config.json", generalConfig);
|
2022-07-11 14:22:37 +02:00
|
|
|
|
2022-10-12 19:59:10 +02:00
|
|
|
await expect(getVectorConfig()).resolves.toEqual(generalConfig);
|
2022-05-27 15:09:27 +02:00
|
|
|
});
|
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
it("returns general config when specific config returns an error", async () => {
|
2022-10-12 19:59:10 +02:00
|
|
|
fetchMock.getOnce("express:/config.app.element.io.json", { throws: "err1" });
|
|
|
|
fetchMock.getOnce("express:/config.json", generalConfig);
|
2022-07-11 14:22:37 +02:00
|
|
|
|
2022-10-12 19:59:10 +02:00
|
|
|
await expect(getVectorConfig()).resolves.toEqual(generalConfig);
|
2022-05-27 15:09:27 +02:00
|
|
|
});
|
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
it("rejects with an error when general config rejects", async () => {
|
2022-10-12 19:59:10 +02:00
|
|
|
fetchMock.getOnce("express:/config.app.element.io.json", { throws: "err-specific" });
|
|
|
|
fetchMock.getOnce("express:/config.json", { throws: "err-general" });
|
2022-05-27 15:09:27 +02:00
|
|
|
|
2022-10-12 19:59:10 +02:00
|
|
|
await expect(getVectorConfig()).rejects.toBe("err-general");
|
2022-05-27 15:09:27 +02:00
|
|
|
});
|
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
it("rejects with an error when config is invalid JSON", async () => {
|
2022-10-12 19:59:10 +02:00
|
|
|
fetchMock.getOnce("express:/config.app.element.io.json", { throws: "err-specific" });
|
|
|
|
fetchMock.getOnce("express:/config.json", '{"invalid": "json",}');
|
2022-07-11 14:22:37 +02:00
|
|
|
|
2022-10-12 19:59:10 +02:00
|
|
|
// We can't assert it'll be a SyntaxError as node-fetch behaves differently
|
|
|
|
// https://github.com/wheresrhys/fetch-mock/issues/270
|
|
|
|
await expect(getVectorConfig()).rejects.toThrow("Unexpected token } in JSON at position 19");
|
2022-07-11 14:22:37 +02:00
|
|
|
});
|
2022-05-27 15:09:27 +02:00
|
|
|
});
|