-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.spec.ts
274 lines (191 loc) · 8.27 KB
/
auth.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import {TestBed, inject, fakeAsync, tick} from "@angular/core/testing";
import {MockBackend, MockConnection} from "@angular/http/testing";
import {BaseRequestOptions, Http, HttpModule, ResponseOptions, Response, RequestMethod} from "@angular/http";
import {TESTHEADERS, TESTURL, TESTCONFIG, TESTAUTHSUCESSRES} from "./helpers";
import * as utils from '../src/utils/utils'
import {SelfbitsAuth} from "../src/services/auth";
import {SelfbitsAuthConfig} from "../src/utils/interfaces";
import {SELFBITS_CONFIG} from "../src/utils/tokens";
describe('auth.ts',()=> {
beforeEach(()=> {
TestBed.configureTestingModule({
providers: [
MockBackend,
BaseRequestOptions,
{
provide: Http, useFactory: (backend: MockBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backend, defaultOptions);
}, deps: [MockBackend, BaseRequestOptions]
},
{ provide: SELFBITS_CONFIG, useValue:TESTCONFIG},
SelfbitsAuth
],
imports: [
HttpModule
]
})
});
it('should load with config data injected',inject([SelfbitsAuth],(auth:any)=>{
expect(auth.config).toEqual(TESTCONFIG);
expect(auth.headers).toEqual(TESTHEADERS);
expect(auth.baseUrl).toEqual(TESTURL);
}));
it('checkForToken() should append Authorization header ', inject([SelfbitsAuth],(auth:any)=>{
window.localStorage.setItem('token','httpTestToken');
expect(auth.headers.has('Authorization')).toBeFalsy();
auth.checkForToken();
expect(auth.headers.get('Authorization')).toEqual('Bearer httpTestToken')
}));
it('logout() should remove token, userId and expires',inject([SelfbitsAuth],(auth:SelfbitsAuth)=>{
window.localStorage.setItem('token', 'authTesttoken');
window.localStorage.setItem('userId', 'authTestUserId');
window.localStorage.setItem('expires', 'authTestExpiration');
auth.logout();
expect(window.localStorage.hasOwnProperty('token')).toBeFalsy();
expect(window.localStorage.hasOwnProperty('userId')).toBeFalsy();
expect(window.localStorage.hasOwnProperty('expires')).toBeFalsy();
}));
it('getUserId() should getuserId',inject ([SelfbitsAuth],(auth:SelfbitsAuth)=>{
window.localStorage.setItem('userId', 'authTestUserId');
expect(auth.getUserId()).toEqual('authTestUserId');
}));
describe('check methodes', ()=>{
let auth:SelfbitsAuth;
let backend:MockBackend;
let checkTokenSpy:any;
let response:any;
let user: SelfbitsAuthConfig = {
email: '[email protected]',
password: 'test'
};
beforeEach(inject([SelfbitsAuth,MockBackend],(testAuth:SelfbitsAuth, testBackend:MockBackend)=>{
auth = testAuth;
backend = testBackend;
checkTokenSpy = spyOn(testAuth,'checkForToken');
}));
afterEach(()=>{
auth = null;
backend = null;
response = null;
window.localStorage.removeItem('token');
window.localStorage.removeItem('userId');
window.localStorage.removeItem('expires');
});
it('should inject header correctly', fakeAsync(()=>{
backend.connections.subscribe((connection: MockConnection) => {
expect(connection.request.headers.get('sb-app-id')).toEqual('fancyId');
expect(connection.request.headers.get('sb-app-secret')).toEqual('fancySecret');
expect(connection.request.headers.get('Content-Type')).toEqual('application/json');
});
auth.login(user);
auth.signup(user);
auth.unlink('selfbits');
auth.password('newPassword','oldPassword');
}));
it('login() should work and set token', fakeAsync(() => {
backend.connections.subscribe((connection: MockConnection) => {
expect(connection.request.method).toBe(RequestMethod.Post);
expect(connection.request.getBody()).toEqual(JSON.stringify(user));
let response = new ResponseOptions({body:JSON.stringify(TESTAUTHSUCESSRES)});
connection.mockRespond(new Response(response));
});
auth.login(user).subscribe(res => {
response = res;
});
tick();
expect(window.localStorage.getItem('token')).toEqual('fancyToken');
expect(window.localStorage.getItem('userId')).toEqual('fancyUserId');
expect(window.localStorage.getItem('expires')).toEqual('fancyExpiration');
}));
it('signup() should work and set token', fakeAsync(() => {
backend.connections.subscribe((connection: MockConnection) => {
expect(connection.request.method).toBe(RequestMethod.Post);
expect(connection.request.getBody()).toEqual(JSON.stringify(user));
let response = new ResponseOptions({body:JSON.stringify(TESTAUTHSUCESSRES)});
connection.mockRespond(new Response(response));
});
auth.signup(user).subscribe(res => {
response = res;
});
tick();
expect(window.localStorage.getItem('token')).toEqual('fancyToken');
expect(window.localStorage.getItem('userId')).toEqual('fancyUserId');
expect(window.localStorage.getItem('expires')).toEqual('fancyExpiration');
}));
it('signupAnonymous() should login user and return token', fakeAsync(()=>{
backend.connections.subscribe((connection:MockConnection)=>{
expect(connection.request.method).toBe(RequestMethod.Post);
let response = new ResponseOptions({body:JSON.stringify(TESTAUTHSUCESSRES)});
connection.mockRespond(new Response(response));
});
auth.signupAnonymous().subscribe(res => {
response = res.json()
});
tick();
expect(window.localStorage.getItem('token')).toEqual('fancyToken');
expect(window.localStorage.getItem('userId')).toEqual('fancyUserId');
expect(window.localStorage.getItem('expires')).toEqual('fancyExpiration');
}));
it('password() should change password if user is logged and token exists', fakeAsync(() => {
backend.connections.subscribe((connection: MockConnection) => {
expect(connection.request.method).toBe(RequestMethod.Post);
expect(connection.request.getBody()).toEqual(JSON.stringify(changePassword));
let mockResponseBody = { data: 'authTestData' };
let response = new ResponseOptions({ body: JSON.stringify(mockResponseBody) });
connection.mockRespond(new Response(response));
});
let changePassword = {
newPassword: 'newPassword',
oldPassword: 'oldPassword'
};
auth.password(changePassword.newPassword, changePassword.oldPassword).subscribe(res => {
response = res.json();
});
tick();
expect(response.data).toEqual('authTestData');
expect(checkTokenSpy).toHaveBeenCalled();
}));
it('social() should allow user to login with social provider and set token', fakeAsync(() => {
let guidSpy = sinon.spy(utils, 'sbGuid');
let windowStub = sinon.stub(window,'open',(url:string)=>{
let genGuild = guidSpy.getCall(0).returnValue + guidSpy.getCall(1).returnValue;
let popupUrl = `${TESTCONFIG.BASE_URL}/api/v1/oauth/facebook?sb_app_id=${TESTCONFIG.APP_ID}&sb_app_secret=${TESTCONFIG.APP_SECTRET}&state=${genGuild}`;
expect(url).toEqual(popupUrl);
backend.connections.subscribe( (connection:MockConnection)=>{
expect(connection.request.method).toBe(RequestMethod.Get);
expect(connection.request.headers.get('sb-app-id')).toEqual('fancyId');
expect(connection.request.headers.get('sb-app-secret')).toEqual('fancySecret');
expect(connection.request.headers.get('Content-Type')).toEqual('application/json');
let getTokenUrl = `${TESTCONFIG.BASE_URL}/api/v1/oauth/facebook/token?state=${genGuild}`;
expect(connection.request.url).toEqual(getTokenUrl);
let response = new ResponseOptions({body:JSON.stringify(TESTAUTHSUCESSRES)});
connection.mockRespond(new Response(response));
});
return {closed: true}
});
auth.social('facebook').subscribe(res => {
});
tick(1000);
expect(window.localStorage.getItem('token')).toEqual('fancyToken');
expect(window.localStorage.getItem('userId')).toEqual('fancyUserId');
expect(window.localStorage.getItem('expires')).toEqual('fancyExpiration');
}));
it('isAuthenticated() should return check user state',fakeAsync(()=>{
backend.connections.subscribe( (connection:MockConnection)=>{
expect(connection.request.method).toBe(RequestMethod.Get);
expect(connection.request.url).toEqual('www.test.io/api/v1/user');
let res = {
status:200
};
let response = new ResponseOptions({status:200});
connection.mockRespond(new Response(response));
});
auth.isAuthenticated().subscribe(res => {
response = res;
});
tick();
expect(response).toBeTruthy();
expect(checkTokenSpy).toHaveBeenCalled();
}))
})
});