Skip to content

Commit

Permalink
Added test cases for service #44
Browse files Browse the repository at this point in the history
Signed-off-by: raghunandanarava <raghunandan.arava@fau.de>
  • Loading branch information
raghunandanarava committed Jun 7, 2022
1 parent eefd5ff commit 2aff4bb
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/token/token.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class TokenController {
@Res()
res: Response,
): Promise<any> {
const issuer = await this.tokenService.get_issuer(issuer_s).catch(() => {
const issuer = await this.tokenService.getIssuer(issuer_s).catch(() => {
throw new HttpException(
{
status: HttpStatus.BAD_REQUEST,
Expand All @@ -90,7 +90,7 @@ export default class TokenController {
@Res()
res: Response,
): Promise<any> {
const issuer = await this.tokenService.get_issuer(issuer_s).catch(() => {
const issuer = await this.tokenService.getIssuer(issuer_s).catch(() => {
throw new HttpException(
{
status: HttpStatus.BAD_REQUEST,
Expand Down
74 changes: 74 additions & 0 deletions src/token/token.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Test, TestingModule } from '@nestjs/testing';
import { TokenService } from './token.service';
import { DiscoveryModule } from '../discovery/discovery.module';

describe('TokenService', () => {
let service: TokenService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [DiscoveryModule],
providers: [TokenService],
}).compile();

Expand Down Expand Up @@ -73,4 +75,76 @@ describe('TokenService', () => {
).rejects.toThrow('There was no tokenString to decode!');
});
});

describe('getIssuer', () => {
it('should fail if no issuer is provided', async () => {
await expect(service.getIssuer(undefined)).rejects.toThrow(
'There was no issuer string passed to get the issuer',
);
});

it('should fail is an empty issuer is provided', async () => {
await expect(service.getIssuer('')).rejects.toThrow(
'There was no issuer string passed to get the issuer',
);
});
});

describe('getToken', () => {
it('should fail if no token endpoint is provided', async () => {
await expect(
service.getToken(
undefined,
JSON.parse(
'{"client' +
'_id":"test-client-id","client_secret":"test-client-secret","audience":"test-audience","grant_type":"test-grant-type"}',
),
),
).rejects.toThrow('No or Empty token endpoint has been received');
});

it('should fail if empty token endpoint is provided', async () => {
await expect(
service.getToken(
'',
JSON.parse(
'{"client' +
'_id":"test-client-id","client_secret":"test-client-secret","audience":"test-audience","grant_type":"test-grant-type"}',
),
),
).rejects.toThrow('No or Empty token endpoint has been received');
});

it('should fail if no grant type is provided', async () => {
await expect(
service.getToken(
'test-token-endpoint',
JSON.parse(
'{"client' +
'_id":"test-client-id","client_secret":"test-client-secret","audience":"test-audience"}',
),
),
).rejects.toThrow('No or Empty grant_type has been received');
});

it('should fail if empty grant type is provided', async () => {
await expect(
service.getToken(
'test-token-endpoint',
JSON.parse(
'{"client' +
'_id":"test-client-id","client_secret":"test-client-secret","audience":"test-audience", "grant_type":""}',
),
),
).rejects.toThrow('No or Empty grant_type has been received');
});
});

describe('requestToken', () => {
it('should fail if empty issuer is provided', async () => {
await expect(service.requestToken(undefined)).rejects.toThrow(
'Received issuer does not contain the token endpoint to get a token',
);
});
});
});
26 changes: 25 additions & 1 deletion src/token/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,29 @@ export class TokenService {
@Inject(DiscoveryService)
private readonly discoveryService: DiscoveryService;

async get_issuer(issuer_s: string) {
async getIssuer(issuer_s: string) {
if (issuer_s === undefined || issuer_s === '') {
throw new HttpException(
'There was no issuer string passed to get the issuer',
HttpStatus.BAD_REQUEST,
);
}
return await this.discoveryService.get_issuer(issuer_s);
}

async getToken(token_endpoint: string, grantBody: GrantBody): Promise<any> {
if (token_endpoint === undefined || token_endpoint === '') {
throw new HttpException(
'No or Empty token endpoint has been received',
HttpStatus.BAD_REQUEST,
);
}
if (grantBody.grant_type === undefined || grantBody.grant_type === '') {
throw new HttpException(
'No or Empty grant_type has been received',
HttpStatus.BAD_REQUEST,
);
}
return await axios
.post(token_endpoint, qs.stringify(grantBody), {
headers: {
Expand All @@ -33,6 +51,12 @@ export class TokenService {
}

async requestToken(issuer: Issuer): Promise<any> {
if (issuer === undefined) {
throw new HttpException(
'Received issuer does not contain the token endpoint to get a token',
HttpStatus.BAD_REQUEST,
);
}
const grantBody: GrantBody = {
grant_type: process.env.CLIENT_CREDENTIALS_STRING,
client_id: process.env.CLIENT_ID,
Expand Down

0 comments on commit 2aff4bb

Please sign in to comment.