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(audit-service): add unit test for audit service #291

Merged
merged 5 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions services/audit-service/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ coverage/
migrations/
migration.js
.eslintrc.js
.nyc_output
7 changes: 7 additions & 0 deletions services/audit-service/.nycrc
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
}
1 change: 1 addition & 0 deletions services/audit-service/.prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist
*.json
coverage
6 changes: 4 additions & 2 deletions services/audit-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
"openapi-spec": "node ./dist/openapi-spec",
"apidocs": "./node_modules/.bin/widdershins --search false --language_tabs 'javascript:JavaScript:request' 'javascript--nodejs:Node.JS' --summary openapi.json -o openapi.md",
"pretest": "npm run build",
"test": "echo 'No tests'",
"test": "lb-mocha --allow-console-logs \"dist/__tests__\"",
"coverage": "lb-nyc npm run test",
"coverage:ci": "lb-nyc report --reporter=text-lcov | coveralls",
"posttest": "npm run lint",
"test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js && npm run posttest",
"clean": "lb-clean dist *.tsbuildinfo .eslintcache",
Expand Down Expand Up @@ -81,4 +83,4 @@
"registry": "https://registry.npmjs.org/",
"access": "public"
}
}
}
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',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './user';
13 changes: 13 additions & 0 deletions services/audit-service/src/__tests__/sample-data/user.ts
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',
};
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);
});
});
});