-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathrules.test.ts
More file actions
306 lines (265 loc) · 8.88 KB
/
rules.test.ts
File metadata and controls
306 lines (265 loc) · 8.88 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { describe, it, beforeEach, afterEach, expect } from "vitest";
import { simulation } from "../src/index.ts";
import type { FoundationSimulatorListening } from "@simulacrum/foundation-simulator";
import jwt from "jsonwebtoken";
import { assert } from "assert-ts";
import { frontendSimulation } from "./helpers.ts";
let Fields = {
audience: "https://example.nl",
client_id: "00000000000000000000000000000000",
connection: "Username-Password-Authentication",
nonce: "aGV6ODdFZjExbF9iMkdYZHVfQ3lYcDNVSldGRDR6dWdvREQwUms1Z0Ewaw==",
redirect_uri: "http://localhost:3000",
response_type: "token id_token",
scope: "openid profile email offline_access",
state: "sxmRf2Fq.IMN5SER~wQqbsXl5Hx0JHov",
tenant: "localhost:4400",
};
type FixtureDirectories =
| "user"
| "access-token"
| "user-dependent"
| "async-only"
| "sync-wrapper-with-async";
type Fixtures = `test/fixtures/rules-${FixtureDirectories}`;
let person = {
name: "Paul Waters",
email: "paulwaters.white@yahoo.com",
password: "12345",
};
let basePort = 4420;
let host = "https://localhost";
let auth0Url = `${host}:${basePort}`;
async function createSimulation(
rulesDirectory: Fixtures,
initialPerson?: Partial<typeof person>
) {
const frontendServer = await frontendSimulation.listen();
const simulationPerson = { ...person, ...initialPerson };
const app = simulation({
initialState: {
users: [simulationPerson],
},
options: { rulesDirectory },
});
let server = await app.listen(basePort);
// prime the server with the nonce field
await fetch(`${auth0Url}/usernamepassword/login`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
...Fields,
username: simulationPerson.email,
password: simulationPerson.password,
}),
});
let res: Response = await fetch(`${auth0Url}/login/callback`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: `wctx=${encodeURIComponent(
JSON.stringify({
...Fields,
})
)}`,
});
let code = new URL(res.url).searchParams.get("code") as string;
await frontendServer.ensureClose();
return { code, server };
}
describe("rules", () => {
describe("accessToken claims", () => {
let code: string;
let server: FoundationSimulatorListening<unknown>;
beforeEach(async ({ task: _task }) => {
({ code, server } = await createSimulation(
"test/fixtures/rules-access-token"
));
});
afterEach(async () => {
await server.ensureClose();
});
it("should have added the claims", async () => {
let res: Response = await fetch(`${auth0Url}/oauth/token`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
...Fields,
code,
}),
});
expect(res.ok).toBe(true);
let token = (await res.json()) as unknown as { access_token: string };
const accessToken = jwt.decode(token.access_token, {
complete: true,
}) as unknown as { payload: Record<string, unknown> };
assert(!!accessToken);
expect(accessToken.payload["https://example.nl/roles"]).toEqual([
"example",
]);
expect(accessToken.payload["https://example.nl/email"]).toEqual(
"paulwaters.white@yahoo.com"
);
});
});
describe("augment user", () => {
let code: string;
let server: FoundationSimulatorListening<unknown>;
beforeEach(async () => {
({ code, server } = await createSimulation("test/fixtures/rules-user"));
});
afterEach(async () => {
await server.ensureClose();
});
it("should have added a picture to the payload", async () => {
let res: Response = await fetch(`${auth0Url}/oauth/token`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
...Fields,
code,
...person,
}),
});
let token = (await res.json()) as unknown as { id_token: string };
let idToken = jwt.decode(token.id_token, {
complete: true,
}) as unknown as { payload: Record<string, unknown> };
expect(idToken.payload.picture).toContain("https://i.pravatar.cc");
expect(idToken.payload.name).toBe(person.name);
});
});
describe("rely on user data", () => {
it("should trust Fred", async () => {
const otherPerson = {
name: "Fred Waters",
email: "fred@yahoo.com",
};
let { code, server } = await createSimulation(
"test/fixtures/rules-user-dependent",
otherPerson
);
let res: Response = await fetch(`${auth0Url}/oauth/token`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
...Fields,
code,
...otherPerson,
}),
});
const token = (await res.json()) as unknown as { id_token: string };
let idToken = jwt.decode(token.id_token, {
complete: true,
}) as unknown as { payload: Record<string, unknown> };
expect(idToken.payload.name).toBe(otherPerson.name);
expect(idToken.payload.trustProfile).toContain("friend");
await server.ensureClose();
});
it("should distrust Mark", async () => {
const otherPerson = {
name: "Mark Wahl",
email: "mark@yahoo.com",
};
let { code, server } = await createSimulation(
"test/fixtures/rules-user-dependent",
otherPerson
);
let res: Response = await fetch(`${auth0Url}/oauth/token`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
...Fields,
code,
...otherPerson,
}),
});
const token = (await res.json()) as unknown as { id_token: string };
let idToken = jwt.decode(token.id_token, {
complete: true,
}) as unknown as { payload: Record<string, unknown> };
expect(idToken.payload.name).toBe(otherPerson.name);
expect(idToken.payload.trustProfile).toContain("foe");
await server.ensureClose();
});
});
describe("async rule resolution", () => {
// nested describe give it an extra task for time to actually release the port
describe("async top level", () => {
it("should resolve async top level", async () => {
let { code, server } = await createSimulation(
"test/fixtures/rules-async-only",
person
);
let res: Response = await fetch(`${auth0Url}/oauth/token`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
...Fields,
code,
...person,
}),
});
const token = (await res.json()) as unknown as { id_token: string };
const idToken = jwt.decode(token.id_token, {
complete: true,
}) as unknown as { payload: Record<string, unknown> };
expect(idToken.payload.name).toBe(person.name);
expect(idToken?.payload.checkURLOne).toBe("https://frontside.com");
expect(idToken?.payload.checkURLOneStatus).toBe(200);
expect(idToken?.payload.checkURLOneText).toBe("frontside");
expect(idToken?.payload.checkURLTwo).toBe(
"https://frontside.com/effection"
);
expect(idToken?.payload.checkURLTwoStatus).toBe(200);
expect(idToken?.payload.checkURLTwoText).toBe("effection");
await server.ensureClose();
});
});
describe("sync top level", () => {
it("should resolve async nested in sync wrapper", async () => {
let { code, server } = await createSimulation(
"test/fixtures/rules-sync-wrapper-with-async"
);
let res: Response = await fetch(`${auth0Url}/oauth/token`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
...Fields,
code,
...person,
}),
});
const token = (await res.json()) as unknown as { id_token: string };
const idToken = jwt.decode(token.id_token, {
complete: true,
}) as unknown as { payload: Record<string, unknown> };
expect(idToken.payload.name).toBe(person.name);
expect(idToken?.payload.checkURLOne).toBe("https://frontside.com");
expect(idToken?.payload.checkURLOneStatus).toBe(200);
expect(idToken?.payload.checkURLOneText).toBe("frontside");
expect(idToken?.payload.checkURLTwo).toBe(
"https://frontside.com/effection"
);
expect(idToken?.payload.checkURLTwoStatus).toBe(200);
expect(idToken?.payload.checkURLTwoText).toBe("effection");
await server.ensureClose();
});
});
});
});