-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathuser.test.js
More file actions
44 lines (39 loc) · 1.24 KB
/
user.test.js
File metadata and controls
44 lines (39 loc) · 1.24 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
import test from "node:test";
import assert from "node:assert/strict";
import { createApp } from "../app.js";
async function withServer(run) {
const app = createApp();
const server = app.listen(0);
await new Promise((resolve, reject) => {
server.once("listening", resolve);
server.once("error", reject);
});
try {
const { port } = server.address();
await run(`http://127.0.0.1:${port}`);
} finally {
await new Promise((resolve, reject) => {
server.close((error) => (error ? reject(error) : resolve()));
});
}
}
test("POST /api/users ignores caller supplied id", async () => {
await withServer(async (baseUrl) => {
const response = await fetch(`${baseUrl}/api/users`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
id: "usr_attacker",
email: "user@example.com",
name: "User",
}),
});
const payload = await response.json();
assert.equal(response.status, 201);
assert.equal(payload.success, true);
assert.match(payload.data.id, /^usr_\d+$/);
assert.notEqual(payload.data.id, "usr_attacker");
assert.equal(payload.data.email, "user@example.com");
assert.equal(payload.data.name, "User");
});
});