-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathsaleor-sync-webhook.test.ts
171 lines (144 loc) · 6.44 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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 { createLambdaEvent, mockLambdaContext } from "../test-utils";
import { AwsLambdaSyncWebhookHandler, SaleorSyncWebhook } from "./saleor-sync-webhook";
describe("AWS Lambda SaleorSyncWebhook", () => {
const mockAPL = new MockAPL();
const webhookConfig = {
apl: mockAPL,
webhookPath: "api/webhooks/checkout-calculate-taxes",
event: "CHECKOUT_CALCULATE_TAXES" as const,
query: "subscription { event { ... on CheckoutCalculateTaxes { payload } } }",
name: "Lambda Webhook Test",
isActive: true,
};
afterEach(() => {
vi.restoreAllMocks();
});
describe("getWebhookManifest", () => {
it("returns valid URL when baseUrl has Lambda stage", () => {
const webhook = new SaleorSyncWebhook(webhookConfig);
expect(webhook.getWebhookManifest("https://aws-lambda.com/prod").targetUrl).toBe(
"https://aws-lambda.com/prod/api/webhooks/checkout-calculate-taxes",
);
expect(webhook.getWebhookManifest("https://aws-lambda.com/prod/").targetUrl).toBe(
"https://aws-lambda.com/prod/api/webhooks/checkout-calculate-taxes",
);
expect(webhook.getWebhookManifest("https://aws-lambda.com/test").targetUrl).toBe(
"https://aws-lambda.com/test/api/webhooks/checkout-calculate-taxes",
);
});
it("returns valid URL when baseURl doesn't have lambda stage ($default stage)", () => {
const webhook = new SaleorSyncWebhook(webhookConfig);
expect(webhook.getWebhookManifest("https://aws-lambda.com/").targetUrl).toBe(
"https://aws-lambda.com/api/webhooks/checkout-calculate-taxes",
);
});
it("returns valid URL if webhook path has forward slash", () => {
const webhook = new SaleorSyncWebhook({
...webhookConfig,
webhookPath: `/${webhookConfig.webhookPath}`,
});
expect(webhook.getWebhookManifest("https://aws-lambda.com/prod").targetUrl).toBe(
"https://aws-lambda.com/prod/api/webhooks/checkout-calculate-taxes",
);
expect(webhook.getWebhookManifest("https://aws-lambda.com/prod/").targetUrl).toBe(
"https://aws-lambda.com/prod/api/webhooks/checkout-calculate-taxes",
);
expect(webhook.getWebhookManifest("https://aws-lambda.com/test").targetUrl).toBe(
"https://aws-lambda.com/test/api/webhooks/checkout-calculate-taxes",
);
expect(webhook.getWebhookManifest("https://aws-lambda.com/").targetUrl).toBe(
"https://aws-lambda.com/api/webhooks/checkout-calculate-taxes",
);
});
});
describe("createHandler", () => {
it("should validate request and return successful tax calculation", 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: webhookConfig.apl.mockToken,
jwks: webhookConfig.apl.mockJwks,
saleorApiUrl: webhookConfig.apl.workingSaleorApiUrl,
appId: webhookConfig.apl.mockAppId,
},
},
});
const saleorSyncWebhook = new SaleorSyncWebhook<Payload>(webhookConfig);
const handler: AwsLambdaSyncWebhookHandler<Payload> = vi
.fn()
.mockImplementation(async () => ({
statusCode: 200,
body: JSON.stringify(
buildSyncWebhookResponsePayload<"CHECKOUT_CALCULATE_TAXES">({
lines: [{ tax_rate: 8, total_net_amount: 10, total_gross_amount: 10.8 }],
shipping_price_gross_amount: 2.16,
shipping_tax_rate: 8,
shipping_price_net_amount: 2,
}),
),
}));
const wrappedHandler = saleorSyncWebhook.createHandler(handler);
// Note: Events are not representative,
// we mock resolved value from webhook validator
const event = createLambdaEvent();
const response = await wrappedHandler(event, mockLambdaContext);
expect(response.statusCode).toBe(200);
expect(handler).toBeCalledTimes(1);
expect(JSON.parse(response.body!)).toEqual({
lines: [{ tax_rate: 8, total_net_amount: 10, total_gross_amount: 10.8 }],
shipping_price_gross_amount: 2.16,
shipping_tax_rate: 8,
shipping_price_net_amount: 2,
});
});
it("should return 500 error when validation fails", async () => {
vi.spyOn(SaleorWebhookValidator.prototype, "validateRequest").mockResolvedValue({
result: "failure",
error: new Error("Test error"),
});
const saleorSyncWebhook = new SaleorSyncWebhook(webhookConfig);
const handler = vi.fn();
const wrappedHandler = saleorSyncWebhook.createHandler(handler);
const event = createLambdaEvent();
const response = await wrappedHandler(event, mockLambdaContext);
expect(response.statusCode).toBe(500);
expect(response.body).toContain("Unexpected error while handling request");
expect(handler).not.toHaveBeenCalled();
});
it("should use custom error format when provided", async () => {
const mockFormatErrorResponse = vi.fn().mockResolvedValue({
body: "Customized error message",
code: 418,
} as FormatWebhookErrorResult);
const error = new Error("Test error");
vi.spyOn(SaleorWebhookValidator.prototype, "validateRequest").mockResolvedValue({
result: "failure",
error,
});
const saleorSyncWebhook = new SaleorSyncWebhook({
...webhookConfig,
formatErrorResponse: mockFormatErrorResponse,
});
const handler = vi.fn();
const wrappedHandler = saleorSyncWebhook.createHandler(handler);
// Note: Events are not representative,
// we mock resolved value from webhook validator
const event = createLambdaEvent();
const response = await wrappedHandler(event, mockLambdaContext);
expect(mockFormatErrorResponse).toHaveBeenCalledWith(error, event);
expect(response.statusCode).toBe(418);
expect(response.body).toBe("Customized error message");
expect(handler).not.toHaveBeenCalled();
});
});
});