Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split tests across files, various style fixes
- Loading branch information
Showing
9 changed files
with
271 additions
and
257 deletions.
There are no files selected for viewing
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 @@ | ||
declare function require(string: string): string; |
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,106 @@ | ||
import {Chance} from "chance"; | ||
import * as _ from "lodash"; | ||
import * as moment from "moment"; | ||
import {IJwtToken, IUser} from "./ngJwtAuthInterfaces"; | ||
|
||
let seededChance:Chance.Chance = new Chance(1); | ||
export const fixtures = { | ||
user: { | ||
_self: '/users/1', | ||
userId: 1, | ||
email: 'joe.bloggs@example.com', | ||
firstName: seededChance.first(), | ||
lastName: seededChance.last(), | ||
password: 'password', | ||
phone: seededChance.phone() | ||
}, | ||
|
||
get userResponse():IUser{ | ||
return <IUser>_.omit(fixtures.user, 'password'); | ||
}, | ||
|
||
get authBasic():string{ | ||
return 'Basic '+btoa(fixtures.user.email+':'+fixtures.user.password) | ||
}, | ||
|
||
buildToken: (overrides = {}) => { | ||
let defaultConfig = { | ||
header: { | ||
alg: 'RS256', | ||
typ: 'JWT' | ||
}, | ||
data: { | ||
iss: 'api.spira.io', | ||
aud: 'spira.io', | ||
sub: fixtures.user.userId, | ||
iat: Number(moment().format('X')), | ||
exp: Number(moment().add(1, 'hours').format('X')), | ||
jti: 'random-hash', | ||
'#user': fixtures.userResponse, | ||
}, | ||
signature: 'this-is-the-signed-hash' | ||
}; | ||
|
||
let token:IJwtToken = <any>_.merge(defaultConfig, overrides); | ||
|
||
return btoa(JSON.stringify(token.data)) | ||
+ '.' + btoa(JSON.stringify(token.data)) | ||
+ '.' + token.signature | ||
; | ||
}, | ||
|
||
get token(){ | ||
|
||
return fixtures.buildToken(); //no customisations | ||
} | ||
}; | ||
|
||
|
||
export function locationFactoryMock(hostname:string) { | ||
return () => { | ||
|
||
return { | ||
host: function () { | ||
return hostname; | ||
} | ||
}; | ||
}; | ||
} | ||
|
||
export function cookiesFactoryMock(allowDomain:string) { | ||
|
||
let cookieStore = {}; | ||
|
||
return () => { | ||
|
||
return { | ||
/* If you need more then $location.host(), add more methods */ | ||
put: (key, value, conf) => { | ||
|
||
if (conf.domain && conf.domain !== allowDomain || value.split('.')[2] == 'always-fail-domain'){ | ||
return false; | ||
} | ||
|
||
cookieStore[key] = { | ||
value: value, | ||
conf: conf | ||
}; | ||
}, | ||
|
||
get: (key) => { | ||
if (!cookieStore[key]){ | ||
return undefined; | ||
} | ||
return cookieStore[key].value; | ||
}, | ||
|
||
getObject: (key) => { | ||
return cookieStore[key]; | ||
}, | ||
|
||
remove: (key) => { | ||
delete cookieStore[key]; | ||
} | ||
}; | ||
}; | ||
}; |
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
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
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,105 @@ | ||
import {NgJwtAuthServiceProvider, NgJwtAuthException} from "./ngJwtAuthServiceProvider"; | ||
import {NgJwtAuthService} from "../service/ngJwtAuthService"; | ||
import {INgJwtAuthServiceConfig} from "../ngJwtAuthInterfaces"; | ||
|
||
import "angular"; | ||
import "angular-mocks"; | ||
import "../index" //@todo double check this is right | ||
|
||
let expect:Chai.ExpectStatic = chai.expect; | ||
|
||
let defaultAuthServiceProvider:NgJwtAuthServiceProvider; | ||
|
||
describe('Default configuration', function () { | ||
|
||
let defaultAuthService:NgJwtAuthService; | ||
|
||
beforeEach(() => { | ||
|
||
angular.mock.module('ngJwtAuth', (_ngJwtAuthServiceProvider_) => { | ||
defaultAuthServiceProvider = _ngJwtAuthServiceProvider_; //register injection of service provider | ||
}); | ||
|
||
}); | ||
|
||
it('should have the default endpoints', () => { | ||
expect((<any>defaultAuthServiceProvider).config.apiEndpoints.base).to.equal('/api/auth'); | ||
expect((<any>defaultAuthServiceProvider).config.apiEndpoints.login).to.equal('/login'); | ||
expect((<any>defaultAuthServiceProvider).config.apiEndpoints.refresh).to.equal('/refresh'); | ||
}); | ||
|
||
beforeEach(()=>{ | ||
inject(function(_ngJwtAuthService_){ | ||
defaultAuthService = _ngJwtAuthService_; | ||
}) | ||
}); | ||
|
||
it('should have the default login endpoint', function() { | ||
expect((<any>defaultAuthService).getLoginEndpoint()).to.equal('/api/auth/login'); | ||
}); | ||
|
||
it('should have the default token exchange endpoint', function() { | ||
expect((<any>defaultAuthService).getTokenExchangeEndpoint()).to.equal('/api/auth/token'); | ||
}); | ||
|
||
it('should have the default refresh endpoint', function() { | ||
expect((<any>defaultAuthService).getRefreshEndpoint()).to.equal('/api/auth/refresh'); | ||
}); | ||
|
||
}); | ||
|
||
describe('Custom configuration', function () { | ||
|
||
let authServiceProvider:NgJwtAuthServiceProvider; | ||
let customAuthService:NgJwtAuthService; | ||
let partialCustomConfig:INgJwtAuthServiceConfig = { | ||
tokenLocation: 'token-custom', | ||
tokenUser: '#user-custom', | ||
apiEndpoints: { | ||
base: '/api/auth-custom', | ||
login: '/login-custom', | ||
tokenExchange: '/token-custom', | ||
refresh: '/refresh-custom', | ||
}, | ||
//storageKeyName: 'NgJwtAuthToken-custom', //intentionally commented out as this will be tested to be the default | ||
}; | ||
|
||
beforeEach(() => { | ||
|
||
angular.mock.module('ngJwtAuth', (_ngJwtAuthServiceProvider_) => { | ||
authServiceProvider = _ngJwtAuthServiceProvider_; //register injection of service provider | ||
|
||
authServiceProvider.configure(partialCustomConfig); | ||
}); | ||
|
||
}); | ||
|
||
it('should throw an exception when invalid configuration is passed', () => { | ||
|
||
let testInvalidConfigurationFn = () => { | ||
authServiceProvider.configure(<any>{invalid:'config'}); | ||
}; | ||
|
||
expect(testInvalidConfigurationFn).to.throw(NgJwtAuthException); | ||
|
||
}); | ||
|
||
it('should be able to partially configure the service provider', () => { | ||
|
||
expect((<any>authServiceProvider).config.apiEndpoints).to.deep.equal(partialCustomConfig.apiEndpoints); //assert that the custom value has come across | ||
|
||
expect((<any>authServiceProvider).config.storageKeyName).to.deep.equal((<any>authServiceProvider).config.storageKeyName); //assert that the default was not overridden | ||
|
||
}); | ||
|
||
beforeEach(()=>{ | ||
inject((_ngJwtAuthService_) => { | ||
customAuthService = _ngJwtAuthService_; | ||
}) | ||
}); | ||
|
||
it('should have the configured login endpoint', function() { | ||
expect((<any>customAuthService).getLoginEndpoint()).to.equal('/api/auth-custom/login-custom'); | ||
}); | ||
|
||
}); |
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
Oops, something went wrong.