Fix Vercel APL set method + add tests for it (#52)

* Fix Vercel APL set method + add tests for it
This commit is contained in:
Krzysztof Wolski 2022-09-07 17:33:48 +02:00 committed by GitHub
parent 88119aa515
commit a50b055c7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 11 deletions

View file

@ -28,11 +28,10 @@
"uuid": "^8.3.2" "uuid": "^8.3.2"
}, },
"devDependencies": { "devDependencies": {
"release-it": "^15.4.1",
"@types/node-fetch": "^2.6.2",
"@testing-library/dom": "^8.17.1", "@testing-library/dom": "^8.17.1",
"@types/debug": "^4.1.7", "@types/debug": "^4.1.7",
"@types/node": "^18.6.5", "@types/node": "^18.6.5",
"@types/node-fetch": "^2.6.2",
"@types/uuid": "^8.3.4", "@types/uuid": "^8.3.4",
"@typescript-eslint/eslint-plugin": "^5.33.0", "@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0", "@typescript-eslint/parser": "^5.33.0",
@ -50,6 +49,7 @@
"husky": "^8.0.1", "husky": "^8.0.1",
"jsdom": "^20.0.0", "jsdom": "^20.0.0",
"prettier": "2.7.1", "prettier": "2.7.1",
"release-it": "^15.4.1",
"tsm": "^2.2.2", "tsm": "^2.2.2",
"tsup": "^6.2.1", "tsup": "^6.2.1",
"typescript": "^4.7.4", "typescript": "^4.7.4",

View file

@ -1,7 +1,14 @@
import { afterEach, describe, expect, it } from "vitest"; import fetch from "node-fetch";
import { afterEach, describe, expect, it, vi } from "vitest";
import { VercelAPL, VercelAPLVariables } from "./vercel-apl"; import { VercelAPL, VercelAPLVariables } from "./vercel-apl";
vi.mock("node-fetch", () => ({
default: vi.fn().mockImplementation(() => ""),
}));
const mockFetch = vi.mocked(fetch);
const aplConfig = { const aplConfig = {
deploymentToken: "token", deploymentToken: "token",
registerAppURL: "http://example.com", registerAppURL: "http://example.com",
@ -17,6 +24,7 @@ describe("APL", () => {
afterEach(() => { afterEach(() => {
process.env = { ...initialEnv }; process.env = { ...initialEnv };
vi.resetModules();
}); });
describe("VercelAPL", () => { describe("VercelAPL", () => {
@ -56,6 +64,48 @@ describe("APL", () => {
expect(apl["registerAppURL"]).toBe("option"); expect(apl["registerAppURL"]).toBe("option");
}); });
describe("set", () => {
it("Successful save of the auth data", async () => {
// @ts-ignore Ignore type of mocked response
mockFetch.mockResolvedValue({ status: 200 });
const apl = new VercelAPL({
registerAppURL: "https://registerService.example.com",
deploymentToken: "token",
});
await apl.set({ domain: "example.com", token: "token" });
expect(mockFetch).toBeCalledWith(
"https://registerService.example.com",
{
body: JSON.stringify({
token: "token",
envs: [
{ key: "SALEOR_AUTH_TOKEN", value: "token" },
{ key: "SALEOR_DOMAIN", value: "example.com" },
],
}),
headers: {
"Content-Type": "application/json",
},
method: "POST",
}
);
});
it("Raise error when register service returns non 200 response", async () => {
// @ts-ignore Ignore type of mocked response
mockFetch.mockResolvedValue({ status: 500 });
const apl = new VercelAPL({
registerAppURL: "https://registerService.example.com/internalError",
deploymentToken: "token",
});
await expect(apl.set({ domain: "example.com", token: "token" })).rejects.toThrow(
"Vercel APL was not able to save auth data, register service responded with the code 500"
);
});
});
describe("get", () => { describe("get", () => {
describe("Read existing auth data from env", () => { describe("Read existing auth data from env", () => {
it("Read existing auth data", async () => { it("Read existing auth data", async () => {

View file

@ -1,5 +1,5 @@
/* eslint-disable class-methods-use-this */ /* eslint-disable class-methods-use-this */
import fetch from "node-fetch"; import fetch, { Response } from "node-fetch";
import { APL, AuthData } from "./apl"; import { APL, AuthData } from "./apl";
import { createAPLDebug } from "./apl-debug"; import { createAPLDebug } from "./apl-debug";
@ -65,23 +65,34 @@ export class VercelAPL implements APL {
} }
private async saveDataToVercel(authData?: AuthData) { private async saveDataToVercel(authData?: AuthData) {
debug(`saveDataToVercel with: ${authData}`); debug("saveDataToVercel() called with: %j", {
domain: authData?.domain,
token: authData?.token.substring(0, 4),
});
let response: Response;
try { try {
await fetch(this.registerAppURL, { response = await fetch(this.registerAppURL, {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ body: JSON.stringify({
token: this.deploymentToken, token: this.deploymentToken,
envs: { envs: [
[VercelAPLVariables.TOKEN_VARIABLE_NAME]: authData?.token || "", { key: VercelAPLVariables.TOKEN_VARIABLE_NAME, value: authData?.token || "" },
[VercelAPLVariables.DOMAIN_VARIABLE_NAME]: authData?.domain || "", { key: VercelAPLVariables.DOMAIN_VARIABLE_NAME, value: authData?.domain || "" },
}, ],
}), }),
}); });
} catch (error) { } catch (error) {
debug("Error during saving the data:", error); debug("Error during saving the data:", error);
throw new Error(`VercelAPL was not able to save auth data${error}`); throw new Error(`VercelAPL was not able to save auth data ${error}`);
} }
if (response.status >= 400 || response.status < 200) {
debug("Non 200 response code. Register service responded with %j", response);
throw new Error(
`Vercel APL was not able to save auth data, register service responded with the code ${response.status}`
);
}
debug("Register service responded successfully");
} }
async get(domain: string) { async get(domain: string) {