SupersetClient.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import fetchMock from 'fetch-mock';
  20. import { SupersetClient, SupersetClientClass } from '@superset-ui/core';
  21. import { LOGIN_GLOB } from './fixtures/constants';
  22. describe('SupersetClient', () => {
  23. beforeAll(() => fetchMock.get(LOGIN_GLOB, { result: '' }));
  24. afterAll(() => fetchMock.restore());
  25. afterEach(() => SupersetClient.reset());
  26. it('exposes reset, configure, init, get, post, postForm, isAuthenticated, and reAuthenticate methods', () => {
  27. expect(typeof SupersetClient.configure).toBe('function');
  28. expect(typeof SupersetClient.init).toBe('function');
  29. expect(typeof SupersetClient.get).toBe('function');
  30. expect(typeof SupersetClient.post).toBe('function');
  31. expect(typeof SupersetClient.postForm).toBe('function');
  32. expect(typeof SupersetClient.isAuthenticated).toBe('function');
  33. expect(typeof SupersetClient.reAuthenticate).toBe('function');
  34. expect(typeof SupersetClient.getGuestToken).toBe('function');
  35. expect(typeof SupersetClient.request).toBe('function');
  36. expect(typeof SupersetClient.reset).toBe('function');
  37. });
  38. it('throws if you call init, get, post, postForm, isAuthenticated, or reAuthenticate before configure', () => {
  39. expect(SupersetClient.init).toThrow();
  40. expect(SupersetClient.get).toThrow();
  41. expect(SupersetClient.post).toThrow();
  42. expect(SupersetClient.postForm).toThrow();
  43. expect(SupersetClient.isAuthenticated).toThrow();
  44. expect(SupersetClient.reAuthenticate).toThrow();
  45. expect(SupersetClient.request).toThrow();
  46. expect(SupersetClient.configure).not.toThrow();
  47. });
  48. // this also tests that the ^above doesn't throw if configure is called appropriately
  49. it('calls appropriate SupersetClient methods when configured', async () => {
  50. expect.assertions(16);
  51. const mockGetUrl = '/mock/get/url';
  52. const mockPostUrl = '/mock/post/url';
  53. const mockRequestUrl = '/mock/request/url';
  54. const mockPutUrl = '/mock/put/url';
  55. const mockDeleteUrl = '/mock/delete/url';
  56. const mockGetPayload = { get: 'payload' };
  57. const mockPostPayload = { post: 'payload' };
  58. const mockDeletePayload = { delete: 'ok' };
  59. const mockPutPayload = { put: 'ok' };
  60. fetchMock.get(mockGetUrl, mockGetPayload);
  61. fetchMock.post(mockPostUrl, mockPostPayload);
  62. fetchMock.delete(mockDeleteUrl, mockDeletePayload);
  63. fetchMock.put(mockPutUrl, mockPutPayload);
  64. fetchMock.get(mockRequestUrl, mockGetPayload);
  65. const initSpy = jest.spyOn(SupersetClientClass.prototype, 'init');
  66. const getSpy = jest.spyOn(SupersetClientClass.prototype, 'get');
  67. const postSpy = jest.spyOn(SupersetClientClass.prototype, 'post');
  68. const putSpy = jest.spyOn(SupersetClientClass.prototype, 'put');
  69. const deleteSpy = jest.spyOn(SupersetClientClass.prototype, 'delete');
  70. const authenticatedSpy = jest.spyOn(
  71. SupersetClientClass.prototype,
  72. 'isAuthenticated',
  73. );
  74. const csrfSpy = jest.spyOn(SupersetClientClass.prototype, 'fetchCSRFToken');
  75. const requestSpy = jest.spyOn(SupersetClientClass.prototype, 'request');
  76. const getGuestTokenSpy = jest.spyOn(
  77. SupersetClientClass.prototype,
  78. 'getGuestToken',
  79. );
  80. SupersetClient.configure({});
  81. await SupersetClient.init();
  82. expect(initSpy).toHaveBeenCalledTimes(1);
  83. expect(authenticatedSpy).toHaveBeenCalledTimes(2);
  84. expect(csrfSpy).toHaveBeenCalledTimes(1);
  85. await SupersetClient.get({ url: mockGetUrl });
  86. await SupersetClient.post({ url: mockPostUrl });
  87. await SupersetClient.delete({ url: mockDeleteUrl });
  88. await SupersetClient.put({ url: mockPutUrl });
  89. await SupersetClient.request({ url: mockRequestUrl });
  90. // Make sure network calls have Accept: 'application/json' in headers
  91. const networkCalls = [
  92. mockGetUrl,
  93. mockPostUrl,
  94. mockRequestUrl,
  95. mockPutUrl,
  96. mockDeleteUrl,
  97. ];
  98. networkCalls.map((url: string) =>
  99. expect(fetchMock.calls(url)[0][1]?.headers).toStrictEqual({
  100. Accept: 'application/json',
  101. 'X-CSRFToken': '1234',
  102. }),
  103. );
  104. SupersetClient.isAuthenticated();
  105. await SupersetClient.reAuthenticate();
  106. SupersetClient.getGuestToken();
  107. expect(getGuestTokenSpy).toHaveBeenCalledTimes(1);
  108. expect(initSpy).toHaveBeenCalledTimes(2);
  109. expect(deleteSpy).toHaveBeenCalledTimes(1);
  110. expect(putSpy).toHaveBeenCalledTimes(1);
  111. expect(getSpy).toHaveBeenCalledTimes(1);
  112. expect(postSpy).toHaveBeenCalledTimes(1);
  113. expect(requestSpy).toHaveBeenCalledTimes(5); // request rewires to get
  114. expect(csrfSpy).toHaveBeenCalledTimes(2); // from init() + reAuthenticate()
  115. initSpy.mockRestore();
  116. getSpy.mockRestore();
  117. putSpy.mockRestore();
  118. deleteSpy.mockRestore();
  119. requestSpy.mockRestore();
  120. postSpy.mockRestore();
  121. authenticatedSpy.mockRestore();
  122. csrfSpy.mockRestore();
  123. fetchMock.reset();
  124. });
  125. });