-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathjobRoutes.test.js
More file actions
71 lines (62 loc) · 1.97 KB
/
jobRoutes.test.js
File metadata and controls
71 lines (62 loc) · 1.97 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
import test from "node:test";
import assert from "node:assert/strict";
import { createApp } from "../app.js";
import { signAccessToken } from "../utils/jwt.js";
const validJobPayload = {
title: "Senior Node.js Developer",
description: "Build and maintain Express APIs for a production app.",
budgetMin: 50,
budgetMax: 100,
categoryId: "web-development",
skills: ["node.js", "express"]
};
async function withServer(callback) {
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 callback(`http://127.0.0.1:${port}`);
} finally {
await new Promise((resolve, reject) => {
server.close((error) => (error ? reject(error) : resolve()));
});
}
}
test("POST /api/jobs rejects unauthenticated requests", async () => {
await withServer(async (baseUrl) => {
const response = await fetch(`${baseUrl}/api/jobs`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(validJobPayload)
});
const payload = await response.json();
assert.equal(response.status, 401);
assert.equal(payload.success, false);
assert.equal(payload.message, "Unauthorized");
});
});
test("POST /api/jobs accepts authenticated requests", async () => {
await withServer(async (baseUrl) => {
const token = signAccessToken({
id: "user_123",
email: "client@example.com",
role: "client"
});
const response = await fetch(`${baseUrl}/api/jobs`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`
},
body: JSON.stringify(validJobPayload)
});
const payload = await response.json();
assert.equal(response.status, 201);
assert.equal(payload.success, true);
assert.equal(payload.data.title, validJobPayload.title);
});
});