-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Description
The HTTP end-points need to be tested to ensure the APIs are functioning as intended. This includes dependency injection via creating a mock database to perform and test the CRUD operations.
Tools needed
Sub-task
Some API endpoints in the Swagger docs do not have the correct response status codes and valid dummy req-res examples. This need to be fixed
For example in the /signup endpoint
authRouter.post(
"/signup",
[
body("email", "Email is not properly formatted").isEmail(),
body("username", "Username is too short").isLength({ min: 5 }),
body("password", "Password is too short, choose something strong").isLength(
{ min: 5 },
),
],
async (req: Request, res: Response) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res
.status(HttpStatusCode.LENGTH_REQUIRED)
.json({ errors: errors.array() });
}
/**
* @openapi
* '/api/signup':
* post:
* tags:
* - User
* summary: Signup a user
* requestBody:
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/CreateUserInput'
* responses:
* 200:
* description: Success
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/CreateUserResponse'
* 409:
* description: Conflict
* 400:
* description: Bad request
*/
const { name, username, email, password } = req.body;
if (!name || !username || !email || !password) {
return res
.status(HttpStatusCode.UNPROCESSABLE_ENTITY)
.json({ error: "Please add all the fields..." });
}
try {
const savedUser = await User.findOne({
$or: [{ email }, { username }],
});
if (savedUser) {
return res
.status(HttpStatusCode.UNPROCESSABLE_ENTITY)
.json({ error: "A user already exists with the email or username" });
}
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);
const user = new User({
name,
username,
email,
password: hashedPassword,
});
await user.save();
res.status(HttpStatusCode.OK).json({ success: true });
} catch (err) {
console.log(err);
res.status(HttpStatusCode.BAD_REQUEST).json({ success: false });
}
},
);The possible status codes that can be returned are :
- HttpStatusCode.LENGTH_REQUIRED
- HttpStatusCode.UNPROCESSABLE_ENTITY
- HttpStatusCode.BAD_REQUEST
- HttpStatusCode.OK
But the documentation mentions
* responses:
* 200:
* description: Success
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/CreateUserResponse'
* 409:
* description: Conflict
* 400:
* description: Bad request409 is not even present in this POST route. These inconsistencies need to be fixed
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels