-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
89 changed files
with
3,920 additions
and
951 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
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
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
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,122 @@ | ||
import { Request, NextFunction, Response } from 'express'; | ||
import { testingEnvironment } from 'api/utils/testingEnvironment'; | ||
import { getFixturesFactory } from 'api/utils/fixturesFactory'; | ||
import { UserSchema } from 'shared/types/userType'; | ||
import { UserRole } from 'shared/types/userSchema'; | ||
import { encryptPassword } from '../encryptPassword'; | ||
import { validatePasswordMiddleWare } from '../validatePasswordMiddleWare'; | ||
|
||
const fixturesFactory = getFixturesFactory(); | ||
|
||
describe('validatePasswordMiddleWare', () => { | ||
const mockResponse = (): Response => { | ||
const res: Partial<Response> = {}; | ||
res.status = jest.fn().mockReturnValue(res); | ||
res.json = jest.fn().mockReturnValue(res); | ||
return res as Response; | ||
}; | ||
|
||
const res = mockResponse(); | ||
const users: UserSchema[] = []; | ||
const next: NextFunction = jest.fn(); | ||
|
||
const createRequest = (request: Partial<Request>): Request => | ||
<Request>{ | ||
...request, | ||
user: request.user, | ||
body: request.body, | ||
headers: request.headers, | ||
}; | ||
|
||
const gernerateFixtures = async () => { | ||
users.push( | ||
...[ | ||
{ | ||
...fixturesFactory.user( | ||
'admin', | ||
UserRole.ADMIN, | ||
'[email protected]', | ||
await encryptPassword('admin1234') | ||
), | ||
}, | ||
{ | ||
...fixturesFactory.user( | ||
'editor', | ||
UserRole.EDITOR, | ||
'[email protected]', | ||
await encryptPassword('editor1234') | ||
), | ||
}, | ||
] | ||
); | ||
|
||
return { | ||
users, | ||
}; | ||
}; | ||
|
||
beforeAll(async () => { | ||
const fixtures = await gernerateFixtures(); | ||
await testingEnvironment.setUp(fixtures); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await testingEnvironment.tearDown(); | ||
}); | ||
|
||
it('should reject when the password is incorrect', async () => { | ||
const request = createRequest({ | ||
user: { _id: users[0]._id, username: users[0].username, role: users[0].role }, | ||
body: { username: 'a_new_user', role: 'collaborator', email: '[email protected]' }, | ||
headers: { authorization: 'Basic wrongPass' }, | ||
}); | ||
|
||
await validatePasswordMiddleWare(request, res, next); | ||
|
||
expect(next).not.toHaveBeenCalled(); | ||
expect(res.status).toHaveBeenCalledWith(403); | ||
expect(res.json).toHaveBeenCalledWith({ message: 'Forbidden', error: 'Password error' }); | ||
}); | ||
|
||
it('should reject when the password is empty', async () => { | ||
const emptyPasswordRequest = createRequest({ | ||
user: { _id: users[0]._id, username: users[0].username, role: users[0].role }, | ||
body: { username: 'a_new_user', role: 'collaborator', email: '[email protected]' }, | ||
headers: { authorization: 'Basic ' }, | ||
}); | ||
|
||
const noAuthHeaderRequest = createRequest({ | ||
user: { _id: users[0]._id, username: users[0].username, role: users[0].role }, | ||
body: { _id: users[0]._id, username: users[0].username, role: users[0].role }, | ||
headers: { cookie: 'some cookie' }, | ||
}); | ||
|
||
await validatePasswordMiddleWare(emptyPasswordRequest, res, next); | ||
|
||
expect(next).not.toHaveBeenCalled(); | ||
expect(res.status).toHaveBeenCalledWith(403); | ||
expect(res.json).toHaveBeenNthCalledWith(1, { message: 'Forbidden', error: 'Password error' }); | ||
|
||
await validatePasswordMiddleWare(noAuthHeaderRequest, res, next); | ||
|
||
expect(next).not.toHaveBeenCalled(); | ||
expect(res.status).toHaveBeenCalledWith(403); | ||
expect(res.json).toHaveBeenNthCalledWith(2, { message: 'Forbidden', error: 'Password error' }); | ||
}); | ||
|
||
it('should succeed when the passwords match', async () => { | ||
const request = createRequest({ | ||
user: { _id: users[1]._id, username: users[1].username, role: users[1].role }, | ||
body: { _id: users[1]._id, username: users[1].username, role: users[1].role }, | ||
headers: { authorization: 'Basic editor1234' }, | ||
}); | ||
|
||
await validatePasswordMiddleWare(request, res, next); | ||
|
||
expect(next).toHaveBeenCalled(); | ||
}); | ||
}); |
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,28 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { User } from 'api/users/usersModel'; | ||
import usersModel from '../users/users'; | ||
import { comparePasswords } from './encryptPassword'; | ||
|
||
const validatePassword = async (submittedPassword: string, requestUser: User) => { | ||
const user = await usersModel.getById(requestUser._id, '+password'); | ||
const currentPassword = user.password; | ||
return comparePasswords(submittedPassword, currentPassword); | ||
}; | ||
|
||
const validatePasswordMiddleWare = async (req: Request, res: Response, next: NextFunction) => { | ||
const { user, headers } = req; | ||
const submittedPassword = headers?.authorization?.split('Basic ')[1]; | ||
|
||
if (submittedPassword) { | ||
const validPassword = await validatePassword(submittedPassword, user); | ||
|
||
if (validPassword) { | ||
return next(); | ||
} | ||
} | ||
|
||
res.status(403); | ||
return res.json({ error: 'Password error', message: 'Forbidden' }); | ||
}; | ||
|
||
export { validatePasswordMiddleWare }; |
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
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
Oops, something went wrong.