Skip to content

Commit

Permalink
refactor(SupersetClient): use new /api/v1/security/csrf_token/ endpoi…
Browse files Browse the repository at this point in the history
…nt (apache#980)
  • Loading branch information
nytai authored and zhaoyongjie committed Nov 24, 2021
1 parent 4615dbc commit 2a7e865
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ export default class SupersetClientClass {
method: 'GET',
mode: this.mode,
timeout: this.timeout,
url: this.getUrl({ endpoint: 'superset/csrf_token/' }),
url: this.getUrl({ endpoint: 'api/v1/security/csrf_token/' }),
parseMethod: 'json',
}).then(({ json }) => {
if (typeof json === 'object') {
this.csrfToken = json.csrf_token as string;
this.csrfToken = json.result as string;
if (typeof this.csrfToken === 'string') {
this.headers = { ...this.headers, 'X-CSRFToken': this.csrfToken };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('ChartClient', () => {
let chartClient: ChartClient;

beforeAll(() => {
fetchMock.get(LOGIN_GLOB, { csrf_token: '1234' });
fetchMock.get(LOGIN_GLOB, { result: '1234' });
SupersetClient.reset();
SupersetClient.configure().init();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const LOGIN_GLOB = 'glob:*superset/csrf_token/*'; // eslint-disable-line import/prefer-default-export
export const LOGIN_GLOB = 'glob:*api/v1/security/csrf_token/*'; // eslint-disable-line import/prefer-default-export
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { LOGIN_GLOB } from './fixtures/constants';

describe('SupersetClient', () => {
beforeAll(() => {
fetchMock.get(LOGIN_GLOB, { csrf_token: '' });
fetchMock.get(LOGIN_GLOB, { result: '' });
});

afterAll(fetchMock.restore);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { LOGIN_GLOB } from './fixtures/constants';

describe('SupersetClientClass', () => {
beforeAll(() => {
fetchMock.get(LOGIN_GLOB, { csrf_token: '' });
fetchMock.get(LOGIN_GLOB, { result: '' });
});

afterAll(fetchMock.restore);
Expand Down Expand Up @@ -68,22 +68,22 @@ describe('SupersetClientClass', () => {
afterEach(() => {
fetchMock.reset();
// reset
fetchMock.get(LOGIN_GLOB, { csrf_token: 1234 }, { overwriteRoutes: true });
fetchMock.get(LOGIN_GLOB, { result: 1234 }, { overwriteRoutes: true });
});

it('calls superset/csrf_token/ when init() is called if no CSRF token is passed', async () => {
it('calls api/v1/security/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 () => {
it('does NOT call api/v1/security/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 () => {
it('calls api/v1/security/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 });
Expand All @@ -97,7 +97,7 @@ describe('SupersetClientClass', () => {
expect(client.csrfToken).not.toBe(initialToken);
});

it('throws if superset/csrf_token/ returns an error', async () => {
it('throws if api/v1/security/csrf_token/ returns an error', async () => {
expect.assertions(1);
const rejectError = { status: 403 };
fetchMock.get(LOGIN_GLOB, () => Promise.reject(rejectError), {
Expand All @@ -116,7 +116,7 @@ describe('SupersetClientClass', () => {

const invalidCsrfTokenError = { error: 'Failed to fetch CSRF token' };

it('throws if superset/csrf_token/ does not return a token', async () => {
it('throws if api/v1/security/csrf_token/ does not return a token', async () => {
expect.assertions(1);
fetchMock.get(LOGIN_GLOB, {}, { overwriteRoutes: true });

Expand Down Expand Up @@ -226,7 +226,7 @@ describe('SupersetClientClass', () => {
// reset
fetchMock.get(
LOGIN_GLOB,
{ csrf_token: 1234 },
{ result: 1234 },
{
overwriteRoutes: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { LOGIN_GLOB } from '../fixtures/constants';

describe('callApi()', () => {
beforeAll(() => {
fetchMock.get(LOGIN_GLOB, { csrf_token: '1234' });
fetchMock.get(LOGIN_GLOB, { result: '1234' });
});

afterAll(fetchMock.restore);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { LOGIN_GLOB } from '../fixtures/constants';

describe('callApiAndParseWithTimeout()', () => {
beforeAll(() => {
fetchMock.get(LOGIN_GLOB, { csrf_token: '1234' });
fetchMock.get(LOGIN_GLOB, { result: '1234' });
});

afterAll(fetchMock.restore);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { LOGIN_GLOB } from '../fixtures/constants';

describe('parseResponse()', () => {
beforeAll(() => {
fetchMock.get(LOGIN_GLOB, { csrf_token: '1234' });
fetchMock.get(LOGIN_GLOB, { result: '1234' });
});

afterAll(fetchMock.restore);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
* specific language governing permissions and limitations
* under the License.
*/
export const LOGIN_GLOB = 'glob:*superset/csrf_token/*'; // eslint-disable-line import/prefer-default-export
export const LOGIN_GLOB = 'glob:*api/v1/security/csrf_token/*'; // eslint-disable-line import/prefer-default-export
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import fetchMock from 'fetch-mock';
import { SupersetClient } from '@superset-ui/core/src/connection';

const LOGIN_GLOB = 'glob:*superset/csrf_token/*';
const LOGIN_GLOB = 'glob:*api/v1/security/csrf_token/*';

export default function setupClientForTest() {
fetchMock.get(LOGIN_GLOB, { csrf_token: '1234' });
fetchMock.get(LOGIN_GLOB, { result: '1234' });
SupersetClient.reset();
SupersetClient.configure().init();
}

0 comments on commit 2a7e865

Please sign in to comment.