-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #912 from OneCommunityGlobal/Anirudh_loginmethod_u…
…nittest Anirudh loginmethod unittest
- Loading branch information
Showing
5 changed files
with
276 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Check mark: ✅ | ||
Cross Mark: ❌ | ||
|
||
# GetUser | ||
|
||
> ## Positive case | ||
1. ❌ Receives a POST request in the **/api/userProfile** route | ||
2. ✅ Returns **200**, with the requestor body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Check mark: ✅ | ||
Cross Mark: ❌ | ||
|
||
# login | ||
|
||
> ## Positive case | ||
1. ❌ Receives a POST request in the **/api/userProfile** route | ||
2. ✅ Returns 200, if the user is a new user and there is a password match | ||
3. ✅ Returns 200, if the user already exists and the password is a match | ||
|
||
## Negative case | ||
|
||
1. ✅ Returns error 400 if there is no email or password | ||
2. ✅ Returns error 403 if there is no user | ||
3. ✅ Returns error 403 if the user exists but is not active | ||
4. ✅ Returns error 403 if the password is not a match and if the user already exists - in progress | ||
|
||
## Edge case | ||
|
||
1. ✅ Returns the error if the try block fails - in progress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
const path = require('path'); | ||
require('dotenv').config({ path: path.resolve(__dirname, '../../.env') }); | ||
const bcrypt = require('bcryptjs'); | ||
const logincontroller = require('./logincontroller'); | ||
const { mockReq, mockRes, assertResMock, mockUser } = require('../test'); | ||
const userProfile = require('../models/userProfile'); | ||
|
||
const makeSut = () => { | ||
const { login, getUser } = logincontroller(); | ||
return { | ||
login, | ||
getUser, | ||
}; | ||
}; | ||
|
||
describe('logincontroller module', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('login', () => { | ||
test('Ensure login returns error 400 if there is no email or password', async () => { | ||
const { login } = makeSut(); | ||
const mockReqModified = { | ||
...mockReq, | ||
...{ | ||
body: { | ||
email: '', | ||
password: '', | ||
}, | ||
}, | ||
}; | ||
const res = await login(mockReqModified, mockRes); | ||
assertResMock(400, { error: 'Invalid request' }, res, mockRes); | ||
}); | ||
|
||
test('Ensure login returns error 403 if there is no user', async () => { | ||
const { login } = makeSut(); | ||
const mockReqModified = { | ||
...mockReq, | ||
...{ | ||
body: { | ||
email: '[email protected]', | ||
password: 'exampletest', | ||
}, | ||
}, | ||
}; | ||
const findOneSpy = jest | ||
.spyOn(userProfile, 'findOne') | ||
.mockImplementation(() => Promise.resolve(null)); | ||
|
||
const res = await login(mockReqModified, mockRes); | ||
expect(findOneSpy).toHaveBeenCalledWith({ email: mockReqModified.body.email }); | ||
assertResMock(403, { message: 'Username not found.' }, res, mockRes); | ||
}); | ||
|
||
test('Ensure login returns error 403 if the user exists but is not active', async () => { | ||
const { login } = makeSut(); | ||
const mockReqModified = { | ||
...mockReq, | ||
...{ | ||
body: { | ||
email: '[email protected]', | ||
password: 'exampletest', | ||
}, | ||
}, | ||
}; | ||
const mockUserModified = { | ||
...mockUser, | ||
...{ | ||
isActive: false, | ||
}, | ||
}; | ||
|
||
const findOneSpy = jest | ||
.spyOn(userProfile, 'findOne') | ||
.mockImplementation(() => Promise.resolve(mockUserModified)); | ||
|
||
const res = await login(mockReqModified, mockRes); | ||
expect(findOneSpy).toHaveBeenCalledWith({ email: mockReqModified.body.email }); | ||
assertResMock( | ||
403, | ||
{ | ||
message: | ||
'Sorry, this account is no longer active. If you feel this is in error, please contact your Manager and/or Administrator.', | ||
}, | ||
res, | ||
mockRes, | ||
); | ||
}); | ||
|
||
test('Ensure login returns error 403 if the password is not a match and if the user already exists', async () => { | ||
const { login } = makeSut(); | ||
const mockReqModified = { | ||
...mockReq, | ||
...{ | ||
body: { | ||
email: '[email protected]', | ||
password: 'SuperSecretPassword@', | ||
}, | ||
}, | ||
}; | ||
|
||
const findOneSpy = jest | ||
.spyOn(userProfile, 'findOne') | ||
.mockImplementation(() => Promise.resolve(mockUser)); | ||
jest.spyOn(bcrypt, 'compare').mockResolvedValue(false); | ||
|
||
const res = await login(mockReqModified, mockRes); | ||
expect(findOneSpy).toHaveBeenCalledWith({ email: mockReqModified.body.email }); | ||
|
||
assertResMock( | ||
403, | ||
{ | ||
message: 'Invalid password.', | ||
}, | ||
res, | ||
mockRes, | ||
); | ||
}); | ||
|
||
test('Ensure login returns the error if the try block fails', async () => { | ||
const { login } = makeSut(); | ||
const error = new Error('Try block failed'); | ||
const mockReqModified = { | ||
...mockReq, | ||
...{ | ||
body: { | ||
email: '[email protected]', | ||
password: 'exampletest', | ||
}, | ||
}, | ||
}; | ||
|
||
jest.spyOn(userProfile, 'findOne').mockImplementation(() => Promise.reject(error)); | ||
|
||
await login(mockReqModified, mockRes); | ||
expect(mockRes.json).toHaveBeenCalledWith(error); | ||
}); | ||
|
||
test('Ensure login returns 200, if the user is a new user and there is a password match', async () => { | ||
const { login } = makeSut(); | ||
const mockReqModified = { | ||
...mockReq, | ||
...{ | ||
body: { | ||
email: '[email protected]', | ||
password: '123Welcome!', | ||
}, | ||
}, | ||
}; | ||
|
||
const mockUserModified = { | ||
_id: 'user123', | ||
email: '[email protected]', | ||
password: 'hashedPassword', | ||
resetPwd: 'newUserPassword', | ||
isActive: true, | ||
}; | ||
|
||
jest | ||
.spyOn(userProfile, 'findOne') | ||
.mockImplementation(() => Promise.resolve(mockUserModified)); | ||
|
||
jest.spyOn(bcrypt, 'compare').mockResolvedValue(true); | ||
|
||
const res = await login(mockReqModified, mockRes); | ||
assertResMock(200, { new: true, userId: 'user123' }, res, mockRes); | ||
}); | ||
|
||
test('Ensure login returns 200, if the user already exists and the password is a match', async () => { | ||
const { login } = makeSut(); | ||
const mockReqModified = { | ||
...mockReq, | ||
body: { | ||
email: '[email protected]', | ||
password: 'existingUserPassword', | ||
}, | ||
}; | ||
const mockUserModified = { | ||
_id: 'user123', | ||
email: '[email protected]', | ||
password: 'hashedPassword', | ||
resetPwd: 'newUserPassword', | ||
isActive: true, | ||
role: 'Volunteer', | ||
permissions: ['read', 'write'], | ||
}; | ||
|
||
const findOneSpy = jest | ||
.spyOn(userProfile, 'findOne') | ||
.mockImplementation(() => Promise.resolve(mockUserModified)); | ||
|
||
jest.spyOn(bcrypt, 'compare').mockResolvedValue(true); | ||
|
||
const res = await login(mockReqModified, mockRes); | ||
expect(findOneSpy).toHaveBeenCalledWith({ email: mockReqModified.body.email }); | ||
|
||
assertResMock(200, { token: expect.any(String) }, res, mockRes); | ||
}); | ||
}); | ||
|
||
describe('getUser', () => { | ||
it('Ensure getUser returns 200, with the requestor body', () => { | ||
const { getUser } = makeSut(); | ||
|
||
const res = getUser(mockReq, mockRes); | ||
assertResMock(200, mockReq.body.requestor, res, mockRes); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
const mockRes = { | ||
status: jest.fn().mockReturnThis(), | ||
send: jest.fn(), | ||
json: jest.fn(), | ||
}; | ||
|
||
module.exports = mockRes; |