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

120 lines
3.9 KiB
TypeScript
Raw Normal View History

import { promises as fsPromises } from "fs";
import { afterEach, describe, expect, it, vi } from "vitest";
2022-09-05 08:31:31 +00:00
import { FileAPL } from "./file-apl";
2022-09-01 14:43:28 +00:00
const stubAuthData = {
domain: "example.com",
token: "example-token",
};
describe("APL", () => {
2022-09-02 13:39:15 +00:00
afterEach(() => {
vi.clearAllMocks();
});
describe("FileAPL", () => {
describe("get", () => {
2022-09-02 13:39:15 +00:00
it("Should throw error when JSON parse fails", async () => {
vi.spyOn(fsPromises, "access").mockResolvedValue();
vi.spyOn(fsPromises, "readFile").mockResolvedValue("Not a valid JSON");
const apl = new FileAPL();
await expect(apl.get(stubAuthData.domain)).rejects.toThrow(
"File APL could not read auth data from the .saleor-app-auth.json file"
);
});
it("Returns auth data for existing domain", async () => {
vi.spyOn(fsPromises, "access").mockResolvedValue();
vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData));
const apl = new FileAPL();
expect(await apl.get(stubAuthData.domain)).toStrictEqual(stubAuthData);
});
it("Returns undefined for unknown domain", async () => {
vi.spyOn(fsPromises, "access").mockResolvedValue();
vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData));
const apl = new FileAPL();
expect(await apl.get("unknown-domain.example.com")).toBe(undefined);
});
});
2022-09-02 13:39:15 +00:00
describe("set", () => {
it("Handle write file errors", async () => {
const spyWriteFile = vi.spyOn(fsPromises, "writeFile").mockImplementation(() => {
throw Error("Write error");
});
const apl = new FileAPL();
await expect(apl.set(stubAuthData)).rejects.toThrow(
"File APL was unable to save auth data"
);
expect(spyWriteFile).toBeCalledWith(".saleor-app-auth.json", JSON.stringify(stubAuthData));
});
2022-09-02 13:39:15 +00:00
});
2022-09-02 13:39:15 +00:00
it("Successfully save to file", async () => {
const spyWriteFile = vi.spyOn(fsPromises, "writeFile").mockResolvedValue();
const apl = new FileAPL();
await expect(apl.set(stubAuthData));
expect(spyWriteFile).toBeCalledWith(".saleor-app-auth.json", JSON.stringify(stubAuthData));
});
describe("delete", () => {
it("Should override file when called with known domain", async () => {
vi.spyOn(fsPromises, "access").mockResolvedValue();
vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData));
2022-09-01 14:36:37 +00:00
const spyWriteFile = vi.spyOn(fsPromises, "writeFile").mockResolvedValue();
const apl = new FileAPL();
2022-09-01 14:36:37 +00:00
await apl.delete(stubAuthData.domain);
2022-09-01 16:40:58 +00:00
expect(spyWriteFile).toBeCalledWith(".saleor-app-auth.json", "{}");
});
it("Should not delete data when called with unknown domain", async () => {
vi.spyOn(fsPromises, "access").mockResolvedValue();
vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData));
const spyWriteFile = vi.spyOn(fsPromises, "writeFile").mockResolvedValue();
const apl = new FileAPL();
await apl.delete("unknown-domain.example.com");
expect(spyWriteFile).toBeCalledTimes(0);
});
});
describe("getAll", () => {
it("Should return list with one item when auth data are existing", async () => {
vi.spyOn(fsPromises, "access").mockResolvedValue();
vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData));
const apl = new FileAPL();
expect(await apl.getAll()).toStrictEqual([stubAuthData]);
});
it("Should return empty list when auth data are empty", async () => {
vi.spyOn(fsPromises, "access").mockResolvedValue();
vi.spyOn(fsPromises, "readFile").mockResolvedValue("{}");
const apl = new FileAPL();
expect(await apl.getAll()).toStrictEqual([]);
});
});
});
});