Skip to content

Commit

Permalink
fix(authentication-service): added test cases (#316)
Browse files Browse the repository at this point in the history
* fix(authentication-service): added test cases

gh-181

* fix(authentication-service): removed code smells

gh-181

* fix(authentication-service): code smells removed

gh-181

* fix(authentication-service): updated tests

gh-181

Co-authored-by: akshatdubeysf <77672713+akshatdubeysf@users.noreply.github.com>
  • Loading branch information
ashutosh-bansal-2136 and akshatdubeysf authored Sep 21, 2021
1 parent 320e513 commit 65d55e4
Show file tree
Hide file tree
Showing 44 changed files with 2,803 additions and 456 deletions.
733 changes: 375 additions & 358 deletions services/authentication-service/package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion services/authentication-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"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__\"",
"coverage": "nyc npm run test",
"posttest": "npm run lint",
"prepublishOnly": "npm run test",
"test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js && npm run posttest",
Expand Down Expand Up @@ -74,11 +75,13 @@
"moment": "^2.27.0",
"moment-timezone": "^0.5.31",
"node-fetch": "^2.6.1",
"nyc": "^15.1.0",
"passport-apple": "^2.0.1",
"passport-facebook": "^3.0.0",
"passport-google-oauth20": "^2.0.0",
"passport-instagram": "^1.0.0",
"prom-client": "^13.1.0",
"sinon": "^11.1.2",
"tslib": "^2.0.0"
},
"devDependencies": {
Expand All @@ -96,6 +99,7 @@
"@types/passport-facebook": "^2.1.10",
"@types/passport-google-oauth20": "^2.0.3",
"@types/passport-instagram": "^1.0.0",
"@types/sinon": "^10.0.2",
"db-migrate": "^0.11.12",
"db-migrate-pg": "^1.2.2",
"eslint": "^7.12.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import {Client, expect} from '@loopback/testlab';
import * as jwt from 'jsonwebtoken';
import {AuthClientRepository} from '../../repositories';
import {TestingApplication} from '../fixtures/application';
import {setupApplication} from './test-helper';

describe('Auth Client Controller', () => {
let app: TestingApplication;
let client: Client;
let authClientRepo: AuthClientRepository;
const basePath = '/auth-clients';
const pass = 'test_password';
const testUser = {
id: 1,
username: 'test_user',
password: pass,
permissions: ['NotAllowed'],
};

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

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 client detail when client is added', async () => {
const reqToAddEntity = await addEntity();
expect(reqToAddEntity.status).to.be.equal(200);

const response = await client
.get(`${basePath}/${reqToAddEntity.body.id}`)
.set('authorization', `Bearer ${token}`)
.expect(200);
expect(response.body).to.have.properties(['clientId', 'clientSecret']);
expect(response.body.clientId).to.be.equal('test_client_id');
});

it('updates client successfully using PATCH request', async () => {
const reqToAddEntity = await addEntity();

const entityToUpdate = {
clientId: 'test_client_id_updated',
clientSecret: 'test_client_secret',
};

await client
.patch(`${basePath}/${reqToAddEntity.body.id}`)
.set('authorization', `Bearer ${token}`)
.send(entityToUpdate)
.expect(204);

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

expect(response.body).to.have.properties(['clientId', 'clientSecret']);
expect(response.body.clientId).to.be.equal('test_client_id_updated');
});

it('updates client using PUT request', async () => {
const reqToAddEntity = await addEntity();

const entityToUpdate = {
clientId: 'test_client_id_updated',
clientSecret: 'test_client_secret',
secret: 'test_secret',
accessTokenExpiration: 1800,
refreshTokenExpiration: 1800,
authCodeExpiration: 1800,
};

await client
.put(`${basePath}/${reqToAddEntity.body.id}`)
.set('authorization', `Bearer ${token}`)
.send(entityToUpdate)
.expect(204);

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

expect(response.body).to.have.properties(['clientId', 'clientSecret']);
expect(response.body.clientId).to.be.equal('test_client_id_updated');
});

it('deletes a client successfully', async () => {
const reqToAddEntity = await addEntity();
await client
.del(`${basePath}/${reqToAddEntity.body.id}`)
.set('authorization', `Bearer ${token}`)
.expect(204);
});

it('should return count', async () => {
await client
.get(`${basePath}/count`)
.set('authorization', `Bearer ${token}`)
.expect(200);
});

async function addEntity() {
const enitityToAdd = {
clientId: 'test_client_id',
clientSecret: 'test_client_secret',
secret: 'test_secret',
accessTokenExpiration: 1800,
refreshTokenExpiration: 1800,
authCodeExpiration: 1800,
};

return client
.post(basePath)
.set('authorization', `Bearer ${token}`)
.send(enitityToAdd);
}

async function deleteMockData() {
await authClientRepo.deleteAllHard();
}

async function givenRepositories() {
authClientRepo = await app.getRepository(AuthClientRepository);
}
});
Loading

0 comments on commit 65d55e4

Please sign in to comment.