Skip to content

Commit

Permalink
test(notification-service): added unit testcases (#264)
Browse files Browse the repository at this point in the history
* test(notification-service): added unit testcases

gh-242

* fix(notification-service): removed code smells

gh-242

* fix(notification-service): moved types file to dev dependencies

gh-242

* fix(notification-service): added sinon package

gh-242

Co-authored-by: akshatdubeysf <[email protected]>
  • Loading branch information
ashutosh-bansal-2136 and akshatdubeysf authored Sep 3, 2021
1 parent e30a0ed commit 6e650aa
Show file tree
Hide file tree
Showing 23 changed files with 21,970 additions and 10,182 deletions.
11,138 changes: 11,138 additions & 0 deletions services/chat-service/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

19,945 changes: 9,765 additions & 10,180 deletions services/notification-service/package-lock.json

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions services/notification-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
"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 clean && npm run build",
"test": "echo \"No tests !\"",
"test": "lb-mocha --allow-console-logs \"dist/__tests__\"",
"posttest": "npm run lint",
"test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js && npm run posttest",
"clean": "lb-clean dist *.tsbuildinfo .eslintcache",
"coverage": "nyc npm run test",
"prepublishOnly": "npm run test",
"postinstall": "node migration.js"
},
Expand Down Expand Up @@ -57,26 +58,34 @@
"@loopback/core": "^2.16.0",
"@loopback/repository": "^3.6.0",
"@loopback/rest": "^9.3.0",
"@loopback/rest-explorer": "^3.3.2",
"@loopback/service-proxy": "^3.2.2",
"@sourceloop/core": "^2.0.1",
"aws-sdk": "^2.907.0",
"dotenv": "^8.2.0",
"dotenv-extended": "^2.9.0",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
"loopback4-authentication": "^4.6.0",
"loopback4-authorization": "^3.2.0",
"loopback4-notifications": "^1.5.0",
"loopback4-soft-delete": "^3.2.2",
"mocha": "^8.3.2",
"nyc": "^15.1.0",
"prom-client": "^13.1.0",
"pubnub": "^4.28.0",
"pubnub": "^4.32.1",
"sinon": "^11.1.2",
"tslib": "^2.0.0"
},
"devDependencies": {
"@loopback/build": "^6.4.0",
"@loopback/eslint-config": "^10.2.0",
"@loopback/testlab": "^3.4.0",
"@types/jsonwebtoken": "^8.5.4",
"@types/lodash": "^4.14.169",
"@types/node": "^10.17.60",
"@types/pubnub": "^4.29.4",
"@types/sinon": "^10.0.2",
"db-migrate": "^0.11.12",
"db-migrate-pg": "^1.2.2",
"eslint": "^7.25.0",
Expand Down
47 changes: 47 additions & 0 deletions services/notification-service/src/__tests__/acceptance/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
Client,
createRestAppClient,
givenHttpServerConfig,
} from '@loopback/testlab';
import {NotificationBindings} from 'loopback4-notifications';
import {NotifServiceBindings} from '../../keys';
import {NotificationApplication} from '../application';
import {
ChannelManagerMockProvider,
PubNubMockProvider,
SnsMockProvider,
} from '../fixture';

export async function setUpApplication(): Promise<AppWithClient> {
const app = new NotificationApplication({
rest: givenHttpServerConfig(),
});

await app.boot();

app.bind('datasources.config.notificationDb').to({
name: 'pgdb',
connector: 'memory',
});

app
.bind(NotificationBindings.NotificationProvider)
.toProvider(SnsMockProvider);

app.bind(NotificationBindings.PushProvider).toProvider(PubNubMockProvider);

app
.bind(NotifServiceBindings.ChannelManager)
.toProvider(ChannelManagerMockProvider);

await app.start();

const client = createRestAppClient(app);

return {app, client};
}

export interface AppWithClient {
app: NotificationApplication;
client: Client;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import {Client, expect} from '@loopback/testlab';
import * as jwt from 'jsonwebtoken';
import {NotificationRepository} from '../../repositories';
import {NotificationApplication} from '../application';
import {setUpApplication} from './helper';

describe('Notification Notification User Controller', () => {
let app: NotificationApplication;
let client: Client;
let notificationRepo: NotificationRepository;
const basePath = '/notifications/{id}/notification-users';
const pass = 'test_password';
const testUser = {
id: 1,
username: 'test_user',
password: pass,
permissions: [
'ViewNotification',
'CreateNotification',
'UpdateNotification',
'DeleteNotification',
],
};

const token = jwt.sign(testUser, 'kdskssdkdfs', {
expiresIn: 180000,
issuer: 'sf',
});

before('setupApplication', async () => {
({app, client} = await setUpApplication());
});
after(async () => app.stop());

before(givenRepositories);
afterEach(deleteMockData);

it('gives status 401 when no token is passed', async () => {
const response = await client.get(basePath).expect(401);

expect(response).to.have.property('error');
});

it('gives status 200 when token is passed', async () => {
await client
.get(basePath)
.set('authorization', `Bearer ${token}`)
.expect(200);
});

it('gives status 200 and check properties exist with response', async () => {
const response = await client
.get(`${basePath}`)
.set('authorization', `Bearer ${token}`)
.expect(200);
expect(response).to.have.properties(['res', 'req']);
});

it('updates notification successfully using PATCH request', async () => {
const notificationToUpdate = {
body: 'updated-body',
};

await client
.patch(`${basePath}`)
.set('authorization', `Bearer ${token}`)
.send(notificationToUpdate)
.expect(200);

const response = await client
.get(`${basePath}`)
.set('authorization', `Bearer ${token}`)
.expect(200);

expect(response).to.have.properties(['res', 'req']);
});

it('deletes a notification successfully', async () => {
await client
.del(`${basePath}`)
.set('authorization', `Bearer ${token}`)
.expect(200);
});

async function deleteMockData() {
await notificationRepo.deleteAll();
}

async function givenRepositories() {
notificationRepo = await app.getRepository(NotificationRepository);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {Client, expect} from '@loopback/testlab';
import * as jwt from 'jsonwebtoken';
import {NotificationUserRepository} from '../../repositories';
import {NotificationApplication} from '../application';
import {setUpApplication} from './helper';

describe('Notification User Notification Controller', () => {
let app: NotificationApplication;
let client: Client;
let notificationUserRepo: NotificationUserRepository;
const basePath = '/notification-users/{id}/notification';
const pass = 'test_password';
const testUser = {
id: 1,
username: 'test_user',
password: pass,
permissions: [
'ViewNotification',
'CreateNotification',
'UpdateNotification',
'DeleteNotification',
],
};

const token = jwt.sign(testUser, 'kdskssdkdfs', {
expiresIn: 180000,
issuer: 'sf',
});

before('setupApplication', async () => {
({app, client} = await setUpApplication());
});
after(async () => app.stop());

before(givenRepositories);
afterEach(deleteMockData);

it('gives status 401 when no token is passed', async () => {
const response = await client.get(basePath).expect(401);

expect(response).to.have.property('error');
});

it('gives status 404 when token is passed for non presence of entity', async () => {
const response = await client
.get(basePath)
.set('authorization', `Bearer ${token}`)
.expect(404);

expect(response).to.have.property('error');
});

async function deleteMockData() {
await notificationUserRepo.deleteAll();
}

async function givenRepositories() {
notificationUserRepo = await app.getRepository(NotificationUserRepository);
}
});
Loading

0 comments on commit 6e650aa

Please sign in to comment.