Skip to content

add tests for the ts-backend api endpoints #44

@sanam2405

Description

@sanam2405

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 request

409 is not even present in this POST route. These inconsistencies need to be fixed

@aanu2021

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions