2022-10-12 13:13:57 +00:00
|
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
|
|
|
|
|
|
import { AuthData } from "./apl";
|
|
|
|
import { UpstashAPL, UpstashAPLConfig, UpstashAPLVariables } from "./upstash-apl";
|
|
|
|
|
2023-01-11 15:55:10 +00:00
|
|
|
const fetchMock = vi.fn();
|
2022-10-12 13:13:57 +00:00
|
|
|
|
2023-01-11 15:55:10 +00:00
|
|
|
vi.stubGlobal("fetch", fetchMock);
|
2022-10-12 13:13:57 +00:00
|
|
|
|
|
|
|
const aplConfig: UpstashAPLConfig = {
|
|
|
|
restToken: "token",
|
|
|
|
restURL: "http://example.com",
|
|
|
|
};
|
|
|
|
|
|
|
|
const stubAuthData: AuthData = {
|
|
|
|
domain: "example.com",
|
|
|
|
token: "example-token",
|
2023-01-12 12:39:49 +00:00
|
|
|
saleorApiUrl: "https://example.com/graphql/",
|
2023-01-11 15:55:10 +00:00
|
|
|
appId: "42",
|
|
|
|
jwks: "{}",
|
2022-10-12 13:13:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
describe("APL", () => {
|
|
|
|
const initialEnv = { ...process.env };
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
process.env = { ...initialEnv };
|
|
|
|
vi.resetModules();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("UpstashAPL", () => {
|
|
|
|
it("Test if constructor use options over environment variables", async () => {
|
|
|
|
process.env[UpstashAPLVariables.UPSTASH_TOKEN] = "environment";
|
|
|
|
process.env[UpstashAPLVariables.UPSTASH_URL] = "environment";
|
|
|
|
|
|
|
|
const apl = await new UpstashAPL({ restToken: "option", restURL: "option" });
|
|
|
|
// eslint-disable-next-line dot-notation
|
|
|
|
expect(apl["restToken"]).toBe("option");
|
|
|
|
// eslint-disable-next-line dot-notation
|
|
|
|
expect(apl["restURL"]).toBe("option");
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("set", () => {
|
|
|
|
it("Successful save of the auth data", async () => {
|
|
|
|
// @ts-ignore Ignore type of mocked response
|
2023-01-11 15:55:10 +00:00
|
|
|
fetchMock.mockResolvedValue({
|
2022-10-12 13:13:57 +00:00
|
|
|
status: 200,
|
2023-03-09 07:59:35 +00:00
|
|
|
ok: true,
|
2022-10-12 13:13:57 +00:00
|
|
|
json: async () => ({ result: "ok" }),
|
|
|
|
});
|
|
|
|
const apl = new UpstashAPL({
|
|
|
|
restURL: "https://example.com",
|
|
|
|
restToken: "token",
|
|
|
|
});
|
2023-01-11 15:55:10 +00:00
|
|
|
await apl.set(stubAuthData);
|
|
|
|
expect(fetchMock).toBeCalledWith(
|
2022-10-12 13:13:57 +00:00
|
|
|
"https://example.com",
|
|
|
|
|
|
|
|
{
|
|
|
|
// eslint-disable-next-line quotes
|
2023-01-12 12:39:49 +00:00
|
|
|
body: `["SET", "${stubAuthData.saleorApiUrl}", "${JSON.stringify(stubAuthData)}"]`,
|
2022-10-12 13:13:57 +00:00
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer token",
|
|
|
|
},
|
|
|
|
method: "POST",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Raise error when register service returns non 200 response", async () => {
|
|
|
|
// @ts-ignore Ignore type of mocked response
|
2023-03-09 07:59:35 +00:00
|
|
|
fetchMock.mockResolvedValue({
|
|
|
|
status: 401,
|
|
|
|
ok: false,
|
|
|
|
json: async () => ({ error: "Unauthorized" }),
|
|
|
|
});
|
2022-10-12 13:13:57 +00:00
|
|
|
|
|
|
|
const apl = new UpstashAPL({
|
|
|
|
restURL: "https://example.com",
|
|
|
|
restToken: "token",
|
|
|
|
});
|
2023-01-11 15:55:10 +00:00
|
|
|
await expect(apl.set(stubAuthData)).rejects.toThrow(
|
2023-03-09 07:59:35 +00:00
|
|
|
"Upstash APL was not able to perform operation. Status code: 401. Error: Unauthorized"
|
2022-10-12 13:13:57 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("get", () => {
|
2023-01-11 15:55:10 +00:00
|
|
|
describe("Read existing auth data from upstash", () => {
|
2022-10-12 13:13:57 +00:00
|
|
|
it("Read existing auth data", async () => {
|
|
|
|
// @ts-ignore Ignore type of mocked response
|
2023-01-11 15:55:10 +00:00
|
|
|
fetchMock.mockResolvedValue({
|
2022-10-12 13:13:57 +00:00
|
|
|
status: 200,
|
2023-03-09 07:59:35 +00:00
|
|
|
ok: true,
|
2022-10-12 13:13:57 +00:00
|
|
|
json: async () => ({
|
2023-01-11 15:55:10 +00:00
|
|
|
result: JSON.stringify(stubAuthData),
|
2022-10-12 13:13:57 +00:00
|
|
|
}),
|
|
|
|
});
|
|
|
|
const apl = new UpstashAPL(aplConfig);
|
|
|
|
|
2023-01-12 12:39:49 +00:00
|
|
|
expect(await apl.get(stubAuthData.saleorApiUrl)).toStrictEqual(stubAuthData);
|
2022-10-12 13:13:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Return undefined when unknown domain requested", async () => {
|
|
|
|
// @ts-ignore Ignore type of mocked response
|
2023-01-11 15:55:10 +00:00
|
|
|
fetchMock.mockResolvedValue({
|
2022-10-12 13:13:57 +00:00
|
|
|
status: 200,
|
2023-03-09 07:59:35 +00:00
|
|
|
ok: true,
|
2022-10-12 13:13:57 +00:00
|
|
|
json: async () => ({
|
|
|
|
result: null,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
const apl = new UpstashAPL(aplConfig);
|
|
|
|
|
|
|
|
expect(await apl.get("unknown-domain.example.com")).toBe(undefined);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("getAll", () => {
|
|
|
|
describe("Check if error is raised", () => {
|
|
|
|
it("Read existing auth data", async () => {
|
|
|
|
const apl = new UpstashAPL(aplConfig);
|
|
|
|
await expect(apl.getAll()).rejects.toThrow("UpstashAPL does not support getAll method");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("isReady", () => {
|
|
|
|
it("Returns error with message mentioning missing configuration variables", async () => {
|
2022-11-02 13:18:27 +00:00
|
|
|
// Delete upstash variables if are already present in the env
|
|
|
|
delete process.env[UpstashAPLVariables.UPSTASH_TOKEN];
|
|
|
|
delete process.env[UpstashAPLVariables.UPSTASH_URL];
|
|
|
|
|
2022-10-12 13:13:57 +00:00
|
|
|
const apl = new UpstashAPL();
|
|
|
|
|
|
|
|
const result = await apl.isReady();
|
|
|
|
expect(result.ready).toBeFalsy();
|
|
|
|
// @ts-ignore
|
|
|
|
expect(result.error.message).toEqual(
|
|
|
|
// eslint-disable-next-line quotes
|
|
|
|
'Configuration values for: "restToken", "restURL" not found or is empty. Pass values to constructor of use env variables.'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|