This repository was archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathauth-http.spec.ts
143 lines (120 loc) · 4.37 KB
/
auth-http.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
import { ReflectiveInjector } from '@angular/core';
import { BaseRequestOptions, ConnectionBackend, RequestOptions } from '@angular/http';
import { Response, ResponseOptions } from '@angular/http';
import { MockBackend, MockConnection } from '@angular/http/testing';
import { BBAuth } from '@blackbaud/auth-client';
import { SkyAuthHttp } from './auth-http';
import { SkyAppConfig } from './config';
import { SkyAppRuntimeConfigParams } from './params';
import { SkyAuthTokenProvider } from './auth-token-provider';
describe('SkyAuthHttp', () => {
let skyAuthHttp: SkyAuthHttp;
let lastConnection: MockConnection;
function setupInjector(url: string) {
const injector = ReflectiveInjector.resolveAndCreate([
SkyAuthTokenProvider,
SkyAuthHttp,
{
provide: ConnectionBackend,
useClass: MockBackend
},
{
provide: RequestOptions,
useClass: BaseRequestOptions
},
{
provide: SkyAppConfig,
useValue: {
runtime: {
params: new SkyAppRuntimeConfigParams(url, [
'envid',
'svcid'
])
}
}
}
]);
skyAuthHttp = injector.get(SkyAuthHttp);
const backend = injector.get(ConnectionBackend) as MockBackend;
backend.connections.subscribe((connection: MockConnection) => {
lastConnection = connection;
connection.mockRespond(new Response(new ResponseOptions({})));
});
}
it('should call BBAuth.getToken and add token as header', (done) => {
const token = 'my-fake-token';
const getTokenSpy = spyOn(BBAuth, 'getToken').and.returnValue(Promise.resolve(token));
setupInjector('');
skyAuthHttp.get('my-bff-url.com').subscribe(() => {
expect(lastConnection.request.headers.get('Authorization')).toEqual(`Bearer ${token}`);
expect(getTokenSpy).toHaveBeenCalled();
done();
});
});
it('should include envid if it is in the current url', (done) => {
const search = '?envid=1234';
spyOn(BBAuth, 'getToken').and.returnValue(Promise.resolve());
setupInjector(search);
skyAuthHttp.get('example.com').subscribe(() => {
expect(lastConnection.request.url).toContain(search);
done();
});
});
it('should include svc if it is in the current url', (done) => {
const search = '?svcid=1234';
spyOn(BBAuth, 'getToken').and.returnValue(Promise.resolve());
setupInjector(search);
skyAuthHttp.get('example.com').subscribe(() => {
expect(lastConnection.request.url).toContain(search);
done();
});
});
it('should include envid and svcid if they are in the current url', (done) => {
const search = '?envid=1234&svcid=5678';
spyOn(BBAuth, 'getToken').and.returnValue(Promise.resolve());
setupInjector(search);
skyAuthHttp.get('example.com').subscribe(() => {
expect(lastConnection.request.url).toContain(search);
done();
});
});
it('should not pass through unknown query params', (done) => {
const search = '?junk=asdf';
spyOn(BBAuth, 'getToken').and.returnValue(Promise.resolve());
setupInjector(search);
skyAuthHttp.get('example.com').subscribe(() => {
expect(lastConnection.request.url).not.toContain(search);
done();
});
});
it('should handle a requested url with a querystring', (done) => {
const url = 'example.com?custom=true';
const search = 'envid=asdf';
spyOn(BBAuth, 'getToken').and.returnValue(Promise.resolve());
setupInjector('?' + search);
skyAuthHttp.get(url).subscribe(() => {
expect(lastConnection.request.url).toEqual(url + '&' + search);
done();
});
});
it('should handle being passed a url string (instead of Request)', (done) => {
const url = 'url-as-string.com';
spyOn(BBAuth, 'getToken').and.returnValue(Promise.resolve());
setupInjector('');
skyAuthHttp.request(url).subscribe(() => {
expect(lastConnection.request.url).toEqual(url);
done();
});
});
/**
* PLEASE NOTE
* If this tests fails, it means you've changed the required parameters to the constructor.
* To successfully maintain backwards compatibility:
* - make any new parameters optional
* - add any new parameters to the end of the constructor
*/
it('should maintain backwards compatibility if new parameters added to the constructor', () => {
setupInjector('');
expect(skyAuthHttp).toBeDefined();
});
});