-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcreate-manifest-handler.test.ts
114 lines (102 loc) · 3.71 KB
/
create-manifest-handler.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
import { describe, expect, it, vi } from "vitest";
import { SALEOR_SCHEMA_VERSION } from "@/const";
import { createManifestHandler, CreateManifestHandlerOptions } from "./create-manifest-handler";
import { createLambdaEvent, mockLambdaContext } from "./test-utils";
describe("AWS Lambda createManifestHandler", () => {
it("Creates a handler that responds with manifest, includes a request and baseUrl in factory method", async () => {
// Note: This event uses $default stage which means it's not included in the URL
// More details: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
// also see platform-adapter
const event = createLambdaEvent({
method: "GET",
path: "/manifest",
headers: {
"content-type": "application/json",
host: "some-app-host.cloud",
"x-forwarded-proto": "https",
[SALEOR_SCHEMA_VERSION]: "3.20",
},
});
const expectedBaseUrl = "https://some-app-host.cloud";
const mockManifestFactory = vi
.fn<CreateManifestHandlerOptions["manifestFactory"]>()
.mockImplementation(({ appBaseUrl }) => ({
name: "Test app",
tokenTargetUrl: `${appBaseUrl}/api/register`,
appUrl: appBaseUrl,
permissions: [],
id: "app-id",
version: "1",
}));
const handler = createManifestHandler({
manifestFactory: mockManifestFactory,
});
const response = await handler(event, mockLambdaContext);
expect(mockManifestFactory).toHaveBeenCalledWith(
expect.objectContaining({
appBaseUrl: expectedBaseUrl,
request: event,
schemaVersion: [3, 20],
}),
);
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body!)).toStrictEqual({
appUrl: expectedBaseUrl,
id: "app-id",
name: "Test app",
permissions: [],
tokenTargetUrl: `${expectedBaseUrl}/api/register`,
version: "1",
});
});
it("Works with event that has AWS Lambda stage", async () => {
// Note: AWS lambda uses stages which are passed in lambda request context
// Contexts are appended to the lambda base URL, like so: <baseUrl>/<stage>
// In this case we're simulating test stage, which results in <baseUrl>/test
// More details: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
const event = createLambdaEvent({
method: "GET",
path: "/manifest",
headers: {
"content-type": "application/json",
host: "some-app-host.cloud",
"x-forwarded-proto": "https",
[SALEOR_SCHEMA_VERSION]: "3.20",
},
requestContext: {
stage: "test",
},
});
const expectedBaseUrl = "https://some-app-host.cloud/test";
const mockManifestFactory = vi
.fn<CreateManifestHandlerOptions["manifestFactory"]>()
.mockImplementation(({ appBaseUrl }) => ({
name: "Test app",
tokenTargetUrl: `${appBaseUrl}/api/register`,
appUrl: appBaseUrl,
permissions: [],
id: "app-id",
version: "1",
}));
const handler = createManifestHandler({
manifestFactory: mockManifestFactory,
});
const response = await handler(event, mockLambdaContext);
expect(mockManifestFactory).toHaveBeenCalledWith(
expect.objectContaining({
appBaseUrl: expectedBaseUrl,
request: event,
schemaVersion: [3, 20],
}),
);
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body!)).toStrictEqual({
appUrl: expectedBaseUrl,
id: "app-id",
name: "Test app",
permissions: [],
tokenTargetUrl: `${expectedBaseUrl}/api/register`,
version: "1",
});
});
});