Step 14

Exercise Please add the more tests to auth.test.js to test the /register endpoint.

Solution
const mongoose = require("mongoose");
const supertest = require("supertest");
const app = require("../../server");
const UserDao = require("../../server/data/UserDao");

const users = new UserDao();
const request = supertest(app);

describe("Test authentication endpoints", () => {

  beforeAll(async () => {
    await mongoose.connect(global.__MONGO_URI__);
    await users.create({
      username: "testclient",
      password: "testclient",
      role: "CLIENT",
    });
  });

  describe("Test /authenticate", () => {
    test("Return 400 when username is missing", async () => {
      const response = await request.post("/authenticate").send({
        password: "testclient",
      });
      expect(response.status).toBe(400);
    });

    test("Return 400 when password is missing", async () => {
      const response = await request.post("/authenticate").send({
        username: "testclient",
      });
      expect(response.status).toBe(400);
    });

    test("Return 403 when username is incorrect", async () => {
      const response = await request.post("/authenticate").send({
        username: "client",
        password: "testclient",
      });
      expect(response.status).toBe(403);
    });

    test("Return 403 when password is incorrect", async () => {
      const response = await request.post("/authenticate").send({
        username: "testclient",
        password: "client",
      });
      expect(response.status).toBe(403);
    });

    test("Return 200 when authentication is sucessfull", async () => {
      const response = await request.post("/authenticate").send({
        username: "testclient",
        password: "testclient",
      });
      expect(response.status).toBe(200);
    });

    test("Return a JWT when authentication is sucessfull", async () => {
      const response = await request.post("/authenticate").send({
        username: "testclient",
        password: "testclient",
      });
      expect(response.body.token).toBeTruthy(); // exists and non empty!
    });
  });

  describe("Test /register", () => {
    test("Return 400 when username is missing", async () => {
      const response = await request.post("/register").send({
        password: "newtestclient",
      });
      expect(response.status).toBe(400);
    });

    test("Return 400 when password is missing", async () => {
      const response = await request.post("/register").send({
        username: "newtestclient",
      });
      expect(response.status).toBe(400);
    });

    test("Return 500 when username already exist", async () => {
      const response = await request.post("/register").send({
        username: "testclient",
        password: "testclient",
      });
      expect(response.status).toBe(500);
    });

    test("Return 201 when registeration is sucessfull", async () => {
      const response = await request.post("/register").send({
        username: "newtestclient",
        password: "newtestclient",
      });
      expect(response.status).toBe(201);
    });

    test("Return a JWT when registeration is sucessfull", async () => {
      const response = await request.post("/register").send({
        username: "anothernewtestclient",
        password: "anothernewtestclient",
      });
      expect(response.body.token).toBeTruthy(); // exists and non empty!
    });
  });


  afterAll(async () => {
    await mongoose.connection.close();
  });
});

Notice the following:

  1. I have moved the setup and teardown methods to the parent describe function. Therefore, both suited for testing /authenticate and /register will use these.
  2. I test for the case where the username already exists!
  3. I test for the /register to return a token due to successful registration. The API currently does not behave in that way. Therefore this test will fail. You can then refactor the handler for /register and rerun the test to see it pass.
  router.post("/register", async (req, res) => {
    try {
      const { username, password } = req.body;
-     const data = await users.create({ username, password, role: "CLIENT" });
+     const user = await users.create({ username, password, role: "CLIENT" });
+     const token = createToken(user);
-     res.status(201).json({ data });
+     return res.status(201).json({
+       message: "Registeration successful!",
+       token: token,
+     });
    } catch (err) {
      res.status(err.status || 500).json({ message: err.message });
    }
  });