-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.test.js
More file actions
119 lines (102 loc) · 3.71 KB
/
delete.test.js
File metadata and controls
119 lines (102 loc) · 3.71 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
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("DELETE to api/v1/sessions", () => {
describe("Default user", () => {
test("With nonexistent session", async () => {
const nonexistentToken =
"883f4782ed6c87d7f30ab3351f4614591ddeb148ae73a214d9f05b848d53f4377c973559aba52e907263835ba5dc7a97";
const response = await fetch("http://localhost:3000/api/v1/sessions", {
method: "DELETE",
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();
const sessionObject = await orchestrator.createSession(createdUser.id);
jest.useRealTimers();
const response = await fetch("http://localhost:3000/api/v1/sessions", {
method: "DELETE",
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,
});
});
test("With valid session", async () => {
const createdUser = await orchestrator.createUser();
const sessionObject = await orchestrator.createSession(createdUser.id);
const response = await fetch("http://localhost:3000/api/v1/sessions", {
method: "DELETE",
headers: {
Cookie: `session_id=${sessionObject.token}`,
},
});
expect(response.status).toBe(200);
const responseBody = await response.json();
expect(responseBody).toEqual({
id: sessionObject.id,
token: sessionObject.token,
user_id: sessionObject.user_id,
expires_at: responseBody.expires_at,
created_at: responseBody.created_at,
updated_at: responseBody.updated_at,
});
expect(uuidVersion(responseBody.id)).toBe(4);
expect(Date.parse(responseBody.expires_at)).not.toBeNaN();
expect(Date.parse(responseBody.created_at)).not.toBeNaN();
expect(Date.parse(responseBody.updated_at)).not.toBeNaN();
expect(
responseBody.expires_at < sessionObject.expires_at.toISOString(),
).toBe(true);
expect(
responseBody.updated_at > sessionObject.updated_at.toISOString(),
).toBe(true);
// Test Set-Cookie
const parsedSetCookie = setCookieParser(response, { map: true });
expect(parsedSetCookie.session_id).toEqual({
name: "session_id",
value: "invalid",
maxAge: -1,
path: "/",
httpOnly: true,
});
// Doube check
const doubleCheckResponse = await fetch(
"http://localhost:3000/api/v1/user",
{
headers: {
Cookie: `session_id=${sessionObject.token}`,
},
},
);
expect(doubleCheckResponse.status).toBe(401);
});
});
});