forked from neroniaky/angular-token
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular2-token.spec.ts
235 lines (183 loc) · 7.34 KB
/
angular2-token.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { Http, BaseRequestOptions, Response, ResponseOptions, Headers, RequestMethod } from '@angular/http';
import { MockBackend } from '@angular/http/testing';
import { inject, TestBed } from '@angular/core/testing';
import { ActivatedRoute, RouterOutletMap, RouterState } from '@angular/router';
import { Angular2TokenService } from './angular2-token.service';
describe('Angular2TokenService', () => {
// Init common test data
let tokenType = 'Bearer';
let uid = 'test@test.com';
let accessToken = 'fJypB1ugmWHJfW6CELNfug';
let client = '5dayGs4hWTi4eKwSifu_mg';
let expiry = '1472108318';
let emptyHeaders = new Headers({
'content-Type': 'application/json'
});
let tokenHeaders = new Headers({
'content-Type': 'application/json',
'token-type': tokenType,
'uid': uid,
'access-token': accessToken,
'client': client,
'expiry': expiry
});
let signInData = {
email: 'test@test.de',
password: 'password'
}
let registerData = {
email: 'test@test.de',
password: 'password',
password_confirmation: 'password',
confirm_success_url: window.location.href
}
class Mock { }
beforeEach(() => {
// Inject HTTP and Angular2TokenService
TestBed.configureTestingModule({
providers: [
BaseRequestOptions,
MockBackend,
{ provide: ActivatedRoute, useClass: Mock },
{
provide: Http,
useFactory: (backend, defaultOptions) => { return new Http(backend, defaultOptions) },
deps: [MockBackend, BaseRequestOptions]
},
Angular2TokenService
]
});
// Fake Local Storage
var store = {};
spyOn(localStorage, 'getItem').and.callFake((key: string): String => {
return store[key] || null;
});
spyOn(localStorage, 'removeItem').and.callFake((key: string): void => {
delete store[key];
});
spyOn(localStorage, 'setItem').and.callFake((key: string, value: string): string => {
return store[key] = <string>value;
});
spyOn(localStorage, 'clear').and.callFake(() => {
store = {};
});
});
// Testing Default Configuration
it('signIn method should post data to default url', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
mockBackend.connections.subscribe(
c => {
expect(c.request.getBody()).toEqual(JSON.stringify(signInData));
expect(c.request.method).toEqual(RequestMethod.Post);
expect(c.request.url).toEqual('auth/sign_in');
}
);
tokenService.init();
tokenService.signIn(signInData.email, signInData.password);
}));
it('signOut method should delete to default url', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
mockBackend.connections.subscribe(
c => {
expect(c.request.method).toEqual(RequestMethod.Delete);
expect(c.request.url).toEqual('auth/sign_out');
}
);
tokenService.init();
tokenService.signOut();
}));
it('registerAccount should post data to default url', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
mockBackend.connections.subscribe(
c => {
expect(c.request.getBody()).toEqual(JSON.stringify(registerData));
expect(c.request.method).toEqual(RequestMethod.Post);
expect(c.request.url).toEqual('auth');
}
);
tokenService.init();
tokenService.registerAccount(registerData.email, registerData.password, registerData.password_confirmation);
}));
// Testing Custom Configuration
it('Methods should send to configured path', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
mockBackend.connections.subscribe(
c => expect(c.request.url).toEqual('myapi/myauth/mysignin')
);
tokenService.init({ apiPath: 'myapi', signInPath: 'myauth/mysignin' });
tokenService.signIn(signInData.email, signInData.password);
}));
it('signOut should send to configured path', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
mockBackend.connections.subscribe(
c => expect(c.request.url).toEqual('myapi/myauth/mysignout')
);
tokenService.init({ apiPath: 'myapi', signOutPath: 'myauth/mysignout' });
tokenService.signOut();
}));
it('registerAccount should send to configured path', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
mockBackend.connections.subscribe(
c => expect(c.request.url).toEqual('myapi/myauth/myregister')
);
tokenService.init({ apiPath: 'myapi', registerAccountPath: 'myauth/myregister' });
tokenService.registerAccount('example@example.org', 'password', 'password');
}));
it('deleteAccount should send to configured path', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
mockBackend.connections.subscribe(
c => expect(c.request.url).toEqual('myapi/myauth/mydelete')
);
tokenService.init({ apiPath: 'myapi', deleteAccountPath: 'myauth/mydelete' });
tokenService.deleteAccount();
}));
it('validateToken should send to configured path', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
mockBackend.connections.subscribe(
c => expect(c.request.url).toEqual('myapi/myauth/myvalidate')
);
tokenService.init({ apiPath: 'myapi', validateTokenPath: 'myauth/myvalidate' });
tokenService.validateToken();
}));
it('updatePasswordPath should send to configured path', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
mockBackend.connections.subscribe(
c => expect(c.request.url).toEqual('myapi/myauth/myupdate')
);
tokenService.init({ apiPath: 'myapi', updatePasswordPath: 'myauth/myupdate' });
tokenService.updatePassword('password', 'password');
}));
it('resetPasswordPath should send to configured path', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
mockBackend.connections.subscribe(
c => expect(c.request.url).toEqual('myapi/myauth/myreset')
);
tokenService.init({ apiPath: 'myapi', resetPasswordPath: 'myauth/myreset' });
tokenService.resetPassword('emxaple@example.org');
}));
// Testing Token handling
it('signIn method should receive headers and set local storage', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
mockBackend.connections.subscribe(
c => c.mockRespond(new Response(
new ResponseOptions({
headers: tokenHeaders,
body: { email: 'test@email.com' }
})
))
);
tokenService.init();
tokenService.signIn(signInData.email, signInData.password);
expect(localStorage.getItem('accessToken')).toEqual(accessToken);
expect(localStorage.getItem('client')).toEqual(client);
expect(localStorage.getItem('expiry')).toEqual(expiry);
expect(localStorage.getItem('tokenType')).toEqual(tokenType);
expect(localStorage.getItem('uid')).toEqual(uid);
}));
it('signOut method should clear local storage', inject([Angular2TokenService, MockBackend], (tokenService, mockBackend) => {
localStorage.setItem('token-type', tokenType);
localStorage.setItem('uid', uid);
localStorage.setItem('access-token', accessToken);
localStorage.setItem('client', client);
localStorage.setItem('expiry', expiry);
mockBackend.connections.subscribe(
c => expect(c.request.method).toEqual(RequestMethod.Delete)
);
tokenService.init();
tokenService.signOut();
expect(localStorage.getItem('accessToken')).toBe(null);
expect(localStorage.getItem('client')).toBe(null);
expect(localStorage.getItem('expiry')).toBe(null);
expect(localStorage.getItem('tokenType')).toBe(null);
expect(localStorage.getItem('uid')).toBe(null);
}));
});