You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by el...@apache.org on 2021/11/23 00:13:08 UTC

[superset] 04/09: fix: accept headers on import (#17080)

This is an automated email from the ASF dual-hosted git repository.

elizabeth pushed a commit to branch 1.4
in repository https://gitbox.apache.org/repos/asf/superset.git

commit cca78f1b6e69c778b91ce1f3bb0aeb3d1c8ad26d
Author: Beto Dealmeida <ro...@dealmeida.net>
AuthorDate: Wed Oct 13 10:06:00 2021 -0700

    fix: accept headers on import (#17080)
    
    * fix: accept headers on import
    
    * Add unit test
    
    (cherry picked from commit 40e9add641483a1c3a8bc87efa0d8e525d4cfd0d)
---
 .../components/ImportModal/ImportModal.test.tsx    | 33 ++++++++++++++++++++++
 superset-frontend/src/views/CRUD/hooks.ts          |  1 +
 2 files changed, 34 insertions(+)

diff --git a/superset-frontend/src/components/ImportModal/ImportModal.test.tsx b/superset-frontend/src/components/ImportModal/ImportModal.test.tsx
index d4e3723..7ca93e1 100644
--- a/superset-frontend/src/components/ImportModal/ImportModal.test.tsx
+++ b/superset-frontend/src/components/ImportModal/ImportModal.test.tsx
@@ -22,6 +22,8 @@ import thunk from 'redux-thunk';
 import configureStore from 'redux-mock-store';
 import { styledMount as mount } from 'spec/helpers/theming';
 import { ReactWrapper } from 'enzyme';
+import fetchMock from 'fetch-mock';
+import waitForComponentToPaint from 'spec/helpers/waitForComponentToPaint';
 import { Upload } from 'src/common/components';
 import Button from 'src/components/Button';
 import { ImportResourceName } from 'src/views/CRUD/types';
@@ -31,6 +33,10 @@ import Modal from 'src/components/Modal';
 const mockStore = configureStore([thunk]);
 const store = mockStore({});
 
+const DATABASE_IMPORT_URL = 'glob:*/api/v1/database/import/';
+fetchMock.config.overwriteRoutes = true;
+fetchMock.post(DATABASE_IMPORT_URL, { result: 'OK' });
+
 const requiredProps = {
   resourceName: 'database' as ImportResourceName,
   resourceLabel: 'database',
@@ -101,6 +107,33 @@ describe('ImportModelsModal', () => {
     expect(wrapper.find(Button).at(2).prop('disabled')).toBe(false);
   });
 
+  it('should POST with request header `Accept: application/json`', async () => {
+    const file = new File([new ArrayBuffer(1)], 'model_export.zip');
+    act(() => {
+      const handler = wrapper.find(Upload).prop('onChange');
+      if (handler) {
+        handler({
+          fileList: [],
+          file: {
+            name: 'model_export.zip',
+            originFileObj: file,
+            uid: '-1',
+            size: 0,
+            type: 'zip',
+          },
+        });
+      }
+    });
+    wrapper.update();
+
+    wrapper.find(Button).at(2).simulate('click');
+    await waitForComponentToPaint(wrapper);
+    expect(fetchMock.calls(DATABASE_IMPORT_URL)[0][1]?.headers).toStrictEqual({
+      Accept: 'application/json',
+      'X-CSRFToken': '1234',
+    });
+  });
+
   it('should render password fields when needed for import', () => {
     const wrapperWithPasswords = mount(
       <ImportModelsModal
diff --git a/superset-frontend/src/views/CRUD/hooks.ts b/superset-frontend/src/views/CRUD/hooks.ts
index 54eccad..7a546ba 100644
--- a/superset-frontend/src/views/CRUD/hooks.ts
+++ b/superset-frontend/src/views/CRUD/hooks.ts
@@ -428,6 +428,7 @@ export function useImportResource(
       return SupersetClient.post({
         endpoint: `/api/v1/${resourceName}/import/`,
         body: formData,
+        headers: { Accept: 'application/json' },
       })
         .then(() => true)
         .catch(response =>