-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.test.js
More file actions
139 lines (124 loc) · 4.43 KB
/
post.test.js
File metadata and controls
139 lines (124 loc) · 4.43 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
import { version as uuidVersion } from "uuid";
import setCookieParser from "set-cookie-parser";
import orchestrator from "tests/orchestrator.js";
import session from "models/session.js";
beforeAll(async () => {
await orchestrator.waitForAllServices();
await orchestrator.clearDatabase();
await orchestrator.runPendingMigrations();
});
describe("POST to api/v1/sessions", () => {
describe("Anonymous user", () => {
test("With incorrect 'email' but correct 'password'", async () => {
await orchestrator.createUser({
password: "correct-password",
});
const response = await fetch("http://localhost:3000/api/v1/sessions", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: "wrong.email@isaacmuniz.pro",
password: "correct-password",
}),
});
expect(response.status).toBe(401);
const responseBody = await response.json();
expect(responseBody).toEqual({
name: "UnauthorizedError",
message: "Credentials were not accepted.",
action: "Check if the data you submitted is correct.",
status_code: 401,
});
});
test("With correct 'email' but incorrect 'password'", async () => {
await orchestrator.createUser({
email: "correct.email@isaacmuniz.pro",
});
const response = await fetch("http://localhost:3000/api/v1/sessions", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: "correct.email@isaacmuniz.pro",
password: "wrong-password",
}),
});
expect(response.status).toBe(401);
const responseBody = await response.json();
expect(responseBody).toEqual({
name: "UnauthorizedError",
message: "Credentials were not accepted.",
action: "Check if the data you submitted is correct.",
status_code: 401,
});
});
test("With incorrect 'email' and incorrect 'password'", async () => {
await orchestrator.createUser();
const response = await fetch("http://localhost:3000/api/v1/sessions", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: "wrong.email@isaacmuniz.pro",
password: "wrong-password",
}),
});
expect(response.status).toBe(401);
const responseBody = await response.json();
expect(responseBody).toEqual({
name: "UnauthorizedError",
message: "Credentials were not accepted.",
action: "Check if the data you submitted is correct.",
status_code: 401,
});
});
test("With correct 'email' and correct 'password'", async () => {
const createdUser = await orchestrator.createUser({
email: "all.correct@isaacmuniz.pro",
password: "allCorrect",
});
const response = await fetch("http://localhost:3000/api/v1/sessions", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: "all.correct@isaacmuniz.pro",
password: "allCorrect",
}),
});
expect(response.status).toBe(201);
const responseBody = await response.json();
expect(responseBody).toEqual({
id: responseBody.id,
token: responseBody.token,
user_id: createdUser.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();
const expiresAt = new Date(responseBody.expires_at);
const createdAt = new Date(responseBody.created_at);
expiresAt.setMilliseconds(0);
createdAt.setMilliseconds(0);
expect(expiresAt - createdAt).toBe(session.EXPIRATION_IN_MILLISECONDS);
// Test cookie
const parsedSetCookie = setCookieParser(response, { map: true });
expect(parsedSetCookie.session_id).toEqual({
name: "session_id",
value: responseBody.token,
maxAge: session.EXPIRATION_IN_MILLISECONDS / 1000,
path: "/",
httpOnly: true,
});
});
});
});