-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget.test.js
More file actions
117 lines (100 loc) · 3.72 KB
/
get.test.js
File metadata and controls
117 lines (100 loc) · 3.72 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
import { version as uuidVersion } from "uuid";
import setCookieParser from "set-cookie-parser";
import orchestrator from "tests/orchestrator.js";
import session from "models/session";
beforeAll(async () => {
await orchestrator.waitForAllServices();
await orchestrator.clearDatabase();
await orchestrator.runPendingMigrations();
});
describe("GET to api/v1/user", () => {
describe("Default user", () => {
test("With valid session", async () => {
const createdUser = await orchestrator.createUser({
username: "UserWithValidSession",
});
const sessionObject = await orchestrator.createSession(createdUser.id);
const response = await fetch("http://localhost:3000/api/v1/user", {
headers: {
Cookie: `session_id=${sessionObject.token}`,
},
});
expect(response.status).toBe(200);
const cacheControl = response.headers.get("Cache-Control");
expect(cacheControl).toBe(
"no-store, no-cache, max-age=0, must-revalidate",
);
const responseBody = await response.json();
expect(responseBody).toEqual({
id: createdUser.id,
username: "UserWithValidSession",
email: createdUser.email,
password: createdUser.password,
created_at: createdUser.created_at.toISOString(),
updated_at: createdUser.updated_at.toISOString(),
});
expect(uuidVersion(responseBody.id)).toBe(4);
expect(Date.parse(responseBody.created_at)).not.toBeNaN();
expect(Date.parse(responseBody.updated_at)).not.toBeNaN();
// Test session renewal
const renewedSessionObject = await session.findOneValidByToken(
sessionObject.token,
);
expect(renewedSessionObject.expires_at > sessionObject.expires_at).toBe(
true,
);
expect(renewedSessionObject.updated_at > sessionObject.updated_at).toBe(
true,
);
// Test Set-Cookie
const parsedSetCookie = setCookieParser(response, { map: true });
expect(parsedSetCookie.session_id).toEqual({
name: "session_id",
value: sessionObject.token,
maxAge: session.EXPIRATION_IN_MILLISECONDS / 1000,
path: "/",
httpOnly: true,
});
});
test("With nonexistent session", async () => {
const nonexistentToken =
"883f4782ed6c87d7f30ab3351f4614591ddeb148ae73a214d9f05b848d53f4377c973559aba52e907263835ba5dc7a97";
const response = await fetch("http://localhost:3000/api/v1/user", {
headers: {
Cookie: `session_id=${nonexistentToken}`,
},
});
expect(response.status).toBe(401);
const responseBody = await response.json();
expect(responseBody).toEqual({
name: "UnauthorizedError",
message: "User do not have an active session.",
action: "Verify if you are logged in and try again.",
status_code: 401,
});
});
test("With expired session", async () => {
jest.useFakeTimers({
now: new Date(Date.now() - session.EXPIRATION_IN_MILLISECONDS),
});
const createdUser = await orchestrator.createUser({
username: "UserWithExpiredSession",
});
const sessionObject = await orchestrator.createSession(createdUser.id);
jest.useRealTimers();
const response = await fetch("http://localhost:3000/api/v1/user", {
headers: {
Cookie: `session_id=${sessionObject.token}`,
},
});
expect(response.status).toBe(401);
const responseBody = await response.json();
expect(responseBody).toEqual({
name: "UnauthorizedError",
message: "User do not have an active session.",
action: "Verify if you are logged in and try again.",
status_code: 401,
});
});
});
});