saleor-app-sdk-REDIS_APL/src/APL/upstash-apl.test.ts

149 lines
4.6 KiB
TypeScript
Raw Normal View History

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";
const fetchMock = vi.fn();
2022-10-12 13:13:57 +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",
saleorApiUrl: "https://example.com/graphql/",
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
fetchMock.mockResolvedValue({
2022-10-12 13:13:57 +00:00
status: 200,
ok: true,
2022-10-12 13:13:57 +00:00
json: async () => ({ result: "ok" }),
});
const apl = new UpstashAPL({
restURL: "https://example.com",
restToken: "token",
});
await apl.set(stubAuthData);
expect(fetchMock).toBeCalledWith(
2022-10-12 13:13:57 +00:00
"https://example.com",
{
// eslint-disable-next-line quotes
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
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",
});
await expect(apl.set(stubAuthData)).rejects.toThrow(
"Upstash APL was not able to perform operation. Status code: 401. Error: Unauthorized"
2022-10-12 13:13:57 +00:00
);
});
});
describe("get", () => {
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
fetchMock.mockResolvedValue({
2022-10-12 13:13:57 +00:00
status: 200,
ok: true,
2022-10-12 13:13:57 +00:00
json: async () => ({
result: JSON.stringify(stubAuthData),
2022-10-12 13:13:57 +00:00
}),
});
const apl = new UpstashAPL(aplConfig);
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
fetchMock.mockResolvedValue({
2022-10-12 13:13:57 +00:00
status: 200,
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 () => {
// 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.'
);
});
});
});
});