Skip to content

Commit a655b83

Browse files
author
Matheus Ishiyama
authored
Refactored tests (#4)
2 parents 212723f + 1453e65 commit a655b83

18 files changed

+571
-715
lines changed

src/controllers/user.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const UserController = {
2727

2828
await sendMail(body.email, verifyCode, "Confirm register");
2929

30-
return res.status(201).json({ message: user });
30+
return res.status(201).json({ user });
3131
},
3232

3333
async confirm(req, res) {
@@ -38,7 +38,7 @@ const UserController = {
3838
{ verifyCode: "verified", verified: true }
3939
);
4040

41-
return res.status(200).json({ message: "User confirmed" });
41+
return res.status(200).json({ error: "User confirmed" });
4242
},
4343

4444
async addPending(req, res) {
@@ -57,7 +57,7 @@ const UserController = {
5757
{ $push: { requests: userId } }
5858
);
5959

60-
return res.status(200).json({ message: "Pending added" });
60+
return res.status(200).json({ error: "Pending added" });
6161
},
6262

6363
async removePending(req, res) {
@@ -76,7 +76,7 @@ const UserController = {
7676
{ $pull: { requests: userId } }
7777
);
7878

79-
return res.status(200).json({ message: "Pending removed" });
79+
return res.status(200).json({ error: "Pending removed" });
8080
},
8181

8282
async addFriend(req, res) {
@@ -101,7 +101,7 @@ const UserController = {
101101
}
102102
);
103103

104-
return res.status(200).json({ message: "Friend added" });
104+
return res.status(200).json({ error: "Friend added" });
105105
},
106106

107107
async removeFriend(req, res) {
@@ -120,7 +120,7 @@ const UserController = {
120120
{ $pull: { friendList: userId } }
121121
);
122122

123-
return res.status(200).json({ message: "Friend removed" });
123+
return res.status(200).json({ error: "Friend removed" });
124124
},
125125
};
126126

src/middlewares/authenticate.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ require("dotenv").config();
44
const authenticate = (req, res, next) => {
55
const authHeader = req.headers.authorization;
66
if (!authHeader)
7-
return res.status(401).json({ message: "No token provided" });
7+
return res.status(401).json({ error: "No token provided" });
88

99
const parts = authHeader.split(" ");
1010

1111
if (!(parts.length === 2))
12-
return res.status(401).json({ message: "Token error" });
12+
return res.status(401).json({ error: "Token error" });
1313

1414
const [scheme, token] = parts;
1515

1616
if (!/^Bearer$/i.test(scheme))
17-
return res.status(401).json({ message: "Token malformatted" });
17+
return res.status(401).json({ error: "Token malformatted" });
1818

1919
jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => {
20-
if (err) return res.status(401).json({ message: "Invalid token" });
20+
if (err) return res.status(401).json({ error: "Invalid token" });
2121

2222
req.userId = decoded.id;
2323
next();

src/middlewares/validateAuth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const validateAuth = (req, res, next) => {
22
if (!req.body.username)
3-
return res.status(400).json({ message: "No username provided" });
3+
return res.status(400).json({ error: "No username provided" });
44
if (!req.body.password)
5-
return res.status(400).json({ message: "No password provided" });
5+
return res.status(400).json({ error: "No password provided" });
66

77
next();
88
};

src/middlewares/validateCode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const validateCode = (req, res, next) => {
22
if (!req.params.verifyCode)
3-
return res.status(400).json({ message: "No verifyCode provided" });
3+
return res.status(400).json({ error: "No verifyCode provided" });
44

55
if (req.params.verifyCode.length !== 172)
6-
return res.status(400).json({ message: "No verifyCode length accept" });
6+
return res.status(400).json({ error: "No verifyCode length accept" });
77

88
next();
99
};

src/middlewares/validateFriend.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ const validateFriend = async (req, res, next) => {
44
const { friendId } = req.body;
55

66
if (!friendId)
7-
return res.status(400).json({ message: "No friendId provided" });
7+
return res.status(400).json({ error: "No friendId provided" });
88

99
try {
1010
const isValid = await User.findOne({ _id: friendId });
1111

1212
if (!isValid)
13-
return res.status(404).json({ message: "User not found" });
13+
return res.status(404).json({ error: "User not found" });
1414

1515
if (!isValid.verified)
16-
return res.status(404).json({ message: "User not found" });
16+
return res.status(404).json({ error: "User unverified" });
1717
} catch (error) {
18-
return res.status(404).json({ message: "User not found" });
18+
return res.status(404).json({ error: "User not found" });
1919
}
2020

2121
next();

src/middlewares/validateRegister.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,37 @@ const validateRegister = async (req, res, next) => {
99
if (!validateBody.username(username)) {
1010
return res
1111
.status(400)
12-
.json({ message: "No username provided or no length accept" });
12+
.json({ error: "No username provided or no length accept" });
1313
}
1414

1515
if (!validateBody.name(name))
1616
return res
1717
.status(400)
18-
.json({ message: "No name provided or no length accept" });
18+
.json({ error: "No name provided or no length accept" });
1919

2020
if (!EmailValidator.validate(email))
2121
return res
2222
.status(400)
23-
.json({ message: "No email provided or invalid format" });
23+
.json({ error: "No email provided or invalid format" });
2424

2525
if (!validateBody.password(password))
2626
return res
2727
.status(400)
28-
.json({ message: "No password provided or doesn't match params" });
28+
.json({ error: "No password provided or doesn't match params" });
2929

3030
if (!validateBody.checkPassword(password, checkPassword))
3131
return res.status(400).json({
32-
message: "No checkPassword provided or passwords doesn't match",
32+
error: "No checkPassword provided or passwords doesn't match",
3333
});
3434

3535
//* verify if user exists
36-
const userExists = await User.findOne({ username });
36+
const userExists = await User.findOne({ username: username.toLowerCase() });
3737
const emailExists = await User.findOne({ email });
3838

3939
if (userExists || emailExists)
4040
return res
4141
.status(400)
42-
.json({ message: "Username or Email already exists" });
42+
.json({ error: "Username or Email already exists" });
4343

4444
next();
4545
};

src/services/authenticate.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ require("dotenv").config();
55

66
const authenticate = async (username, password, res) => {
77
if (!username)
8-
return res.status(400).json({ message: "No username provided" });
8+
return res.status(400).json({ error: "No username provided" });
99

1010
if (!password)
11-
return res.status(400).json({ message: "No password provided" });
11+
return res.status(400).json({ error: "No password provided" });
1212

1313
const userExists = await User.findOne({ username });
1414

@@ -19,7 +19,7 @@ const authenticate = async (username, password, res) => {
1919
userExists.password = undefined;
2020

2121
if (!userExists.verified)
22-
return res.status(401).json({ message: "User unverified" });
22+
return res.status(401).json({ error: "User unverified" });
2323

2424
const token = jwt.sign(
2525
{ id: userExists._id },
@@ -31,9 +31,9 @@ const authenticate = async (username, password, res) => {
3131

3232
return res
3333
.status(401)
34-
.json({ message: "Invalid username or password" });
34+
.json({ error: "Invalid username or password" });
3535
}
36-
return res.status(401).json({ message: "Invalid username or password" });
36+
return res.status(401).json({ error: "Invalid username or password" });
3737
};
3838

3939
module.exports = authenticate;

src/services/sendMail.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ const transporter = nodeMailer.createTransport({
1111
});
1212

1313
const sendEmail = async (email, verifyCode, subject) => {
14-
if (!email) return "no email provided";
15-
if (!verifyCode) return "no verifyCode provided";
16-
if (!subject) return "no subject provided";
14+
if (!email) return "No email provided";
15+
if (!verifyCode) return "No verifyCode provided";
16+
if (!subject) return "No subject provided";
1717

1818
const isValid = validateBody.email(email);
19-
if (!isValid) return "invalid email type";
19+
if (!isValid) return "Invalid email format";
2020

2121
const mailOptions = {
2222
from: process.env.EMAIL,
@@ -27,7 +27,7 @@ const sendEmail = async (email, verifyCode, subject) => {
2727

2828
await transporter.sendMail(mailOptions);
2929

30-
return "email sent";
30+
return "Email sent";
3131
};
3232

3333
module.exports = sendEmail;

0 commit comments

Comments
 (0)