diff --git a/src/routes/user.controller.spec.ts b/src/routes/user.controller.spec.ts index 2de3d2d5..b9fe7ad7 100644 --- a/src/routes/user.controller.spec.ts +++ b/src/routes/user.controller.spec.ts @@ -2,6 +2,7 @@ import { createConnection, getConnection, Entity, getRepository } from 'typeorm' const userController = require('./user.controller'); import { User } from '../entity/user'; import { v4 as uuidv4 } from 'uuid'; +import { generateHash } from '../utilities/password.utility'; describe('User Controller', () => { // Mocks the Request Object that is returned const mockRequest = () => { @@ -221,4 +222,173 @@ describe('User Controller', () => { await userController.verify(verifyReq, res); expect(res.status).toHaveBeenCalledWith(400); }); + test('Update user password failure passwords do not match', async () => { + const req = mockRequest(); + const res = mockResponse(); + req.body = { + oldPassword: 'fakePassword', + newPassword: 'fakePassword2', + confirmNewPassword: 'fakePasswordDifferent' + }; + await userController.updateUserPassword(req, res); + expect(res.status).toHaveBeenCalledWith(400); + }); + test('Update user password failure same password', async () => { + const req = mockRequest(); + const res = mockResponse(); + req.body = { + oldPassword: 'fakePassword', + newPassword: 'fakePassword', + confirmNewPassword: 'fakePassword' + }; + await userController.updateUserPassword(req, res); + expect(res.status).toHaveBeenCalledWith(400); + }); + test('Update user password failure password validation', async () => { + const req = mockRequest(); + const res = mockResponse(); + req.body = { + oldPassword: '123', + newPassword: '234', + confirmNewPassword: '234' + }; + await userController.updateUserPassword(req, res); + expect(res.status).toHaveBeenCalledWith(400); + }); + test('Update user password success', async () => { + const req = mockRequest(); + const res = mockResponse(); + req.body = { + oldPassword: 'TangoDown123!!!', + newPassword: '9z4O4^HSvHkt3iU', + confirmNewPassword: '9z4O4^HSvHkt3iU' + }; + const existUser = new User(); + existUser.firstName = 'master'; + existUser.lastName = 'chief'; + existUser.email = 'testing@jest.com'; + existUser.active = true; + const uuid = uuidv4(); + existUser.uuid = uuid; + existUser.password = await generateHash('TangoDown123!!!'); + const userr = await getConnection().getRepository(User).insert(existUser); + req.user = userr.identifiers[0].id; + await userController.updateUserPassword(req, res); + expect(res.status).toHaveBeenCalledWith(200); + }); + test('Update user failure user does not exist', async () => { + const req = mockRequest(); + const res = mockResponse(); + req.body = { + oldPassword: 'TangoDown123!!!', + newPassword: '9z4O4^HSvHkt3iU', + confirmNewPassword: '9z4O4^HSvHkt3iU' + }; + const existUser = new User(); + existUser.firstName = 'master'; + existUser.lastName = 'chief'; + existUser.email = 'testing@jest.com'; + existUser.active = true; + const uuid = uuidv4(); + existUser.uuid = uuid; + existUser.password = await generateHash('TangoDown123!!!'); + const userr = await getConnection().getRepository(User).insert(existUser); + userr.identifiers[0].id = 117; + const x: any = 2; + req.user = x; + await userController.updateUserPassword(req, res); + expect(res.status).toHaveBeenCalledWith(400); + }); + test('patch user success', async () => { + const req = mockRequest(); + const res = mockResponse(); + req.body = { + firstName: 'Master', + lastName: 'Chief', + title: 'Spartan 117' + }; + const existUser = new User(); + existUser.firstName = 'Cortana'; + existUser.lastName = 'AI'; + existUser.email = 'testing@jest.com'; + existUser.title = 'A.I.'; + existUser.active = true; + const uuid = uuidv4(); + existUser.uuid = uuid; + existUser.password = await generateHash('TangoDown123!!!'); + const userr = await getConnection().getRepository(User).insert(existUser); + req.user = userr.identifiers[0].id; + await userController.patch(req, res); + expect(res.status).toHaveBeenCalledWith(200); + }); + test('get user failure user does not exist', async () => { + const req = mockRequest(); + const res = mockResponse(); + const existUser = new User(); + existUser.firstName = 'Cortana'; + existUser.lastName = 'AI'; + existUser.email = 'testing@jest.com'; + existUser.title = 'A.I.'; + existUser.active = true; + const uuid = uuidv4(); + existUser.uuid = uuid; + existUser.password = await generateHash('TangoDown123!!!'); + await getConnection().getRepository(User).insert(existUser); + const x: any = 2; + req.user = x; + await userController.getUser(req, res); + expect(res.status).toHaveBeenCalledWith(404); + }); + test('get user success', async () => { + const req = mockRequest(); + const res = mockResponse(); + const existUser = new User(); + existUser.firstName = 'Cortana'; + existUser.lastName = 'AI'; + existUser.email = 'testing@jest.com'; + existUser.title = 'A.I.'; + existUser.active = true; + const uuid = uuidv4(); + existUser.uuid = uuid; + existUser.password = await generateHash('TangoDown123!!!'); + const userr = await getConnection().getRepository(User).insert(existUser); + req.user = userr.identifiers[0].id; + await userController.getUser(req, res); + expect(res.status).toHaveBeenCalledWith(200); + }); + test('get users success', async () => { + const req = mockRequest(); + const res = mockResponse(); + await userController.getUsers(req, res); + expect(res.status).toHaveBeenCalledWith(200); + }); + test('get users by id success', async () => { + const req = mockRequest(); + const res = mockResponse(); + const user1 = new User(); + user1.firstName = 'Cortana'; + user1.lastName = 'AI'; + user1.email = 'testing1@jest.com'; + user1.title = 'A.I.'; + user1.active = true; + const uuid = uuidv4(); + user1.uuid = uuid; + user1.password = await generateHash('TangoDown123!!!'); + const user2 = new User(); + user2.firstName = 'Cortana'; + user2.lastName = 'AI'; + user2.email = 'testing2@jest.com'; + user2.title = 'A.I.'; + user2.active = true; + const uuid2 = uuidv4(); + user2.uuid = uuid2; + user2.password = await generateHash('TangoDown123!!!'); + const usrIdAry: number[] = []; + const insUser1 = await getConnection().getRepository(User).insert(user1); + usrIdAry.push(insUser1.identifiers[0].id); + const insUser2 = await getConnection().getRepository(User).insert(user2); + usrIdAry.push(insUser2.identifiers[0].id); + const retUserAry = await userController.getUsersById(usrIdAry); + expect(retUserAry).toHaveLength(2); + }); });