-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(audit-service): add unit test for audit service (#291)
- Loading branch information
1 parent
8a7fb48
commit 444ed5d
Showing
8 changed files
with
135 additions
and
2 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 |
---|---|---|
|
@@ -4,3 +4,4 @@ coverage/ | |
migrations/ | ||
migration.js | ||
.eslintrc.js | ||
.nyc_output |
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,7 @@ | ||
{ | ||
"include": ["dist"], | ||
"exclude": ["dist/__tests__/"], | ||
"extension": [".js", ".ts"], | ||
"reporter": ["text", "html"], | ||
"exclude-after-remap": false | ||
} |
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,2 +1,3 @@ | ||
dist | ||
*.json | ||
coverage |
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
9 changes: 9 additions & 0 deletions
9
services/audit-service/src/__tests__/sample-data/dummy-log.ts
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 @@ | ||
import {Action, AuditLog} from '@sourceloop/audit-log'; | ||
|
||
export const dummyLog: AuditLog = new AuditLog({ | ||
action: Action.INSERT_ONE, | ||
actedAt: new Date(), | ||
actionKey: 'insertone', | ||
entityId: '1', | ||
actor: 'testUser', | ||
}); |
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 @@ | ||
export * from './user'; |
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,13 @@ | ||
import {IAuthUserWithPermissions} from '@sourceloop/core'; | ||
|
||
export const user: IAuthUserWithPermissions = { | ||
id: 'random-id', | ||
firstName: 'random', | ||
lastName: 'user', | ||
tenantId: '1234-5678', | ||
username: 'random', | ||
email: 'random@random', | ||
permissions: [], | ||
authClientId: 1, | ||
role: 'random', | ||
}; |
99 changes: 99 additions & 0 deletions
99
services/audit-service/src/__tests__/unit/audit.controller.unit.ts
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,99 @@ | ||
import {HttpErrors} from '@loopback/rest'; | ||
import { | ||
createStubInstance, | ||
expect, | ||
sinon, | ||
StubbedInstanceWithSinonAccessor, | ||
} from '@loopback/testlab'; | ||
import {Filter} from '@loopback/repository'; | ||
import {AuditLog, AuditLogRepository} from '@sourceloop/audit-log'; | ||
import {AuditController} from '../../controllers'; | ||
import {dummyLog} from '../sample-data/dummy-log'; | ||
|
||
let auditLogRepository: StubbedInstanceWithSinonAccessor<AuditLogRepository>; | ||
let controller: AuditController; | ||
const setUpStub = () => { | ||
auditLogRepository = createStubInstance(AuditLogRepository); | ||
controller = new AuditController(auditLogRepository); | ||
}; | ||
|
||
beforeEach(setUpStub); | ||
|
||
describe('AuditController(unit) ', () => { | ||
describe('POST /audit-logs', () => { | ||
it('creates an audit log', async () => { | ||
const logCreate = auditLogRepository.stubs.create; | ||
logCreate.resolves(Object.assign(dummyLog, {id: 'uuid'})); | ||
const controllerResult = await controller.create(dummyLog); | ||
expect(controllerResult).to.have.a.property('id').which.is.a.String(); | ||
sinon.assert.calledOnce(logCreate); | ||
}); | ||
it('throws 400 if payload invalid', async () => { | ||
const logCreate = auditLogRepository.stubs.create; | ||
logCreate.rejects(new HttpErrors.BadRequest('Invalid Payload')); | ||
const controllerResult = await controller | ||
.create({abc: '1'}) | ||
.catch(err => err); | ||
expect(controllerResult).instanceOf(HttpErrors.BadRequest); | ||
}); | ||
}); | ||
|
||
describe('GET /audit-logs', () => { | ||
it('fetches all audit logs', async () => { | ||
const logFetch = auditLogRepository.stubs.find; | ||
logFetch.resolves([dummyLog]); | ||
const controllerResult = await controller.find(); | ||
sinon.assert.calledOnce(logFetch); | ||
expect(controllerResult).to.have.length(1); | ||
}); | ||
|
||
it('fetches only filtered logs', async () => { | ||
const logFetch = auditLogRepository.stubs.find; | ||
const filter: Filter<AuditLog> = { | ||
where: { | ||
id: 1, | ||
}, | ||
}; | ||
logFetch.withArgs(filter).resolves([Object.assign(dummyLog, {id: 1})]); | ||
logFetch.resolves([ | ||
Object.assign(dummyLog, {id: 1}), | ||
Object.assign(dummyLog, {id: 2}), | ||
]); | ||
const controllerResult = await controller.find(filter); | ||
sinon.assert.calledOnce(logFetch); | ||
expect(controllerResult).to.have.length(1); | ||
const controllerResultWithoutFilter = await controller.find(); | ||
expect(controllerResultWithoutFilter).to.have.length(2); | ||
}); | ||
}); | ||
|
||
describe('GET /audit-logs/count', () => { | ||
it('fetches count of all audit logs', async () => { | ||
const logFetch = auditLogRepository.stubs.count; | ||
logFetch.resolves({ | ||
count: 1, | ||
}); | ||
const controllerResult = await controller.count(); | ||
sinon.assert.calledOnce(logFetch); | ||
expect(controllerResult).to.have.property('count').equal(1); | ||
}); | ||
}); | ||
|
||
describe('GET /audit-logs/:id', () => { | ||
it('fetches audit log by id', async () => { | ||
const logFetch = auditLogRepository.stubs.findById; | ||
logFetch.withArgs('1').resolves(dummyLog); | ||
const controllerResult = await controller.findById('1'); | ||
sinon.assert.calledOnce(logFetch); | ||
expect(controllerResult).to.be.deepEqual(dummyLog); | ||
}); | ||
|
||
it('gives 404 when audit log not found for given id', async () => { | ||
const logFetch = auditLogRepository.stubs.findById; | ||
logFetch.withArgs('1').rejects(new HttpErrors.NotFound('Not Found')); | ||
const controllerResult = await controller.findById('1').catch(err => err); | ||
sinon.assert.calledOnce(logFetch); | ||
expect(controllerResult).instanceOf(HttpErrors.NotFound); | ||
}); | ||
}); | ||
}); |