-
Notifications
You must be signed in to change notification settings - Fork 8
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 #262 from adhocteam/main
Add extra audit logging messages
- Loading branch information
Showing
14 changed files
with
111 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,62 @@ | ||
import {} from 'dotenv/config'; | ||
import winston from 'winston'; | ||
import { format, transports, createLogger } from 'winston'; | ||
import expressWinston from 'express-winston'; | ||
import env from './env'; | ||
|
||
const formatFunc = ({ | ||
level, message, label, timestamp, meta = {}, | ||
}) => `${timestamp} ${label || '-'} ${level}: ${message} ${JSON.stringify(meta)}`; | ||
|
||
const stringFormatter = winston.format.combine( | ||
winston.format.timestamp(), | ||
winston.format.colorize(), | ||
winston.format.align(), | ||
winston.format.printf(formatFunc), | ||
const stringFormatter = format.combine( | ||
format.timestamp(), | ||
format.colorize(), | ||
format.align(), | ||
format.printf(formatFunc), | ||
); | ||
|
||
const jsonFormatter = winston.format.combine( | ||
winston.format.timestamp(), | ||
winston.format.json(), | ||
const jsonFormatter = format.combine( | ||
format.timestamp(), | ||
format.json(), | ||
); | ||
|
||
const formatter = env.bool('LOG_JSON_FORMAT') ? jsonFormatter : stringFormatter; | ||
|
||
const logger = winston.createLogger({ | ||
const logger = createLogger({ | ||
level: process.env.LOG_LEVEL, | ||
format: formatter, | ||
transports: [ | ||
new winston.transports.Console(), | ||
new transports.Console(), | ||
], | ||
}); | ||
|
||
export const auditLogger = createLogger({ | ||
level: 'info', | ||
format: format.combine( | ||
format.label({ label: 'AUDIT' }), | ||
formatter, | ||
), | ||
transports: [ | ||
new transports.Console(), | ||
], | ||
}); | ||
|
||
export const requestLogger = expressWinston.logger({ | ||
transports: [ | ||
new winston.transports.Console(), | ||
new transports.Console(), | ||
], | ||
format: formatter, | ||
format: format.combine( | ||
format.label({ label: 'REQUEST' }), | ||
formatter, | ||
), | ||
dynamicMeta: (req) => { | ||
if (req && req.session) { | ||
const { userId } = req.session; | ||
return { | ||
userId, | ||
}; | ||
} | ||
return {}; | ||
}, | ||
}); | ||
|
||
export default logger; |
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 |
---|---|---|
@@ -1,13 +1,15 @@ | ||
import {} from 'dotenv/config'; | ||
import db from '../models'; | ||
import db, { User, Permission } from '../models'; | ||
import userAdminAccessMiddleware from './userAdminAccessMiddleware'; | ||
import handleErrors from '../lib/apiErrorHandler'; | ||
import SCOPES from './scopeConstants'; | ||
|
||
const { ADMIN } = SCOPES; | ||
|
||
jest.mock('../lib/apiErrorHandler', () => jest.fn().mockReturnValue(() => Promise.resolve())); | ||
|
||
const mockNext = jest.fn(); | ||
const mockSession = jest.fn(); | ||
mockSession.userId = 2; | ||
const mockRequest = { | ||
path: '/api/endpoint', | ||
session: mockSession, | ||
|
@@ -19,27 +21,41 @@ const mockResponse = { | |
end: jest.fn(), | ||
})), | ||
}; | ||
let user; | ||
|
||
describe('userAdminAccessMiddleware', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
afterAll(() => { | ||
beforeEach(async () => { | ||
[user] = await User.findOrCreate({ | ||
where: { | ||
email: '[email protected]', | ||
}, | ||
}); | ||
|
||
mockSession.userId = user.id; | ||
}); | ||
afterAll(async () => { | ||
await User.destroy({ where: { id: user.id } }); | ||
db.sequelize.close(); | ||
}); | ||
|
||
it('should allow access if an user has an admin role', async () => { | ||
mockSession.role = 'admin'; | ||
const admin = await Permission.create({ | ||
scopeId: ADMIN, | ||
userId: user.id, | ||
regionId: 14, | ||
}); | ||
|
||
await userAdminAccessMiddleware(mockRequest, mockResponse, mockNext); | ||
|
||
expect(mockResponse.sendStatus).not.toHaveBeenCalled(); | ||
expect(mockNext).toHaveBeenCalled(); | ||
await admin.destroy(); | ||
}); | ||
|
||
it('should return 403 if an user does not have an admin permission', async () => { | ||
mockSession.role = undefined; | ||
|
||
await userAdminAccessMiddleware(mockRequest, mockResponse, mockNext); | ||
|
||
expect(mockResponse.sendStatus).toHaveBeenCalledWith(403); | ||
|
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
Oops, something went wrong.