Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extend AuditLogService #36

Merged
merged 5 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).

### Added

- Export class `AuditLogService` for use in custom implementations
sjvans marked this conversation as resolved.
Show resolved Hide resolved

### Changed

### Fixed
Expand Down
7 changes: 7 additions & 0 deletions cds-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ cds.on('served', services => {
}
}
})

/*
* Export base class for extending in custom implementations
*/
module.exports = {
AuditLogService: require('./srv/service')
}
16 changes: 16 additions & 0 deletions test/api/MyAuditLogService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { AuditLogService } = require('../../') //> package root

class MyAuditLogService extends AuditLogService {
async init() {
this.on('*', function (req) {
const { event, data } = req

console.log(`[my-audit-log] - ${event}:`, data)
})

// call AuditLogService's init
await super.init()
}
}

module.exports = MyAuditLogService
34 changes: 0 additions & 34 deletions test/api/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,38 +88,4 @@ describe('AuditLogService API', () => {
await audit.log('foo', { data_subject: { ID: { bar: 'baz' } } })
expect(_logs).toContainMatchObject({ data_subject: { ID: { bar: 'baz' } } })
})

describe('intercept audit logs', () => {
let _intercept

beforeAll(async () => {
const als = cds.services['audit-log'] || (await cds.connect.to('audit-log'))

const _log = als.log
als.log = async function (event, data) {
if (!_intercept) return _log.call(this, event, data)
}
})

beforeEach(async () => {
_intercept = undefined
})

test('intercept on', async () => {
_intercept = true

const response = await POST('/api/testLog', {}, { auth: ALICE })
expect(response).toMatchObject({ status: 204 })
expect(_logs.length).toBe(0)
})

test('intercept off', async () => {
_intercept = false

const response = await POST('/api/testLog', {}, { auth: ALICE })
expect(response).toMatchObject({ status: 204 })
expect(_logs.length).toBe(1)
expect(_logs).toContainMatchObject({ user: 'alice', bar: 'baz' })
})
})
})
39 changes: 39 additions & 0 deletions test/api/custom.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const cds = require('@sap/cds')

cds.env.requires['audit-log'] = {
impl: 'MyAuditLogService.js'
}

// set cwd for resolving impl
cds.test(__dirname)

describe('Custom Implementation', () => {
let __log, _logs
const _log = (...args) => {
if (!(args.length === 2 && typeof args[0] === 'string' && args[0].match(/\[my-audit-log\]/i))) {
// > not an audit log (most likely, anyway)
return __log(...args)
}

_logs.push(args[1])
}

beforeAll(() => {
__log = global.console.log
global.console.log = _log
})

afterAll(() => {
global.console.log = __log
})

beforeEach(async () => {
_logs = []
})

test('extending AuditLogService exported by plugin', async () => {
const audit = await cds.connect.to('audit-log')
await audit.log('foo', { data_subject: { ID: { bar: 'baz' } } })
expect(_logs).toContainMatchObject({ data_subject: { ID: { bar: 'baz' } } })
})
})
Loading