forked from apache-superset/superset-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSupersetClientClass.test.ts
426 lines (357 loc) · 15.1 KB
/
SupersetClientClass.test.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import fetchMock from 'fetch-mock';
import { SupersetClientClass, ClientConfig } from '../src';
import { LOGIN_GLOB } from './fixtures/constants';
describe('SupersetClientClass', () => {
beforeAll(() => {
fetchMock.get(LOGIN_GLOB, { csrf_token: '' });
});
afterAll(fetchMock.restore);
describe('new SupersetClientClass()', () => {
it('fallback protocol to https when setting only host', () => {
const client = new SupersetClientClass({ host: 'TEST-HOST' });
expect(client.baseUrl).toEqual('https://test-host');
});
});
describe('.getUrl()', () => {
let client = new SupersetClientClass();
beforeEach(() => {
client = new SupersetClientClass({ protocol: 'https:', host: 'CONFIG_HOST' });
});
it('uses url if passed', () => {
expect(client.getUrl({ url: 'myUrl', endpoint: 'blah', host: 'blah' })).toBe('myUrl');
});
it('constructs a valid url from config.protocol + host + endpoint if passed', () => {
expect(client.getUrl({ endpoint: '/test', host: 'myhost' })).toBe('https://myhost/test');
expect(client.getUrl({ endpoint: '/test', host: 'myhost/' })).toBe('https://myhost/test');
expect(client.getUrl({ endpoint: 'test', host: 'myhost' })).toBe('https://myhost/test');
expect(client.getUrl({ endpoint: '/test/test//', host: 'myhost/' })).toBe(
'https://myhost/test/test//',
);
});
it('constructs a valid url from config.host + endpoint if host is omitted', () => {
expect(client.getUrl({ endpoint: '/test' })).toBe('https://config_host/test');
});
it('does not throw if url, endpoint, and host are all empty', () => {
client = new SupersetClientClass({ protocol: 'https:', host: '' });
expect(client.getUrl()).toBe('https://localhost/');
});
});
describe('.init()', () => {
afterEach(() => {
fetchMock.reset();
// reset
fetchMock.get(LOGIN_GLOB, { csrf_token: 1234 }, { overwriteRoutes: true });
});
it('calls superset/csrf_token/ when init() is called if no CSRF token is passed', async () => {
expect.assertions(1);
await new SupersetClientClass().init();
expect(fetchMock.calls(LOGIN_GLOB)).toHaveLength(1);
});
it('does NOT call superset/csrf_token/ when init() is called if a CSRF token is passed', async () => {
expect.assertions(1);
await new SupersetClientClass({ csrfToken: 'abc' }).init();
expect(fetchMock.calls(LOGIN_GLOB)).toHaveLength(0);
});
it('calls superset/csrf_token/ when init(force=true) is called even if a CSRF token is passed', async () => {
expect.assertions(4);
const initialToken = 'initial_token';
const client = new SupersetClientClass({ csrfToken: initialToken });
await client.init();
expect(fetchMock.calls(LOGIN_GLOB)).toHaveLength(0);
expect(client.csrfToken).toBe(initialToken);
await client.init(true);
expect(fetchMock.calls(LOGIN_GLOB)).toHaveLength(1);
expect(client.csrfToken).not.toBe(initialToken);
});
it('throws if superset/csrf_token/ returns an error', async () => {
expect.assertions(1);
const rejectError = { status: 403 };
fetchMock.get(LOGIN_GLOB, () => Promise.reject(rejectError), {
overwriteRoutes: true,
});
try {
await new SupersetClientClass({}).init();
} catch (error) {
expect(error as typeof rejectError).toEqual(rejectError);
}
});
const invalidCsrfTokenError = { error: 'Failed to fetch CSRF token' };
it('throws if superset/csrf_token/ does not return a token', async () => {
expect.assertions(1);
fetchMock.get(LOGIN_GLOB, {}, { overwriteRoutes: true });
try {
await new SupersetClientClass({}).init();
} catch (error) {
expect(error as typeof invalidCsrfTokenError).toEqual(invalidCsrfTokenError);
}
});
it('does not set csrfToken if response is not json', async () => {
expect.assertions(1);
fetchMock.get(LOGIN_GLOB, '123', {
overwriteRoutes: true,
});
try {
await new SupersetClientClass({}).init();
} catch (error) {
expect(error as typeof invalidCsrfTokenError).toEqual(invalidCsrfTokenError);
}
});
});
describe('.isAuthenticated()', () => {
afterEach(fetchMock.reset);
it('returns true if there is a token and false if not', async () => {
expect.assertions(2);
const client = new SupersetClientClass({});
expect(client.isAuthenticated()).toBe(false);
await client.init();
expect(client.isAuthenticated()).toBe(true);
});
it('returns true if a token is passed at configuration', () => {
expect.assertions(2);
const clientWithoutToken = new SupersetClientClass({ csrfToken: undefined });
const clientWithToken = new SupersetClientClass({ csrfToken: 'token' });
expect(clientWithoutToken.isAuthenticated()).toBe(false);
expect(clientWithToken.isAuthenticated()).toBe(true);
});
});
describe('.ensureAuth()', () => {
it(`returns a promise that rejects if .init() has not been called`, async () => {
expect.assertions(2);
const client = new SupersetClientClass({});
try {
await client.ensureAuth();
} catch (error) {
expect(error).toEqual({ error: expect.any(String) });
}
expect(client.isAuthenticated()).toBe(false);
});
it('returns a promise that resolves if .init() resolves successfully', async () => {
expect.assertions(1);
const client = new SupersetClientClass({});
await client.init();
await client.ensureAuth();
expect(client.isAuthenticated()).toBe(true);
});
it(`returns a promise that rejects if .init() is unsuccessful`, async () => {
expect.assertions(4);
const rejectValue = { status: 403 };
fetchMock.get(LOGIN_GLOB, () => Promise.reject(rejectValue), {
overwriteRoutes: true,
});
const client = new SupersetClientClass({});
try {
await client.init();
} catch (error) {
expect(error).toEqual(expect.objectContaining(rejectValue));
expect(client.isAuthenticated()).toBe(false);
try {
await client.ensureAuth();
} catch (error2) {
expect(error2).toEqual(expect.objectContaining(rejectValue));
expect(client.isAuthenticated()).toBe(false);
}
}
// reset
fetchMock.get(
LOGIN_GLOB,
{ csrf_token: 1234 },
{
overwriteRoutes: true,
},
);
});
});
describe('requests', () => {
afterEach(fetchMock.reset);
const protocol = 'https:';
const host = 'host';
const mockGetEndpoint = '/get/url';
const mockRequestEndpoint = '/request/url';
const mockPostEndpoint = '/post/url';
const mockPutEndpoint = '/put/url';
const mockDeleteEndpoint = '/delete/url';
const mockTextEndpoint = '/text/endpoint';
const mockGetUrl = `${protocol}//${host}${mockGetEndpoint}`;
const mockRequestUrl = `${protocol}//${host}${mockRequestEndpoint}`;
const mockPostUrl = `${protocol}//${host}${mockPostEndpoint}`;
const mockTextUrl = `${protocol}//${host}${mockTextEndpoint}`;
const mockPutUrl = `${protocol}//${host}${mockPutEndpoint}`;
const mockDeleteUrl = `${protocol}//${host}${mockDeleteEndpoint}`;
const mockTextJsonResponse = '{ "value": 9223372036854775807 }';
const mockPayload = { json: () => Promise.resolve('payload') };
fetchMock.get(mockGetUrl, mockPayload);
fetchMock.post(mockPostUrl, mockPayload);
fetchMock.put(mockPutUrl, mockPayload);
fetchMock.delete(mockDeleteUrl, mockPayload);
fetchMock.delete(mockRequestUrl, mockPayload);
fetchMock.get(mockTextUrl, mockTextJsonResponse);
fetchMock.post(mockTextUrl, mockTextJsonResponse);
it('checks for authentication before every get and post request', async () => {
expect.assertions(6);
const authSpy = jest.spyOn(SupersetClientClass.prototype, 'ensureAuth');
const client = new SupersetClientClass({ protocol, host });
await client.init();
await client.get({ url: mockGetUrl });
await client.post({ url: mockPostUrl });
await client.put({ url: mockPutUrl });
await client.delete({ url: mockDeleteUrl });
await client.request({ url: mockRequestUrl, method: 'DELETE' });
expect(fetchMock.calls(mockGetUrl)).toHaveLength(1);
expect(fetchMock.calls(mockPostUrl)).toHaveLength(1);
expect(fetchMock.calls(mockDeleteUrl)).toHaveLength(1);
expect(fetchMock.calls(mockPutUrl)).toHaveLength(1);
expect(fetchMock.calls(mockRequestUrl)).toHaveLength(1);
expect(authSpy).toHaveBeenCalledTimes(5);
authSpy.mockRestore();
});
it('sets protocol, host, headers, mode, and credentials from config', async () => {
expect.assertions(3);
const clientConfig: ClientConfig = {
host,
protocol,
mode: 'cors',
credentials: 'include',
headers: { my: 'header' },
};
const client = new SupersetClientClass(clientConfig);
await client.init();
await client.get({ url: mockGetUrl });
const fetchRequest = fetchMock.calls(mockGetUrl)[0][1];
expect(fetchRequest.mode).toBe(clientConfig.mode);
expect(fetchRequest.credentials).toBe(clientConfig.credentials);
expect(fetchRequest.headers).toEqual(
expect.objectContaining(clientConfig.headers) as typeof fetchRequest.headers,
);
});
describe('.get()', () => {
it('makes a request using url or endpoint', async () => {
expect.assertions(2);
const client = new SupersetClientClass({ protocol, host });
await client.init();
await client.get({ url: mockGetUrl });
expect(fetchMock.calls(mockGetUrl)).toHaveLength(1);
await client.get({ endpoint: mockGetEndpoint });
expect(fetchMock.calls(mockGetUrl)).toHaveLength(2);
});
it('supports parsing a response as text', async () => {
expect.assertions(2);
const client = new SupersetClientClass({ protocol, host });
await client.init();
const { text } = await client.get({ url: mockTextUrl, parseMethod: 'text' });
expect(fetchMock.calls(mockTextUrl)).toHaveLength(1);
expect(text).toBe(mockTextJsonResponse);
});
it('allows overriding host, headers, mode, and credentials per-request', async () => {
expect.assertions(3);
const clientConfig: ClientConfig = {
host,
protocol,
mode: 'cors',
credentials: 'include',
headers: { my: 'header' },
};
const overrideConfig: ClientConfig = {
host: 'override_host',
mode: 'no-cors',
credentials: 'omit',
headers: { my: 'override', another: 'header' },
};
const client = new SupersetClientClass(clientConfig);
await client.init();
await client.get({ url: mockGetUrl, ...overrideConfig });
const fetchRequest = fetchMock.calls(mockGetUrl)[0][1];
expect(fetchRequest.mode).toBe(overrideConfig.mode);
expect(fetchRequest.credentials).toBe(overrideConfig.credentials);
expect(fetchRequest.headers).toEqual(
expect.objectContaining(overrideConfig.headers) as typeof fetchRequest.headers,
);
});
});
describe('.post()', () => {
it('makes a request using url or endpoint', async () => {
expect.assertions(2);
const client = new SupersetClientClass({ protocol, host });
await client.init();
await client.post({ url: mockPostUrl });
expect(fetchMock.calls(mockPostUrl)).toHaveLength(1);
await client.post({ endpoint: mockPostEndpoint });
expect(fetchMock.calls(mockPostUrl)).toHaveLength(2);
});
it('allows overriding host, headers, mode, and credentials per-request', async () => {
expect.assertions(3);
const clientConfig: ClientConfig = {
host,
protocol,
mode: 'cors',
credentials: 'include',
headers: { my: 'header' },
};
const overrideConfig: ClientConfig = {
host: 'override_host',
mode: 'no-cors',
credentials: 'omit',
headers: { my: 'override', another: 'header' },
};
const client = new SupersetClientClass(clientConfig);
await client.init();
await client.post({ url: mockPostUrl, ...overrideConfig });
const fetchRequest = fetchMock.calls(mockPostUrl)[0][1];
expect(fetchRequest.mode).toBe(overrideConfig.mode);
expect(fetchRequest.credentials).toBe(overrideConfig.credentials);
expect(fetchRequest.headers).toEqual(
expect.objectContaining(overrideConfig.headers) as typeof fetchRequest.headers,
);
});
it('supports parsing a response as text', async () => {
expect.assertions(2);
const client = new SupersetClientClass({ protocol, host });
await client.init();
const { text } = await client.post({ url: mockTextUrl, parseMethod: 'text' });
expect(fetchMock.calls(mockTextUrl)).toHaveLength(1);
expect(text).toBe(mockTextJsonResponse);
});
it('passes postPayload key,values in the body', async () => {
expect.assertions(3);
const postPayload = { number: 123, array: [1, 2, 3] };
const client = new SupersetClientClass({ protocol, host });
await client.init();
await client.post({ url: mockPostUrl, postPayload });
const formData = fetchMock.calls(mockPostUrl)[0][1].body as FormData;
expect(fetchMock.calls(mockPostUrl)).toHaveLength(1);
Object.entries(postPayload).forEach(([key, value]) => {
expect(formData.get(key)).toBe(JSON.stringify(value));
});
});
it('respects the stringify parameter for postPayload key,values', async () => {
expect.assertions(3);
const postPayload = { number: 123, array: [1, 2, 3] };
const client = new SupersetClientClass({ protocol, host });
await client.init();
await client.post({ url: mockPostUrl, postPayload, stringify: false });
const formData = fetchMock.calls(mockPostUrl)[0][1].body as FormData;
expect(fetchMock.calls(mockPostUrl)).toHaveLength(1);
Object.entries(postPayload).forEach(([key, value]) => {
expect(formData.get(key)).toBe(String(value));
});
});
});
});
});