Skip to content

Commit

Permalink
Change behavior when FileAPL reads file that doesnt exist to fallback…
Browse files Browse the repository at this point in the history
… value (#96)
  • Loading branch information
lkostrowski authored Oct 24, 2022
1 parent f17e0a3 commit ff49281
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
25 changes: 8 additions & 17 deletions src/APL/file-apl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@ describe("APL", () => {

describe("FileAPL", () => {
describe("get", () => {
it("Should throw error when JSON parse fails", async () => {
vi.spyOn(fsPromises, "access").mockResolvedValue();
it("Should fallback to 'undefined' if parsing fail fails", async () => {
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"
);
await expect(apl.get(stubAuthData.domain)).resolves.toBe(undefined);
});

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();
Expand All @@ -35,7 +31,6 @@ describe("APL", () => {
});

it("Returns undefined for unknown domain", async () => {
vi.spyOn(fsPromises, "access").mockResolvedValue();
vi.spyOn(fsPromises, "readFile").mockResolvedValue(JSON.stringify(stubAuthData));

const apl = new FileAPL();
Expand All @@ -57,21 +52,20 @@ describe("APL", () => {
);
expect(spyWriteFile).toBeCalledWith(".saleor-app-auth.json", JSON.stringify(stubAuthData));
});
});

it("Successfully save to file", async () => {
const spyWriteFile = vi.spyOn(fsPromises, "writeFile").mockResolvedValue();
it("Successfully save to file", async () => {
const spyWriteFile = vi.spyOn(fsPromises, "writeFile").mockResolvedValue();

const apl = new FileAPL();
const apl = new FileAPL();

await expect(apl.set(stubAuthData));
await expect(apl.set(stubAuthData));

expect(spyWriteFile).toBeCalledWith(".saleor-app-auth.json", JSON.stringify(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));
const spyWriteFile = vi.spyOn(fsPromises, "writeFile").mockResolvedValue();

Expand All @@ -83,7 +77,6 @@ describe("APL", () => {
});

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();
Expand All @@ -98,7 +91,6 @@ describe("APL", () => {

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();
Expand All @@ -107,7 +99,6 @@ describe("APL", () => {
});

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();
Expand Down
26 changes: 17 additions & 9 deletions src/APL/file-apl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,46 @@ export class FileAPL implements APL {
/**
* Load auth data from a file and return it as AuthData format.
* In case of incomplete or invalid data, return `undefined`.
*
* @param {string} fileName
*/
private async loadDataFromFile(): Promise<AuthData | undefined> {
debug(`Load auth data from the ${this.fileName} file`);
debug(`Will try to load auth data from the ${this.fileName} file`);
let parsedData: Record<string, string> = {};

try {
await fsPromises.access(this.fileName);
parsedData = JSON.parse(await fsPromises.readFile(this.fileName, "utf-8"));
debug("%s read successfully", this.fileName);
} catch (err) {
debug(`Could not read auth data from the ${this.fileName} file`, err);
throw new Error(`File APL could not read auth data from the ${this.fileName} file`);
debug(
"Maybe apl.get() was called before app was registered. Returning empty, fallback data (undefined)"
);

return undefined;
}

const { token, domain } = parsedData;

if (token && domain) {
debug("Token and domain found, returning values: %s, %s", domain, `${token[0]}***`);
return { token, domain };
}

return undefined;
}

/**
* Save auth data to file.
* When `authData` argument is empty, will overwrite file with empty values.
*
* @param {string} fileName
* @param {AuthData} [authData]
*/
private async saveDataToFile(authData?: AuthData) {
debug(`Save auth data to the ${this.fileName} file`);
debug(`Trying to save auth data to the ${this.fileName} file`);

const newData = authData ? JSON.stringify(authData) : "{}";

try {
await fsPromises.writeFile(this.fileName, newData);

debug("Successfully written file %", this.fileName);
} catch (err) {
debug(`Could not save auth data to the ${this.fileName} file`, err);
throw new Error("File APL was unable to save auth data");
Expand Down

0 comments on commit ff49281

Please sign in to comment.