-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcreate-app-register-handler.test.ts
173 lines (147 loc) · 5.28 KB
/
create-app-register-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
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
172
173
import type { APIGatewayProxyEventV2 } from "aws-lambda";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { AuthData } from "@/APL";
import { SALEOR_API_URL_HEADER } from "@/const";
import * as fetchRemoteJwksModule from "@/fetch-remote-jwks";
import * as getAppIdModule from "@/get-app-id";
import { MockAPL } from "@/test-utils/mock-apl";
import {
createAppRegisterHandler,
CreateAppRegisterHandlerOptions,
} from "./create-app-register-handler";
import { createLambdaEvent, mockLambdaContext } from "./test-utils";
describe("AWS Lambda createAppRegisterHandler", () => {
const mockJwksValue = "{}";
const mockAppId = "42";
const saleorApiUrl = "https://mock-saleor-domain.saleor.cloud/graphql/";
const authToken = "mock-auth-token";
vi.spyOn(fetchRemoteJwksModule, "fetchRemoteJwks").mockResolvedValue(mockJwksValue);
vi.spyOn(getAppIdModule, "getAppId").mockResolvedValue(mockAppId);
let mockApl: MockAPL;
let event: APIGatewayProxyEventV2;
beforeEach(() => {
mockApl = new MockAPL();
event = createLambdaEvent({
body: JSON.stringify({ auth_token: authToken }),
headers: {
"content-type": "application/json",
host: "mock-slaeor-domain.saleor.cloud",
"x-forwarded-proto": "https",
[SALEOR_API_URL_HEADER]: saleorApiUrl,
},
});
});
it("Sets auth data for correct Lambda event", async () => {
const handler = createAppRegisterHandler({ apl: mockApl });
const response = await handler(event, mockLambdaContext);
expect(response.statusCode).toBe(200);
expect(mockApl.set).toHaveBeenCalledWith({
saleorApiUrl,
token: authToken,
appId: mockAppId,
jwks: mockJwksValue,
});
});
it("Returns 403 for prohibited Saleor URLs in Lambda event", async () => {
event.headers[SALEOR_API_URL_HEADER] = "https://wrong-domain.saleor.cloud/graphql/";
const handler = createAppRegisterHandler({
apl: mockApl,
allowedSaleorUrls: [(url) => url === "https://correct-domain.saleor.cloud"],
});
const response = await handler(event, mockLambdaContext);
const body = JSON.parse(response.body!);
expect(response.statusCode).toBe(403);
expect(body.success).toBe(false);
});
it("Handles invalid JSON bodies in Lambda event", async () => {
event.body = "{ ";
const handler = createAppRegisterHandler({ apl: mockApl });
const response = await handler(event, mockLambdaContext);
expect(response.statusCode).toBe(400);
expect(response.body).toBe("Invalid request json.");
});
describe("Lambda callback hooks", () => {
const expectedAuthData: AuthData = {
token: authToken,
saleorApiUrl,
jwks: mockJwksValue,
appId: mockAppId,
};
it("Triggers success callbacks with Lambda event context", async () => {
const mockOnRequestStart = vi.fn();
const mockOnRequestVerified = vi.fn();
const mockOnAuthAplFailed = vi.fn();
const mockOnAuthAplSaved = vi.fn();
const handler = createAppRegisterHandler({
apl: mockApl,
onRequestStart: mockOnRequestStart,
onRequestVerified: mockOnRequestVerified,
onAplSetFailed: mockOnAuthAplFailed,
onAuthAplSaved: mockOnAuthAplSaved,
});
await handler(event, mockLambdaContext);
expect(mockOnRequestStart).toHaveBeenCalledWith(
event,
expect.objectContaining({
authToken,
saleorApiUrl,
})
);
expect(mockOnRequestVerified).toHaveBeenCalledWith(
event,
expect.objectContaining({
authData: expectedAuthData,
})
);
expect(mockOnAuthAplSaved).toHaveBeenCalledWith(
event,
expect.objectContaining({
authData: expectedAuthData,
})
);
expect(mockOnAuthAplFailed).not.toHaveBeenCalled();
});
it("Triggers failure callback when APL save fails", async () => {
const mockOnAuthAplFailed = vi.fn();
const mockOnAuthAplSaved = vi.fn();
mockApl.set.mockRejectedValueOnce(new Error("Save failed"));
const handler = createAppRegisterHandler({
apl: mockApl,
onAplSetFailed: mockOnAuthAplFailed,
onAuthAplSaved: mockOnAuthAplSaved,
});
await handler(event, mockLambdaContext);
expect(mockOnAuthAplFailed).toHaveBeenCalledWith(
event,
expect.objectContaining({
error: expect.any(Error),
authData: expectedAuthData,
})
);
});
it("Allows custom error responses via hooks", async () => {
const mockOnRequestStart = vi
.fn<NonNullable<CreateAppRegisterHandlerOptions["onRequestStart"]>>()
.mockImplementation((_req, context) =>
context.respondWithError({
status: 401,
message: "test message",
})
);
const handler = createAppRegisterHandler({
apl: mockApl,
onRequestStart: mockOnRequestStart,
});
const response = await handler(event, mockLambdaContext);
expect(response.statusCode).toBe(401);
expect(JSON.parse(response.body!)).toStrictEqual({
error: {
code: "REGISTER_HANDLER_HOOK_ERROR",
message: "test message",
},
success: false,
});
expect(mockOnRequestStart).toHaveBeenCalled();
});
});
});