-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathsaleor-sync-webhook.test.ts
128 lines (106 loc) · 4.61 KB
/
saleor-sync-webhook.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { afterEach, describe, expect, it, vi } from "vitest";
import { buildSyncWebhookResponsePayload, FormatWebhookErrorResult } from "@/handlers/shared";
import { SaleorWebhookValidator } from "@/handlers/shared/saleor-webhook-validator";
import { MockAPL } from "@/test-utils/mock-apl";
import { SaleorSyncWebhook, WebApiSyncWebhookHandler } from "./saleor-sync-webhook";
describe("Web API SaleorSyncWebhook", () => {
const mockAPL = new MockAPL();
const baseUrl = "http://saleor-app.com";
const webhookConfiguration = {
apl: mockAPL,
webhookPath: "api/webhooks/checkout-calculate-taxes",
event: "CHECKOUT_CALCULATE_TAXES",
query: "subscription { event { ... on CheckoutCalculateTaxes { payload } } }",
name: "Webhook test name",
isActive: true,
} as const;
afterEach(() => {
vi.restoreAllMocks();
});
it("should validate request and return Response", async () => {
type Payload = { data: "test_payload" };
vi.spyOn(SaleorWebhookValidator.prototype, "validateRequest").mockResolvedValue({
result: "ok",
context: {
baseUrl: "example.com",
event: "checkout_calculate_taxes",
payload: { data: "test_payload" },
schemaVersion: 3.19,
authData: {
token: webhookConfiguration.apl.mockToken,
jwks: webhookConfiguration.apl.mockJwks,
saleorApiUrl: webhookConfiguration.apl.workingSaleorApiUrl,
appId: webhookConfiguration.apl.mockAppId,
},
},
});
const handler = vi.fn<WebApiSyncWebhookHandler<Payload>>().mockImplementation(() => {
const responsePayload = buildSyncWebhookResponsePayload<"ORDER_CALCULATE_TAXES">({
lines: [{ tax_rate: 8, total_net_amount: 10, total_gross_amount: 1.08 }],
shipping_price_gross_amount: 2,
shipping_tax_rate: 8,
shipping_price_net_amount: 1,
});
return new Response(JSON.stringify(responsePayload), { status: 200 });
});
const saleorSyncWebhook = new SaleorSyncWebhook<Payload>(webhookConfiguration);
// Note: Requests are not representative of a real one,
// we mock resolved value from webhook validator, which parses request
const request = new Request(`${baseUrl}/webhook`);
const wrappedHandler = saleorSyncWebhook.createHandler(handler);
const response = await wrappedHandler(request);
expect(response.status).toBe(200);
expect(handler).toBeCalledTimes(1);
await expect(response.json()).resolves.toEqual(
expect.objectContaining({
lines: [{ tax_rate: 8, total_net_amount: 10, total_gross_amount: 1.08 }],
shipping_price_gross_amount: 2,
shipping_tax_rate: 8,
shipping_price_net_amount: 1,
}),
);
});
it("should return error when request is not valid", async () => {
vi.spyOn(SaleorWebhookValidator.prototype, "validateRequest").mockResolvedValue({
result: "failure",
error: new Error("Test error"),
});
const saleorSyncWebhook = new SaleorSyncWebhook({
...webhookConfiguration,
});
const handler = vi.fn();
const wrappedHandler = saleorSyncWebhook.createHandler(handler);
// Note: Requests are not representative of a real one,
// we mock resolved value from webhook validator, which parses request
const request = new Request(`${baseUrl}/webhook`);
const response = await wrappedHandler(request);
expect(response.status).toBe(500);
await expect(response.text()).resolves.toBe("Unexpected error while handling request");
expect(handler).not.toHaveBeenCalled();
});
it("should allow overriding error responses using formatErrorResponse", async () => {
const error = new Error("Test error");
vi.spyOn(SaleorWebhookValidator.prototype, "validateRequest").mockResolvedValue({
result: "failure",
error,
});
const mockFormatErrorResponse = vi.fn().mockResolvedValue({
body: "Custom error",
code: 418,
} as FormatWebhookErrorResult);
const saleorSyncWebhook = new SaleorSyncWebhook({
...webhookConfiguration,
formatErrorResponse: mockFormatErrorResponse,
});
const handler = vi.fn();
const wrappedHandler = saleorSyncWebhook.createHandler(handler);
// Note: Requests are not representative of a real one,
// we mock resolved value from webhook validator, which parses request
const request = new Request(`${baseUrl}/webhook`);
const response = await wrappedHandler(request);
expect(mockFormatErrorResponse).toHaveBeenCalledWith(error, request);
expect(response.status).toBe(418);
await expect(response.text()).resolves.toBe("Custom error");
expect(handler).not.toHaveBeenCalled();
});
});