-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcreate-app-register-handler.test.ts
184 lines (158 loc) · 5.35 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
174
175
176
177
178
179
180
181
182
183
184
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";
describe("Fetch API 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 request: Request;
beforeEach(() => {
mockApl = new MockAPL();
request = new Request("https://example.com", {
method: "POST",
headers: {
"Content-Type": "application/json",
Host: "mock-slaeor-domain.saleor.cloud",
"X-Forwarded-Proto": "https",
[SALEOR_API_URL_HEADER]: saleorApiUrl,
},
body: JSON.stringify({ auth_token: authToken }),
});
});
it("Sets auth data for correct request", async () => {
const handler = createAppRegisterHandler({ apl: mockApl });
const response = await handler(request);
expect(response.status).toBe(200);
expect(mockApl.set).toHaveBeenCalledWith({
saleorApiUrl,
token: authToken,
appId: mockAppId,
jwks: mockJwksValue,
});
});
it("Returns 403 for prohibited Saleor URLs", async () => {
request.headers.set(SALEOR_API_URL_HEADER, "https://wrong-domain.saleor.cloud/graphql/");
const handler = createAppRegisterHandler({
apl: mockApl,
allowedSaleorUrls: [saleorApiUrl],
});
const response = await handler(request);
const data = await response.json();
expect(response.status).toBe(403);
expect(data.success).toBe(false);
});
it("Handles invalid JSON bodies", async () => {
const brokenRequest = new Request("https://example.com", {
method: "POST",
headers: {
"Content-Type": "application/json",
Host: "mock-slaeor-domain.saleor.cloud",
"X-Forwarded-Proto": "https",
[SALEOR_API_URL_HEADER]: saleorApiUrl,
},
body: "{ ",
});
const handler = createAppRegisterHandler({
apl: mockApl,
allowedSaleorUrls: [saleorApiUrl],
});
const response = await handler(brokenRequest);
expect(response.status).toBe(400);
await expect(response.text()).resolves.toBe("Invalid request json.");
});
describe("Callback hooks", () => {
const expectedAuthData: AuthData = {
token: authToken,
saleorApiUrl,
jwks: mockJwksValue,
appId: mockAppId,
};
it("Triggers success callbacks when APL save succeeds", 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(request);
expect(mockOnRequestStart).toHaveBeenCalledWith(
request,
expect.objectContaining({
authToken,
saleorApiUrl,
})
);
expect(mockOnRequestVerified).toHaveBeenCalledWith(
request,
expect.objectContaining({
authData: expectedAuthData,
})
);
expect(mockOnAuthAplSaved).toHaveBeenCalledWith(
request,
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(request);
expect(mockOnAuthAplFailed).toHaveBeenCalledWith(
request,
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(request);
expect(response.status).toBe(401);
await expect(response.json()).resolves.toStrictEqual({
error: {
code: "REGISTER_HANDLER_HOOK_ERROR",
message: "test message",
},
success: false,
});
expect(mockOnRequestStart).toHaveBeenCalled();
});
});
});