Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added login route backend tests. #130

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading