2022-09-21 09:25:01 +00:00
|
|
|
import { createMocks } from "node-mocks-http";
|
|
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
|
|
|
|
import { APL } from "../../APL";
|
|
|
|
import { createAppRegisterHandler } from "./create-app-register-handler";
|
|
|
|
|
2023-01-13 16:29:38 +00:00
|
|
|
vi.mock("../../get-app-id", () => ({
|
|
|
|
getAppId: vi.fn().mockResolvedValue("42"),
|
|
|
|
}));
|
2022-12-06 17:26:14 +00:00
|
|
|
|
2023-01-13 16:29:38 +00:00
|
|
|
vi.mock("../../fetch-remote-jwks", () => ({
|
|
|
|
fetchRemoteJwks: vi.fn().mockResolvedValue("{}"),
|
|
|
|
}));
|
2023-01-11 15:55:10 +00:00
|
|
|
|
2023-01-13 16:29:38 +00:00
|
|
|
const mockApl: APL = {
|
|
|
|
get: vi.fn(),
|
|
|
|
set: vi.fn(),
|
|
|
|
delete: vi.fn(),
|
|
|
|
getAll: vi.fn(),
|
|
|
|
isReady: vi.fn().mockImplementation(async () => ({
|
|
|
|
ready: true,
|
|
|
|
})),
|
|
|
|
isConfigured: vi.fn().mockImplementation(async () => ({
|
|
|
|
configured: true,
|
|
|
|
})),
|
|
|
|
};
|
2022-09-21 09:25:01 +00:00
|
|
|
|
2023-01-13 16:29:38 +00:00
|
|
|
describe("create-app-register-handler", () => {
|
|
|
|
it("Sets auth data for correct request", async () => {
|
2022-09-21 09:25:01 +00:00
|
|
|
const { res, req } = createMocks({
|
|
|
|
/**
|
|
|
|
* Use body, instead of params, otherwise - for some reason - param is not accessible in mock request
|
|
|
|
* Maybe this is a bug https://github.com/howardabrams/node-mocks-http/blob/master/lib/mockRequest.js
|
|
|
|
*/
|
|
|
|
body: {
|
|
|
|
auth_token: "mock-auth-token",
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
host: "some-saleor-host.cloud",
|
|
|
|
"x-forwarded-proto": "https",
|
2023-01-11 15:55:10 +00:00
|
|
|
"saleor-api-url": "https://mock-saleor-domain.saleor.cloud/graphql/",
|
|
|
|
"saleor-domain": "https://mock-saleor-domain.saleor.cloud/",
|
2022-09-21 09:25:01 +00:00
|
|
|
},
|
|
|
|
method: "POST",
|
|
|
|
});
|
|
|
|
|
|
|
|
const handler = createAppRegisterHandler({
|
|
|
|
apl: mockApl,
|
|
|
|
});
|
|
|
|
|
|
|
|
await handler(req, res);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* It fails -> params.auth_token isn't present
|
|
|
|
*/
|
|
|
|
expect(mockApl.set).toHaveBeenCalledWith({
|
2023-01-12 12:39:49 +00:00
|
|
|
saleorApiUrl: "https://mock-saleor-domain.saleor.cloud/graphql/",
|
2023-01-11 15:55:10 +00:00
|
|
|
domain: "https://mock-saleor-domain.saleor.cloud/",
|
2022-09-21 09:25:01 +00:00
|
|
|
token: "mock-auth-token",
|
2023-01-11 15:55:10 +00:00
|
|
|
appId: "42",
|
|
|
|
jwks: "{}",
|
2022-09-21 09:25:01 +00:00
|
|
|
});
|
|
|
|
});
|
2023-01-13 16:29:38 +00:00
|
|
|
|
|
|
|
it("Returns 403 if configured to work only for specific saleor URL and try to install on prohibited one", async () => {
|
|
|
|
const { res, req } = createMocks({
|
|
|
|
/**
|
|
|
|
* Use body, instead of params, otherwise - for some reason - param is not accessible in mock request
|
|
|
|
* Maybe this is a bug https://github.com/howardabrams/node-mocks-http/blob/master/lib/mockRequest.js
|
|
|
|
*/
|
|
|
|
body: {
|
|
|
|
auth_token: "mock-auth-token",
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
host: "some-saleor-host.cloud",
|
|
|
|
"x-forwarded-proto": "https",
|
|
|
|
"saleor-api-url": "https://wrong-saleor-domain.saleor.cloud/graphql/",
|
|
|
|
"saleor-domain": "https://wrong-saleor-domain.saleor.cloud/",
|
|
|
|
},
|
|
|
|
method: "POST",
|
|
|
|
});
|
|
|
|
|
|
|
|
const handler = createAppRegisterHandler({
|
|
|
|
apl: mockApl,
|
|
|
|
allowedSaleorUrls: [(url: string) => url === "https://mock-saleor-domain.saleor.cloud"],
|
|
|
|
});
|
|
|
|
|
|
|
|
await handler(req, res);
|
|
|
|
|
|
|
|
expect(res._getStatusCode()).toBe(403);
|
|
|
|
expect(res._getData().success).toBe(false);
|
|
|
|
});
|
2022-09-21 09:25:01 +00:00
|
|
|
});
|