Permalink
Browse files

feat(auth service): Add tests for jwt auth service

  • Loading branch information...
zakhenry committed Jul 26, 2016
1 parent db3a366 commit 087fd4840b04f1ed6fa13142b944fbffb8a6c802
@@ -8,8 +8,8 @@ import { bootstrap, BootstrapResponse, deferredLog } from './index';
import { registry } from '../../common/registry/entityRegistry';
import { ClassDictionary } from './bootstrap';
import Spy = jasmine.Spy;
import { AuthService } from '../services/authService.service';
import { AuthServiceMock } from '../services/authService.service.mock';
import { AuthService } from '../services/auth.service';
import { AuthServiceMock } from '../services/auth.service.mock';
let loggerInstance: Logger;
@@ -12,8 +12,8 @@ import { AbstractController } from '../controllers/abstract.controller';
import { Route } from '../controllers/route.decorator';
import { Request } from '../controllers/request';
import { Response } from '../controllers/response';
import { AuthServiceMock } from '../services/authService.service.mock';
import { AuthService } from '../services/authService.service';
import { AuthServiceMock } from '../services/auth.service.mock';
import { AuthService } from '../services/auth.service';
const providers: any[] = [
{provide: Logger, useClass: LoggerMock},
@@ -12,8 +12,8 @@ import { AbstractMigration } from '../migrations/index';
import { Database } from '../services/database.service';
import { DatabaseMock } from '../services/database.service.mock';
import Spy = jasmine.Spy;
import { AuthServiceMock } from '../services/authService.service.mock';
import { AuthService } from '../services/authService.service';
import { AuthServiceMock } from '../services/auth.service.mock';
import { AuthService } from '../services/auth.service';
let loggerInstance: Logger = new LoggerMock();
let databaseInstance: Database;
@@ -14,8 +14,8 @@ import { StoredProperty } from '../../common/models/types/storedProperty.decorat
import * as typeormColumns from 'typeorm/columns';
import { CreatedDate, UpdatedDate } from '../../common/models/types/timestamp.decorator';
import Spy = jasmine.Spy;
import { AuthServiceMock } from '../services/authService.service.mock';
import { AuthService } from '../services/authService.service';
import { AuthServiceMock } from '../services/auth.service.mock';
import { AuthService } from '../services/auth.service';
const providers: any[] = [
{provide: Logger, useClass: LoggerMock},
@@ -10,8 +10,8 @@ import { bootstrap, BootstrapResponse } from './index';
import { registry } from '../../common/registry/entityRegistry';
import { AbstractSeeder } from '../seeders/index';
import Spy = jasmine.Spy;
import { AuthServiceMock } from '../services/authService.service.mock';
import { AuthService } from '../services/authService.service';
import { AuthServiceMock } from '../services/auth.service.mock';
import { AuthService } from '../services/auth.service';
let loggerInstance: Logger = new LoggerMock();
@@ -10,8 +10,8 @@ import { RemoteCli } from '../services/remoteCli.service';
import { ServerMock } from '../servers/abstract.server.spec';
import { Server } from '../servers/abstract.server';
import Spy = jasmine.Spy;
import { AuthServiceMock } from '../services/authService.service.mock';
import { AuthService } from '../services/authService.service';
import { AuthServiceMock } from '../services/auth.service.mock';
import { AuthService } from '../services/auth.service';
let loggerInstance: Logger = new LoggerMock();
@@ -11,8 +11,8 @@ import { Response } from './response';
import { AbstractController } from './abstract.controller';
import { Route } from './route.decorator';
import { UnavailableForLegalReasonsException } from '../../common/exeptions/exceptions';
import { AuthServiceMock } from '../services/authService.service.mock';
import { AuthService } from '../services/authService.service';
import { AuthServiceMock } from '../services/auth.service.mock';
import { AuthService } from '../services/auth.service';
@Injectable()
class TestController extends AbstractController {
@@ -17,8 +17,8 @@ import { EventEmitter } from 'events';
import { IncomingMessage } from 'http';
import { Primary } from '../../common/models/types/primary.decorator';
import * as _ from 'lodash';
import { AuthServiceMock } from '../services/authService.service.mock';
import { AuthService } from '../services/authService.service';
import { AuthServiceMock } from '../services/auth.service.mock';
import { AuthService } from '../services/auth.service';
class Fruit extends AbstractModel {
@Primary()
@@ -11,8 +11,8 @@ import { RemoteCli } from '../services/remoteCli.service';
import { RemoteCliMock } from '../services/remoteCli.service.mock';
import { Route } from './route.decorator';
import { Controller } from '../../common/registry/decorators';
import { AuthServiceMock } from '../services/authService.service.mock';
import { AuthService } from '../services/authService.service';
import { AuthServiceMock } from '../services/auth.service.mock';
import { AuthService } from '../services/auth.service';
@Injectable()
@Controller({
@@ -12,8 +12,8 @@ import { ServerMock } from '../servers/abstract.server.spec';
import { RemoteCli } from '../services/remoteCli.service';
import { RemoteCliMock } from '../services/remoteCli.service.mock';
import { debugLog, DebugLogMiddleware } from './debugLog.middleware';
import { AuthServiceMock } from '../services/authService.service.mock';
import { AuthService } from '../services/authService.service';
import { AuthServiceMock } from '../services/auth.service.mock';
import { AuthService } from '../services/auth.service';
@Injectable()
class MiddlewareController extends AbstractController {
@@ -13,8 +13,8 @@ import { ServerMock } from '../servers/abstract.server.spec';
import { RemoteCli } from '../services/remoteCli.service';
import { RemoteCliMock } from '../services/remoteCli.service.mock';
import { PromiseFactory } from '../../common/util/serialPromise';
import { AuthServiceMock } from '../services/authService.service.mock';
import { AuthService } from '../services/authService.service';
import { AuthServiceMock } from '../services/auth.service.mock';
import { AuthService } from '../services/auth.service';
let middlewareCalls: string[] = [];
@@ -7,8 +7,8 @@ import { LoggerMock } from '../../common/services/logger.service.mock';
import { RemoteCliMock } from '../services/remoteCli.service.mock';
import { Response } from '../controllers/response';
import Spy = jasmine.Spy;
import { AuthService } from '../services/authService.service';
import { AuthServiceMock } from '../services/authService.service.mock';
import { AuthService } from '../services/auth.service';
import { AuthServiceMock } from '../services/auth.service.mock';
@Injectable()
export class ServerMock extends Server {
@@ -7,8 +7,8 @@ import * as proxyquire from 'proxyquire';
import { Response } from '../controllers/response';
import { Request } from '../controllers/request';
import { RemoteCliMock } from '../services/remoteCli.service.mock';
import { AuthServiceMock } from '../services/authService.service.mock';
import { AuthService } from '../services/authService.service';
import { AuthServiceMock } from '../services/auth.service.mock';
import { AuthService } from '../services/auth.service';
describe('Express Server', () => {
@@ -7,8 +7,8 @@ import { RemoteCliMock } from '../services/remoteCli.service.mock';
import * as proxyquire from 'proxyquire';
import { Response } from '../controllers/response';
import { Request } from '../controllers/request';
import { AuthServiceMock } from '../services/authService.service.mock';
import { AuthService } from '../services/authService.service';
import { AuthServiceMock } from '../services/auth.service.mock';
import { AuthService } from '../services/auth.service';
describe('Hapi Server', () => {
@@ -6,7 +6,7 @@ import { Injectable } from '@angular/core';
import { Logger } from '../../common/services/logger.service';
import { Service } from '../../common/registry/decorators';
import * as jwt from 'jsonwebtoken';
import { AuthService } from './authService.service';
import { AuthService } from './auth.service';
/**
* Class allows developers to register custom commands that can be remote executed in a
@@ -20,7 +20,7 @@ export class AuthServiceMock extends AuthService {
super(loggerBase);
}
public verify(jwtToken: string, publicKeyPath: string, params: Object = {}): Promise<any> {
public verify(jwtToken: string, publicKeyPath: string = '', params: Object = {}): Promise<any> {
return Promise.resolve(jwt.decode(jwtToken));
}
@@ -0,0 +1,142 @@
import * as proxyquire from 'proxyquire';
import { Logger } from '../../common/services/logger.service';
import { addProviders, async, inject } from '@angular/core/testing';
import { LoggerMock } from '../../common/services/logger.service.mock';
import { AuthServiceMock } from './auth.service.mock';
import { AuthService } from './auth.service';
const jwtSpy = jasmine.createSpyObj('jwt', ['verify', 'decode']);
const payload = {
username: 'bob',
};
jwtSpy.verify.and.returnValue(Promise.resolve(payload));
jwtSpy.decode.and.returnValue(Promise.resolve(payload));
describe('Auth Service (JWT)', () => {
const fileReadSpy = jasmine.createSpy('readFileSync');
const pem = `
--- BEGIN FAKE KEY ---
--- END FAKE KEY ---
`;
fileReadSpy.and.returnValue(pem);
const mockedModule = proxyquire('./auth.service', {
jsonwebtoken: jwtSpy,
fs: {
readFileSync: fileReadSpy
}
});
const providers = [
{
provide: AuthService,
deps: [Logger],
useFactory: (logger: Logger) => {
return new mockedModule.AuthService(logger);
}
},
{provide: Logger, useClass: LoggerMock},
];
beforeEach(() => {
addProviders(providers);
});
afterEach(() => {
jwtSpy.verify.calls.reset();
});
it('verifies credentials with jwt and public key', async(inject([AuthService], (s: AuthService) => {
const jwt = 'pretend.this.is.a.jwt';
const publicKeyPath = './path/to/key';
const params = {
iat: 12345679
};
const resPromise = s.verify(jwt, publicKeyPath, params);
expect(fileReadSpy)
.toHaveBeenCalledWith(publicKeyPath);
expect(jwtSpy.verify)
.toHaveBeenCalledWith(jwt, pem, params, jasmine.any(Function));
const callArgCb = jwtSpy.verify.calls.mostRecent().args[3];
callArgCb(null, payload);
return resPromise.then((res) => {
expect(res)
.toEqual(payload);
});
})));
it('rejects verification of credentials when jwt.verify fails', async(inject([AuthService], (s: AuthService) => {
const jwt = 'pretend.this.is.an.invalid.jwt';
const publicKeyPath = './path/to/key';
const resPromise = s.verify(jwt, publicKeyPath);
expect(fileReadSpy)
.toHaveBeenCalledWith(publicKeyPath);
expect(jwtSpy.verify)
.toHaveBeenCalledWith(jwt, pem, {}, jasmine.any(Function));
const callArgCb = jwtSpy.verify.calls.mostRecent().args[3];
callArgCb(new Error('jwt error'), payload);
return resPromise.catch((e) => {
expect(e.message)
.toEqual('jwt error');
});
})));
});
describe('Auth Service (JWT) Mock', () => {
const mockedModule = proxyquire('./auth.service.mock', {
jsonwebtoken: jwtSpy,
});
const providers = [
{
provide: AuthServiceMock,
deps: [Logger],
useFactory: (logger: Logger) => {
return new mockedModule.AuthServiceMock(logger);
}
},
{provide: Logger, useClass: LoggerMock},
];
beforeEach(() => {
addProviders(providers);
});
it('returns decoded jwt directly without checking', async(inject([AuthServiceMock], (s: AuthServiceMock) => {
const jwt = 'pretend.this.is.a.valid.jwt';
const resPromise = s.verify(jwt);
expect(jwtSpy.decode)
.toHaveBeenCalledWith(jwt);
return resPromise.then((res) => {
expect(res)
.toEqual(payload);
});
})));
});
@@ -10,8 +10,8 @@ import { RemoteCliMock } from './remoteCli.service.mock';
import { registry } from '../../common/registry/entityRegistry';
import * as SQL from 'sql-template-strings';
import Spy = jasmine.Spy;
import { AuthServiceMock } from './authService.service.mock';
import { AuthService } from './authService.service';
import { AuthServiceMock } from './auth.service.mock';
import { AuthService } from './auth.service';
@Injectable()
class ExampleUtil {
@@ -7,7 +7,7 @@ import { RemoteCli, ConnectedSocketCallback } from './remoteCli.service';
import { Logger } from '../../common/services/logger.service';
import { Service } from '../../common/registry/decorators';
import { AuthService } from './authService.service';
import { AuthService } from './auth.service';
/**
* Provides no-side effect mock for RemoteCli for use in testing fixtures
@@ -8,8 +8,8 @@ import { registry } from '../../common/registry/entityRegistry';
import { Server, RouteConfig } from '../servers/abstract.server';
import { ServerMock } from '../servers/abstract.server.spec';
import { RemoteCliMock } from './remoteCli.service.mock';
import { AuthServiceMock } from './authService.service.mock';
import { AuthService } from './authService.service';
import { AuthServiceMock } from './auth.service.mock';
import { AuthService } from './auth.service';
import * as chalk from 'chalk';
import Spy = jasmine.Spy;
@@ -16,7 +16,7 @@ import { AbstractService } from '../../common/services/service';
const table: Table = require('table').default;
import Socket = SocketIO.Socket;
import { AuthService } from './authService.service';
import { AuthService } from './auth.service';
import { jwtAuthStrategyFactory } from './jwtAuthStrategy';
export interface TableBorderTemplate {

0 comments on commit 087fd48

Please sign in to comment.