Files
MagicMirror/tests/unit/functions/server_functions_spec.js
T

186 lines
5.9 KiB
JavaScript
Raw Normal View History

const { cors, getUserAgent, replaceSecretPlaceholder } = require("#server_functions");
2023-01-01 18:09:08 +01:00
describe("server_functions tests", () => {
describe("The replaceSecretPlaceholder method", () => {
it("Calls string without secret placeholder", () => {
const teststring = "test string without secret placeholder";
const result = replaceSecretPlaceholder(teststring);
expect(result).toBe(teststring);
});
it("Calls string with 2 secret placeholders", () => {
const teststring = "test string with secret1=**SECRET_ONE** and secret2=**SECRET_TWO**";
process.env.SECRET_ONE = "secret1";
process.env.SECRET_TWO = "secret2";
const resultstring = `test string with secret1=${process.env.SECRET_ONE} and secret2=${process.env.SECRET_TWO}`;
const result = replaceSecretPlaceholder(teststring);
expect(result).toBe(resultstring);
});
});
2023-01-01 18:09:08 +01:00
describe("The cors method", () => {
let fetchResponse;
let fetchResponseHeadersGet;
let fetchResponseArrayBuffer;
2023-01-01 18:09:08 +01:00
let corsResponse;
let request;
let fetchMock;
beforeEach(() => {
2025-11-03 19:47:01 +01:00
fetchResponseHeadersGet = vi.fn(() => {});
fetchResponseArrayBuffer = vi.fn(() => {});
2023-01-01 18:09:08 +01:00
fetchResponse = {
headers: {
get: fetchResponseHeadersGet
},
arrayBuffer: fetchResponseArrayBuffer,
ok: true
2023-01-01 18:09:08 +01:00
};
2024-09-25 21:05:11 +02:00
2025-11-03 19:47:01 +01:00
fetch = vi.fn();
2023-10-01 20:13:41 +02:00
fetch.mockImplementation(() => fetchResponse);
2023-01-01 18:09:08 +01:00
2023-10-01 20:13:41 +02:00
fetchMock = fetch;
2023-01-01 18:09:08 +01:00
corsResponse = {
2025-11-03 19:47:01 +01:00
set: vi.fn(() => {}),
send: vi.fn(() => {}),
status: vi.fn(function (code) {
this.statusCode = code;
return this;
}),
json: vi.fn(() => {})
2023-01-01 18:09:08 +01:00
};
request = {
2024-01-01 15:38:08 +01:00
url: "/cors?url=www.test.com"
2023-01-01 18:09:08 +01:00
};
});
2024-01-01 15:38:08 +01:00
it("Calls correct URL once", async () => {
2023-01-01 18:09:08 +01:00
const urlToCall = "http://www.test.com/path?param1=value1";
request.url = `/cors?url=${urlToCall}`;
await cors(request, corsResponse);
2024-01-01 15:38:08 +01:00
expect(fetchMock.mock.calls).toHaveLength(1);
2023-01-01 18:09:08 +01:00
expect(fetchMock.mock.calls[0][0]).toBe(urlToCall);
});
2024-09-18 07:37:09 +02:00
it("Forwards Content-Type if json", async () => {
2023-01-01 18:09:08 +01:00
fetchResponseHeadersGet.mockImplementation(() => "json");
await cors(request, corsResponse);
2024-01-01 15:38:08 +01:00
expect(fetchResponseHeadersGet.mock.calls).toHaveLength(1);
2023-01-01 18:09:08 +01:00
expect(fetchResponseHeadersGet.mock.calls[0][0]).toBe("Content-Type");
2024-01-01 15:38:08 +01:00
expect(corsResponse.set.mock.calls).toHaveLength(1);
2023-01-01 18:09:08 +01:00
expect(corsResponse.set.mock.calls[0][0]).toBe("Content-Type");
expect(corsResponse.set.mock.calls[0][1]).toBe("json");
});
2024-09-18 07:37:09 +02:00
it("Forwards Content-Type if xml", async () => {
2023-01-01 18:09:08 +01:00
fetchResponseHeadersGet.mockImplementation(() => "xml");
await cors(request, corsResponse);
2024-01-01 15:38:08 +01:00
expect(fetchResponseHeadersGet.mock.calls).toHaveLength(1);
2023-01-01 18:09:08 +01:00
expect(fetchResponseHeadersGet.mock.calls[0][0]).toBe("Content-Type");
2024-01-01 15:38:08 +01:00
expect(corsResponse.set.mock.calls).toHaveLength(1);
2023-01-01 18:09:08 +01:00
expect(corsResponse.set.mock.calls[0][0]).toBe("Content-Type");
expect(corsResponse.set.mock.calls[0][1]).toBe("xml");
});
2024-01-01 15:38:08 +01:00
it("Sends correct data from response", async () => {
2023-01-01 18:09:08 +01:00
const responseData = "some data";
const encoder = new TextEncoder();
const arrayBuffer = encoder.encode(responseData).buffer;
fetchResponseArrayBuffer.mockImplementation(() => arrayBuffer);
2023-01-01 18:09:08 +01:00
let sentData;
2025-11-03 19:47:01 +01:00
corsResponse.send = vi.fn((input) => {
2023-01-01 18:09:08 +01:00
sentData = input;
});
await cors(request, corsResponse);
expect(fetchResponseArrayBuffer.mock.calls).toHaveLength(1);
expect(sentData).toEqual(Buffer.from(arrayBuffer));
2023-01-01 18:09:08 +01:00
});
2024-01-01 15:38:08 +01:00
it("Sends error data from response", async () => {
2023-01-01 18:09:08 +01:00
const error = new Error("error data");
fetchResponseArrayBuffer.mockImplementation(() => {
2023-01-01 18:09:08 +01:00
throw error;
});
await cors(request, corsResponse);
expect(fetchResponseArrayBuffer.mock.calls).toHaveLength(1);
expect(corsResponse.status).toHaveBeenCalledWith(500);
expect(corsResponse.json).toHaveBeenCalledWith({ error: error.message });
2023-01-01 18:09:08 +01:00
});
2024-01-01 15:38:08 +01:00
it("Fetches with user agent by default", async () => {
2023-01-01 18:09:08 +01:00
await cors(request, corsResponse);
2024-01-01 15:38:08 +01:00
expect(fetchMock.mock.calls).toHaveLength(1);
2023-01-01 18:09:08 +01:00
expect(fetchMock.mock.calls[0][1]).toHaveProperty("headers");
expect(fetchMock.mock.calls[0][1].headers).toHaveProperty("User-Agent");
});
2024-01-01 15:38:08 +01:00
it("Fetches with specified headers", async () => {
2023-01-01 18:09:08 +01:00
const headersParam = "sendheaders=header1:value1,header2:value2";
const urlParam = "http://www.test.com/path?param1=value1";
request.url = `/cors?${headersParam}&url=${urlParam}`;
await cors(request, corsResponse);
2024-01-01 15:38:08 +01:00
expect(fetchMock.mock.calls).toHaveLength(1);
2023-01-01 18:09:08 +01:00
expect(fetchMock.mock.calls[0][1]).toHaveProperty("headers");
expect(fetchMock.mock.calls[0][1].headers).toHaveProperty("header1", "value1");
expect(fetchMock.mock.calls[0][1].headers).toHaveProperty("header2", "value2");
});
2024-01-01 15:38:08 +01:00
it("Sends specified headers", async () => {
2023-01-01 18:09:08 +01:00
fetchResponseHeadersGet.mockImplementation((input) => input.replace("header", "value"));
const expectedheaders = "expectedheaders=header1,header2";
const urlParam = "http://www.test.com/path?param1=value1";
request.url = `/cors?${expectedheaders}&url=${urlParam}`;
await cors(request, corsResponse);
2024-01-01 15:38:08 +01:00
expect(fetchMock.mock.calls).toHaveLength(1);
2023-01-01 18:09:08 +01:00
expect(fetchMock.mock.calls[0][1]).toHaveProperty("headers");
2024-01-01 15:38:08 +01:00
expect(corsResponse.set.mock.calls).toHaveLength(3);
2023-01-01 18:09:08 +01:00
expect(corsResponse.set.mock.calls[0][0]).toBe("Content-Type");
expect(corsResponse.set.mock.calls[1][0]).toBe("header1");
expect(corsResponse.set.mock.calls[1][1]).toBe("value1");
expect(corsResponse.set.mock.calls[2][0]).toBe("header2");
expect(corsResponse.set.mock.calls[2][1]).toBe("value2");
});
2025-08-27 13:50:37 +02:00
it("Gets User-Agent from configuration", () => {
const previousConfig = global.config;
2025-11-03 19:47:01 +01:00
global.config = {};
2025-08-27 13:50:37 +02:00
let userAgent;
userAgent = getUserAgent();
expect(userAgent).toContain("Mozilla/5.0 (Node.js ");
2025-11-03 19:47:01 +01:00
global.config.userAgent = "Mozilla/5.0 (Foo)";
2025-08-27 13:50:37 +02:00
userAgent = getUserAgent();
expect(userAgent).toBe("Mozilla/5.0 (Foo)");
2025-11-03 19:47:01 +01:00
global.config.userAgent = () => "Mozilla/5.0 (Bar)";
2025-08-27 13:50:37 +02:00
userAgent = getUserAgent();
expect(userAgent).toBe("Mozilla/5.0 (Bar)");
global.config = previousConfig;
2025-08-27 13:50:37 +02:00
});
2023-01-01 18:09:08 +01:00
});
});