Skip to content

Commit

Permalink
Added login route backend tests. Added disconnect to database after t…
Browse files Browse the repository at this point in the history
…est.
  • Loading branch information
FernFinder committed Apr 23, 2024
1 parent 21bbbb1 commit 46f012d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 23 deletions.
7 changes: 1 addition & 6 deletions backend/routes/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ var router = express.Router();
const bcrypt = require("bcryptjs");
const { createJWT } = require("../util/jwt.js");

router.get('/test', (req, res) => {
console.log("Hello World")
res.send('Response')
})

//The 'login' function. This
//The 'login' function. This logs in a user
router.post('/submit', async (req, res, next) => {
try {
//Get the request values, the email and plaintext password
Expand Down
79 changes: 79 additions & 0 deletions backend/tests/login.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const request = require("supertest");
const sinon = require('sinon');
const middleware = require('../middleware/auth.js');
const user = require('../models/user');
const mongoose = require('mongoose');
var app;

var newUser = {
email: '',
password: ''
};

beforeAll(async () => {
//Replace the middleware function with a fake function that passes the request through.
sinon.stub(middleware, "userVerification")
.callsFake(function userVerification(req, res, next) {
return next();
});

//You need to declare this after the fake function is made
app = require("../app.js");

});

afterAll(async () => {
//Restore middleware function to the original state
middleware.userVerification.restore();
mongoose.disconnect();
});

// test user creation
describe("POST /login/submit", () => {
it("it should not find a user", async () => {
// Check a user email that is not in database
// Do not create a user with this email as that will break this test
newUser.email = 'expectedfailureforbackendtesting3927492130@test.net';

// post request to create a new user
const response = await request(app).post("/login/submit").send(newUser);
// check if user was not found in response
expect(response.statusCode).toBe(404);
expect(response.body.message).toBe('User not found');
});

it("it should not find a password for user", async () => {
// Check a user email that is in database
newUser.email = 'backendtesting3927492130@test.net';

// post request to create a new user
const response = await request(app).post("/login/submit").send(newUser);
// check if correct user in reponse
expect(response.statusCode).toBe(404);
expect(response.body.message).toBe('Password not found');
});

it("it should login the user", async () => {
// Check a user email that is in database
newUser.email = 'backendtesting3927492130@test.net';
newUser.password = 'password';
// Post request to create a new user
const response = await request(app).post("/login/submit").send(newUser);
// check if correct user in reponse
expect(response.statusCode).toBe(200);
expect(response.body.message).toBe('Signed in successfully');
});

it("it should throw an error", async () => {
// Stub the findOne function to throw an error
sinon.stub(user , 'findOne')
.throws(new Error('Test error'));

const response = await request(app).post("/login/submit").send(null);
// check if error is caught
expect(response.statusCode).toBe(500);

//Restore findOne function
user.findOne.restore();
});
});
35 changes: 18 additions & 17 deletions backend/tests/users.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const request = require("supertest");
const sinon = require('sinon');
const middleware = require('../middleware/auth.js');
const mongoose = require('mongoose');
var app;

let userEmail = "john" + Math.random() + "@testing.com";
Expand All @@ -20,12 +21,12 @@ beforeAll(async () => {

//You need to declare this after the fake function is made
app = require("../app");

});

afterAll(async () => {
//Restore middleware function to the original state
middleware.userVerification.restore();
mongoose.disconnect;
});

/* ChatGPT4 assistance >> */
Expand Down Expand Up @@ -56,25 +57,25 @@ describe("GET /users/:email", () => {
// expect 404 response not found
expect(response.statusCode).toBe(404);
});
});

describe("DELETE /users/:email", () => {
it("should delete a user by id", async () => {
const response1 = await request(app).delete(`/users/${newUser.email}`);
// expect 200 response
expect(response1.statusCode).toBe(200);
expect(response1.body.message).toBe("User deleted successfully");
describe("DELETE /users/:email", () => {
it("should delete a user by id", async () => {
const response1 = await request(app).delete(`/users/${newUser.email}`);
// expect 200 response
expect(response1.statusCode).toBe(200);
expect(response1.body.message).toBe("User deleted successfully");

// Verify the user has been deleted from the database
const response2 = await request(app).get(`/users/${newUser.email}`);
expect(response2.statusCode).toBe(404);
});
// Verify the user has been deleted from the database
const response2 = await request(app).get(`/users/${newUser.email}`);
expect(response2.statusCode).toBe(404);
});

it("should return 404 for a non-existent user", async () => {
const response = await request(app).delete(`/users/${newUser.email}`);
// expect 404 response not found
expect(response.statusCode).toBe(404);
expect(response.body.message).toBe("User not found");
});
it("should return 404 for a non-existent user", async () => {
const response = await request(app).delete(`/users/${newUser.email}`);
// expect 404 response not found
expect(response.statusCode).toBe(404);
expect(response.body.message).toBe("User not found");
});
});

Expand Down

0 comments on commit 46f012d

Please sign in to comment.