Skip to content
Snippets Groups Projects
Commit da8a7b28 authored by Khalid, Rizwan (UG - Computer Science)'s avatar Khalid, Rizwan (UG - Computer Science)
Browse files

Added user service tests

parent 8798f968
Branches
No related tags found
2 merge requests!8CI/CD,!7Service tests
module.exports = {
presets: [['@babel/preset-env', { targets: { node: 'current' } }]],
};
\ No newline at end of file
......@@ -48,6 +48,8 @@ export const checkUserEmail = async (req, res) => {
try {
const user = await User.findOne({ email });
if (!user)
return res.status(404).json({ message: "User doesn't exist" });
return res.status(200).json(user);
} catch (error) {
......
......
......@@ -16,8 +16,12 @@ app.use("/user", userRoutes);
const PORT = 5001;
if (process.env.NODE_ENV !== "test") {
mongoose.connect(process.env.CONNECTION_URL, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => app.listen(PORT, "0.0.0.0", () => console.log(`Users service running on ${PORT}...`)))
.catch((error) => console.log(error.message));
mongoose.set("useFindAndModify", false);
}
export default app;
\ No newline at end of file
module.exports = {
testEnvironment: 'node'
};
\ No newline at end of file
Source diff could not be displayed: it is too large. Options to address this: view the blob.
......@@ -5,7 +5,8 @@
"main": "index.js",
"type": "module",
"scripts": {
"start": "node index.js"
"start": "node index.js",
"test": "jest"
},
"keywords": [],
"author": "",
......@@ -19,5 +20,13 @@
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.12.3"
},
"devDependencies": {
"@babel/core": "^7.14.3",
"@babel/preset-env": "^7.14.2",
"babel-jest": "^26.6.3",
"jest": "^26.6.3",
"mongodb-memory-server": "^6.9.6",
"supertest": "^6.1.3"
}
}
import mongoose from 'mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';
const mongoServer = new MongoMemoryServer();
const opts = {
useNewUrlParser: true,
useUnifiedTopology: true,
};
export const connect = async () => {
await mongoose.disconnect();
const mongoUri = await mongoServer.getUri();
await mongoose.connect(mongoUri, opts, err => {
if (err) {
console.error(err);
}
});
};
export const close = async () => {
await mongoose.disconnect();
await mongoServer.stop();
};
export const clear = async () => {
const collections = mongoose.connection.collections;
for (const key in collections) {
await collections[key].deleteMany();
}
};
import request from "supertest";
import bcrypt from 'bcryptjs';
import app from '../index.js';
import { connect, close, clear } from './db_setup.js';
process.env.NODE_ENV = "test";
process.env.SECRET_KEY = "test";
const agent = request.agent(app);
beforeAll(async (done) => {
await connect();
done();
});
beforeEach(async (done) => {
await clear();
done();
});
afterAll(async (done) => {
await close();
done();
});
describe('signup', () => {
it('should sign up user successfully', async (done) => {
const name = "Test User"
const email = "test@test.com";
const passwordPlain = "test123"
const password = await bcrypt.hash(passwordPlain, 12);
const res = await agent.post(`/user/signup`).send({ name, email, password });
const user = res.body;
expect(res.statusCode).toBe(200);
expect(user.name).toEqual(name);
expect(user.email).toEqual(email);
expect(user._id).toBeDefined();
done();
});
it('should not sign up existing email', async (done) => {
const name = "Test User"
const email = "test@test.com";
const passwordPlain = "test123"
const password = await bcrypt.hash(passwordPlain, 12);
await agent.post(`/user/signup`).send({ name, email, password });
const res = await agent.post(`/user/signup`).send({ name, email, password });
expect(res.statusCode).toBe(400);
done();
});
});
describe('signin', () => {
it('should successfully sign in', async (done) => {
const name = "Test User"
const email = "test@test.com";
const passwordPlain = "test123"
const password = await bcrypt.hash(passwordPlain, 12);
await agent.post(`/user/signup`).send({ name, email, password });
const res = await agent.post(`/user/signin`).send({ name, email, password: passwordPlain });
expect(res.statusCode).toBe(200);
done();
});
it('should not sign in non existent user', async (done) => {
const email = "doesnotexist@test.com";
const password = "test123";
const res = await agent.post(`/user/signin`).send({ email, password });
expect(res.statusCode).toBe(404);
done();
});
it('should not sign in incorrect password', async (done) => {
const name = "Test User"
const email = "test@test.com";
const passwordPlain = "test123"
const password = await bcrypt.hash(passwordPlain, 12);
await agent.post(`/user/signup`).send({ name, email, password });
const res = await agent.post(`/user/signin`).send({ email, password: "INCORRECT_PASSWORD" });
expect(res.statusCode).toBe(400);
done();
});
});
describe('get user by id', () => {
it('should successfully get user by id', async (done) => {
const name = "Test User"
const email = "test@test.com";
const passwordPlain = "test123"
const password = await bcrypt.hash(passwordPlain, 12);
const userRes = await agent.post(`/user/signup`).send({ name, email, password });
const user = userRes.body;
const res = await agent.get(`/user/user/${user._id}`);
const userFound = res.body;
expect(res.statusCode).toBe(200);
expect(userFound.name).toEqual(user.name);
expect(userFound.email).toEqual(user.email);
done();
});
it('should not find non existent user', async (done) => {
const res = await agent.get(`/user/user/1`);
expect(res.statusCode).toBe(500);
done();
});
});
describe('get user by email', () => {
it('should successfully get user by email', async (done) => {
const name = "Test User"
const email = "test@test.com";
const passwordPlain = "test123"
const password = await bcrypt.hash(passwordPlain, 12);
const userRes = await agent.post(`/user/signup`).send({ name, email, password });
const user = userRes.body;
const res = await agent.get(`/user/user/check/${user.email}`);
const userFound = res.body;
expect(res.statusCode).toBe(200);
expect(userFound.name).toEqual(user.name);
expect(userFound.email).toEqual(user.email);
done();
});
it('should not find non existent user', async (done) => {
const res = await agent.get(`/user/user/check/doesntexist@test.com`);
expect(res.statusCode).toBe(404);
done();
});
});
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment