Fix Vercel APL set method + add tests for it (#52)
* Fix Vercel APL set method + add tests for it
This commit is contained in:
parent
88119aa515
commit
a50b055c7c
3 changed files with 72 additions and 11 deletions
|
@ -28,11 +28,10 @@
|
|||
"uuid": "^8.3.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"release-it": "^15.4.1",
|
||||
"@types/node-fetch": "^2.6.2",
|
||||
"@testing-library/dom": "^8.17.1",
|
||||
"@types/debug": "^4.1.7",
|
||||
"@types/node": "^18.6.5",
|
||||
"@types/node-fetch": "^2.6.2",
|
||||
"@types/uuid": "^8.3.4",
|
||||
"@typescript-eslint/eslint-plugin": "^5.33.0",
|
||||
"@typescript-eslint/parser": "^5.33.0",
|
||||
|
@ -50,6 +49,7 @@
|
|||
"husky": "^8.0.1",
|
||||
"jsdom": "^20.0.0",
|
||||
"prettier": "2.7.1",
|
||||
"release-it": "^15.4.1",
|
||||
"tsm": "^2.2.2",
|
||||
"tsup": "^6.2.1",
|
||||
"typescript": "^4.7.4",
|
||||
|
|
|
@ -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";
|
||||
|
||||
vi.mock("node-fetch", () => ({
|
||||
default: vi.fn().mockImplementation(() => ""),
|
||||
}));
|
||||
|
||||
const mockFetch = vi.mocked(fetch);
|
||||
|
||||
const aplConfig = {
|
||||
deploymentToken: "token",
|
||||
registerAppURL: "http://example.com",
|
||||
|
@ -17,6 +24,7 @@ describe("APL", () => {
|
|||
|
||||
afterEach(() => {
|
||||
process.env = { ...initialEnv };
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
describe("VercelAPL", () => {
|
||||
|
@ -56,6 +64,48 @@ describe("APL", () => {
|
|||
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("Read existing auth data from env", () => {
|
||||
it("Read existing auth data", async () => {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/* eslint-disable class-methods-use-this */
|
||||
import fetch from "node-fetch";
|
||||
import fetch, { Response } from "node-fetch";
|
||||
|
||||
import { APL, AuthData } from "./apl";
|
||||
import { createAPLDebug } from "./apl-debug";
|
||||
|
@ -65,23 +65,34 @@ export class VercelAPL implements APL {
|
|||
}
|
||||
|
||||
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 {
|
||||
await fetch(this.registerAppURL, {
|
||||
response = await fetch(this.registerAppURL, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
token: this.deploymentToken,
|
||||
envs: {
|
||||
[VercelAPLVariables.TOKEN_VARIABLE_NAME]: authData?.token || "",
|
||||
[VercelAPLVariables.DOMAIN_VARIABLE_NAME]: authData?.domain || "",
|
||||
},
|
||||
envs: [
|
||||
{ key: VercelAPLVariables.TOKEN_VARIABLE_NAME, value: authData?.token || "" },
|
||||
{ key: VercelAPLVariables.DOMAIN_VARIABLE_NAME, value: authData?.domain || "" },
|
||||
],
|
||||
}),
|
||||
});
|
||||
} catch (error) {
|
||||
debug("Error during saving the 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) {
|
||||
|
|
Loading…
Reference in a new issue