-
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.
test(notification-service): added unit testcases (#264)
* 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
1 parent
e30a0ed
commit 6e650aa
Showing
23 changed files
with
21,970 additions
and
10,182 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
19,945 changes: 9,765 additions & 10,180 deletions
19,945
services/notification-service/package-lock.json
Large diffs are not rendered by default.
Oops, something went wrong.
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
47 changes: 47 additions & 0 deletions
47
services/notification-service/src/__tests__/acceptance/helper.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,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; | ||
} |
92 changes: 92 additions & 0 deletions
92
...-service/src/__tests__/acceptance/notification-notification-user.controller.acceptance.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,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); | ||
} | ||
}); |
60 changes: 60 additions & 0 deletions
60
...-service/src/__tests__/acceptance/notification-user-notification.controller.acceptance.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,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); | ||
} | ||
}); |
Oops, something went wrong.