Step 4: Add password

Let's add a password field to the User model.

password: {
  type: String,
  required: true,
}

We can put more restrictions here like a minimum length. However, we will leave it at that. In fact, we should not store the password in the database. It is not a good practice! Instead, we should encode the password and store the code. We will explore this option shortly!

Let's upate the User.test.js as follows:

  • Everywhere that we call User.create, we must pass a password to it! Example:

    const name = faker.name.fullName();
    const email = faker.internet.email();
    const password = faker.internet.password();
    const user = await User.create({ name, email, password });
    
  • We need tests for cases where the password is null, undefined, empty, etc. Example:

    it("test password is null", async () => {
      try {
        const name = faker.name.fullName();
        const email = faker.internet.email();
        const password = null;
        await User.create({ name, email, password });
      } catch (err) {
        expect(err).toBeDefined();
      }
    });
    

Refer to the commit history to see the changes made at this step.