-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.test.js
More file actions
133 lines (120 loc) · 4.12 KB
/
post.test.js
File metadata and controls
133 lines (120 loc) · 4.12 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
import orchestrator from "tests/orchestrator.js";
import { version as uuidVersion } from "uuid";
import user from "models/user.js";
import password from "models/password.js";
beforeAll(async () => {
await orchestrator.waitForAllServices();
await orchestrator.clearDatabase();
await orchestrator.runPendingMigrations();
});
describe("POST to api/v1/users", () => {
describe("Anonymous user", () => {
test("With unique and valid data", async () => {
// Create user and test it's values
const response = await fetch("http://localhost:3000/api/v1/users", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
username: "isaac",
email: "contato@isaacmuniz.pro",
password: "senha123",
}),
});
expect(response.status).toBe(201);
const responseBody = await response.json();
expect(responseBody).toEqual({
id: responseBody.id,
username: "isaac",
email: "contato@isaacmuniz.pro",
password: responseBody.password,
created_at: responseBody.created_at,
updated_at: responseBody.updated_at,
});
expect(uuidVersion(responseBody.id)).toBe(4);
expect(Date.parse(responseBody.created_at)).not.toBeNaN();
expect(Date.parse(responseBody.updated_at)).not.toBeNaN();
// Check if user's password was correctly hased
const userInDatabase = await user.findOneByUsername("isaac");
const correctPasswordMatch = await password.compare(
"senha123",
userInDatabase.password,
);
const incorrectPasswordMatch = await password.compare(
"senhaErrada",
userInDatabase.password,
);
expect(correctPasswordMatch).toBe(true);
expect(incorrectPasswordMatch).toBe(false);
});
test("With duplicated 'email'", async () => {
const response1 = await fetch("http://localhost:3000/api/v1/users", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
username: "email_duplicado1",
email: "duplicado@isaacmuniz.pro",
password: "senha123",
}),
});
expect(response1.status).toBe(201);
const response2 = await fetch("http://localhost:3000/api/v1/users", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
username: "email_duplicado2",
// same email with uppercase letters
email: "Duplicado@IsaacMuniz.Pro",
password: "senha123",
}),
});
expect(response2.status).toBe(400);
const response2Body = await response2.json();
expect(response2Body).toEqual({
name: "ValidationError",
message: "The email address provided is already in use.",
action: "Please use a different email.",
status_code: 400,
});
});
test("With duplicated 'username'", async () => {
const response1 = await fetch("http://localhost:3000/api/v1/users", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
username: "username_duplicado",
email: "username_duplicado1@isaacmuniz.pro",
password: "senha123",
}),
});
expect(response1.status).toBe(201);
const response2 = await fetch("http://localhost:3000/api/v1/users", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
// same username with uppercase letters
username: "Username_Duplicado",
email: "username_duplicado2@isaacmuniz.pro",
password: "senha123",
}),
});
expect(response2.status).toBe(400);
const response2Body = await response2.json();
expect(response2Body).toEqual({
name: "ValidationError",
message: "The username provided is already in use.",
action: "Please use a different username.",
status_code: 400,
});
});
});
});