-
Notifications
You must be signed in to change notification settings - Fork 747
/
azureAuthWorkflowService.spec.ts
146 lines (128 loc) · 4.23 KB
/
azureAuthWorkflowService.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
import '../fetchProxy';
import { AzureAuthWorkflowService } from './azureAuthWorkflowService';
import { BrowserWindow } from 'electron';
const mockEvent = Event; // this is silly but required by jest
const mockArmToken = 'eyJhbGciOiJSU0EyNTYiLCJraWQiOiJmZGtqc2FoamdmIiwieDV0IjoiZiJ9.' +
'eyJ1cG4iOiJnbGFzZ293QHNjb3RsYW5kLmNvbSJ9.' +
'7gjdshgfdsk98458205jfds9843fjds';
jest.mock('jsonwebtoken', () => ({
verify: () => true
}));
let mockResponses;
jest.mock('node-fetch', () => {
const fetch = (url, opts) => {
return {
ok: true,
json: async () => mockResponses.pop(),
text: async () => '{}',
};
};
(fetch as any).Headers = class {
};
(fetch as any).Response = class {
};
return fetch;
});
jest.mock('rsa-pem-from-mod-exp', () => () => ({}));
jest.mock('electron', () => ({
BrowserWindow: class MockBrowserWindow {
public static reporters = [];
public listeners = [] as any;
public webContents = { history: ['http://someotherUrl', `http://localhost/#t=13&id_token=${mockArmToken}`] };
private static report(...args: any[]) {
this.reporters.forEach(r => r(args));
}
constructor(...args: any[]) {
MockBrowserWindow.report('constructor', ...args);
}
setMenu() {
// no-op
}
addListener(type: string, handler: (event: any) => void) {
this.listeners.push({ type, handler });
MockBrowserWindow.report('addListener', type, handler);
if (type === 'page-title-updated') {
[['http://someotherUrl'], [`http://localhost/#t=13&id_token=${mockArmToken}`]].forEach((url, index) => {
let evt = new mockEvent('page-title-updated');
(evt as any).sender = { history: [`http://localhost/#t=13&id_token=${mockArmToken}`] };
setTimeout(() => {
this.listeners.forEach(l => l.type === evt.type && l.handler(evt));
}, 25 * index);
});
}
}
once(type: string, handler: (event: any) => void) {
this.listeners.push({ type, handler });
MockBrowserWindow.report('once', type, handler);
}
dispatch(event: any) {
this.listeners.forEach(l => l.type === event.type && l.handler(event));
MockBrowserWindow.report('dispatch', event);
}
show() {
MockBrowserWindow.report('show');
}
close() {
MockBrowserWindow.report('hide');
}
loadURL(url: string) {
MockBrowserWindow.report('loadURL', url);
let evt = new mockEvent('ready-to-show');
setTimeout(() => {
this.listeners.forEach(l => l.type === evt.type && l.handler(evt));
});
}
}
}));
describe('The azureAuthWorkflowService', () => {
beforeEach(() => {
mockResponses = [
{ access_token: mockArmToken },
{ jwks_uri: 'http://localhost', keys: { find: () => ({}) } },
{ authorization_endpoint: 'http://localhost', jwks_uri: 'http://localhost', token_endpoint: 'http://localhost' }
];
(BrowserWindow as any).reporters = [];
});
it('should make the appropriate calls and receive the expected values with the "retrieveAuthToken"', async () => {
let reportedValues = [];
let reporter = v => reportedValues.push(v);
(BrowserWindow as any).reporters.push(reporter);
const it = AzureAuthWorkflowService.retrieveAuthToken(false, '');
let value = undefined;
let ct = 0;
while (true) {
let next = it.next(value);
if (next.done) {
break;
}
value = await next.value;
if (!ct) {
expect(value instanceof BrowserWindow).toBe(true);
expect(reportedValues.length).toBe(3);
const [, uri] = reportedValues[1];
const idx = uri.indexOf('#');
const parts = uri.substring(idx).split('&');
[
'response_type',
'client_id',
'redirect_uri',
'state',
'client-request-id'
].forEach((part, index) => {
expect(parts[index].includes(part));
});
reportedValues.length = 0;
}
if (ct === 1) {
expect(value.id_token).toBe(mockArmToken);
// Not sure if this is valuable or not.
expect(reportedValues.length).toBe(3);
}
if (ct === 2) {
expect(value.access_token).toBe(mockArmToken);
}
ct++;
}
expect(ct).toBe(4);
});
});