Skip to content

Commit afd2a0f

Browse files
committed
Added tests for new methods
1 parent addd6ab commit afd2a0f

File tree

4 files changed

+95
-29
lines changed

4 files changed

+95
-29
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log
22

3+
## v0.1.0
4+
**Features:**
5+
- Added tests
6+
37
## v0.0.6 & v0.0.5
48
**Features:**
59
- Added `resetPassword()` for user password reset

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular2-token",
3-
"version": "0.0.6",
3+
"version": "0.1.0",
44
"description": "Angular2 service for token based authentication",
55
"main": "./angular2-token.js",
66
"typings": "./angular2-token.d.ts",

src/angular2-token.service.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class Angular2TokenService {
7676

7777
// Delete Account
7878
deleteAccount(): Observable<Response> {
79-
return this.delete(this._constructUserPath() + this._options.registerAccountPath).map(res => res.json());
79+
return this.delete(this._constructUserPath() + this._options.deleteAccountPath).map(res => res.json());
8080
}
8181

8282
// Sign in request and set storage
@@ -246,21 +246,23 @@ export class Angular2TokenService {
246246
// Try to get auth data from url parameters. Return null if parameter is missing.
247247
private _getAuthDataFromParams() {
248248

249-
this._router.routerState.queryParams.subscribe(params => {
250-
251-
let authData: AuthData = {
252-
accessToken: params['token'],
253-
client: params['client_id'],
254-
expiry: params['expiry'],
255-
tokenType: 'Bearer',
256-
uid: params['uid']
257-
};
258-
259-
if (this._checkIfComplete(authData))
260-
this._currentAuthData = authData;
261-
else
262-
this._currentAuthData = null;
263-
});
249+
if (this._router.routerState != null) { // Fix for Testing, has to be removed later
250+
this._router.routerState.queryParams.subscribe(params => {
251+
252+
let authData: AuthData = {
253+
accessToken: params['token'],
254+
client: params['client_id'],
255+
expiry: params['expiry'],
256+
tokenType: 'Bearer',
257+
uid: params['uid']
258+
};
259+
260+
if (this._checkIfComplete(authData))
261+
this._currentAuthData = authData;
262+
else
263+
this._currentAuthData = null;
264+
});
265+
}
264266
}
265267

266268

src/angular2-token.spec.ts

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Http, BaseRequestOptions, Response, ResponseOptions, Headers, RequestMe
22
import { MockBackend } from '@angular/http/testing';
33
import { provide } from '@angular/core';
44
import { inject, addProviders } from '@angular/core/testing';
5-
import { Router } from '@angular/router';
5+
import { Router, ActivatedRoute, RouterOutletMap, RouterState } from '@angular/router';
66

77
import { Angular2TokenService } from './';
88

@@ -39,19 +39,19 @@ describe('Angular2TokenService', () => {
3939
confirm_success_url: window.location.href
4040
}
4141

42+
class Mock { }
43+
4244
beforeEach(() => {
4345
// Inject HTTP and Angular2TokenService
4446
addProviders([
4547
BaseRequestOptions,
4648
MockBackend,
47-
Router,
48-
provide(Http, {
49-
useFactory:
50-
function (backend, defaultOptions) {
51-
return new Http(backend, defaultOptions);
52-
},
49+
{ provide: Router, useClass: Mock },
50+
{
51+
provide: Http,
52+
useFactory: (backend, defaultOptions) => { return new Http(backend, defaultOptions) },
5353
deps: [MockBackend, BaseRequestOptions]
54-
}),
54+
},
5555
Angular2TokenService
5656
]);
5757

@@ -115,18 +115,78 @@ describe('Angular2TokenService', () => {
115115
tokenService.registerAccount(registerData.email, registerData.password, registerData.password_confirmation);
116116
}));
117117

118-
// Testing Configured Configuration
118+
// Testing Custom Configuration
119119

120-
it('signIn method should send to configured api path', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
120+
it('Methods should send to configured path', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
121121

122122
mockBackend.connections.subscribe(
123-
c => expect(c.request.url).toEqual('myapi/auth/sign_in')
123+
c => expect(c.request.url).toEqual('myapi/myauth/mysignin')
124124
);
125125

126-
tokenService.init({ apiPath: 'myapi' });
126+
tokenService.init({ apiPath: 'myapi', signInPath: 'myauth/mysignin' });
127127
tokenService.signIn(signInData.email, signInData.password);
128128
}));
129129

130+
it('signOut should send to configured path', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
131+
132+
mockBackend.connections.subscribe(
133+
c => expect(c.request.url).toEqual('myapi/myauth/mysignout')
134+
);
135+
136+
tokenService.init({ apiPath: 'myapi', signOutPath: 'myauth/mysignout' });
137+
tokenService.signOut();
138+
}));
139+
140+
it('registerAccount should send to configured path', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
141+
142+
mockBackend.connections.subscribe(
143+
c => expect(c.request.url).toEqual('myapi/myauth/myregister')
144+
);
145+
146+
tokenService.init({ apiPath: 'myapi', registerAccountPath: 'myauth/myregister' });
147+
tokenService.registerAccount('example@example.org', 'password', 'password');
148+
}));
149+
150+
it('deleteAccount should send to configured path', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
151+
152+
mockBackend.connections.subscribe(
153+
c => expect(c.request.url).toEqual('myapi/myauth/mydelete')
154+
);
155+
156+
tokenService.init({ apiPath: 'myapi', deleteAccountPath: 'myauth/mydelete' });
157+
tokenService.deleteAccount();
158+
}));
159+
160+
it('validateToken should send to configured path', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
161+
162+
mockBackend.connections.subscribe(
163+
c => expect(c.request.url).toEqual('myapi/myauth/myvalidate')
164+
);
165+
166+
tokenService.init({ apiPath: 'myapi', validateTokenPath: 'myauth/myvalidate' });
167+
tokenService.validateToken();
168+
}));
169+
170+
it('updatePasswordPath should send to configured path', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
171+
172+
mockBackend.connections.subscribe(
173+
c => expect(c.request.url).toEqual('myapi/myauth/myupdate')
174+
);
175+
176+
tokenService.init({ apiPath: 'myapi', updatePasswordPath: 'myauth/myupdate' });
177+
tokenService.updatePassword('password', 'password');
178+
}));
179+
180+
it('resetPasswordPath should send to configured path', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
181+
182+
mockBackend.connections.subscribe(
183+
c => expect(c.request.url).toEqual('myapi/myauth/myreset')
184+
);
185+
186+
tokenService.init({ apiPath: 'myapi', resetPasswordPath: 'myauth/myreset' });
187+
tokenService.resetPassword('emxaple@example.org');
188+
}));
189+
130190
// Testing Token handling
131191

132192
it('signIn method should receive headers and set local storage', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {

0 commit comments

Comments
 (0)