mirror of https://github.com/apache/superset.git
fix(embedded): Ensure guest token is passed to log endpoint (#20647)
* Pass guest token to sendBeacon * Add tests
This commit is contained in:
parent
e1a918f141
commit
dfab521f50
|
@ -44,6 +44,7 @@ const SupersetClient: SupersetClientInterface = {
|
||||||
get: request => getInstance().get(request),
|
get: request => getInstance().get(request),
|
||||||
init: force => getInstance().init(force),
|
init: force => getInstance().init(force),
|
||||||
isAuthenticated: () => getInstance().isAuthenticated(),
|
isAuthenticated: () => getInstance().isAuthenticated(),
|
||||||
|
getGuestToken: () => getInstance().getGuestToken(),
|
||||||
post: request => getInstance().post(request),
|
post: request => getInstance().post(request),
|
||||||
postForm: (...args) => getInstance().postForm(...args),
|
postForm: (...args) => getInstance().postForm(...args),
|
||||||
put: request => getInstance().put(request),
|
put: request => getInstance().put(request),
|
||||||
|
|
|
@ -158,6 +158,10 @@ export default class SupersetClientClass {
|
||||||
return this.csrfToken !== null && this.csrfToken !== undefined;
|
return this.csrfToken !== null && this.csrfToken !== undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getGuestToken() {
|
||||||
|
return this.guestToken;
|
||||||
|
}
|
||||||
|
|
||||||
async get<T extends ParseMethod = 'json'>(
|
async get<T extends ParseMethod = 'json'>(
|
||||||
requestConfig: RequestConfig & { parseMethod?: T },
|
requestConfig: RequestConfig & { parseMethod?: T },
|
||||||
) {
|
) {
|
||||||
|
|
|
@ -152,6 +152,7 @@ export interface SupersetClientInterface
|
||||||
| 'init'
|
| 'init'
|
||||||
| 'isAuthenticated'
|
| 'isAuthenticated'
|
||||||
| 'reAuthenticate'
|
| 'reAuthenticate'
|
||||||
|
| 'getGuestToken'
|
||||||
> {
|
> {
|
||||||
configure: (config?: ClientConfig) => SupersetClientInterface;
|
configure: (config?: ClientConfig) => SupersetClientInterface;
|
||||||
reset: () => void;
|
reset: () => void;
|
||||||
|
|
|
@ -109,4 +109,39 @@ describe('logger middleware', () => {
|
||||||
SupersetClient.post.getCall(0).args[0].postPayload.events,
|
SupersetClient.post.getCall(0).args[0].postPayload.events,
|
||||||
).toHaveLength(3);
|
).toHaveLength(3);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should use navigator.sendBeacon if it exists', () => {
|
||||||
|
const clock = sinon.useFakeTimers();
|
||||||
|
const beaconMock = jest.fn();
|
||||||
|
Object.defineProperty(navigator, 'sendBeacon', {
|
||||||
|
writable: true,
|
||||||
|
value: beaconMock,
|
||||||
|
});
|
||||||
|
|
||||||
|
logger(mockStore)(next)(action);
|
||||||
|
expect(beaconMock.mock.calls.length).toBe(0);
|
||||||
|
clock.tick(2000);
|
||||||
|
|
||||||
|
expect(beaconMock.mock.calls.length).toBe(1);
|
||||||
|
const endpoint = beaconMock.mock.calls[0][0];
|
||||||
|
expect(endpoint).toMatch('/superset/log/');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should pass a guest token to sendBeacon if present', () => {
|
||||||
|
const clock = sinon.useFakeTimers();
|
||||||
|
const beaconMock = jest.fn();
|
||||||
|
Object.defineProperty(navigator, 'sendBeacon', {
|
||||||
|
writable: true,
|
||||||
|
value: beaconMock,
|
||||||
|
});
|
||||||
|
SupersetClient.configure({ guestToken: 'token' });
|
||||||
|
|
||||||
|
logger(mockStore)(next)(action);
|
||||||
|
expect(beaconMock.mock.calls.length).toBe(0);
|
||||||
|
clock.tick(2000);
|
||||||
|
expect(beaconMock.mock.calls.length).toBe(1);
|
||||||
|
|
||||||
|
const formData = beaconMock.mock.calls[0][1];
|
||||||
|
expect(formData.getAll('guest_token')[0]).toMatch('token');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -44,6 +44,10 @@ const sendBeacon = events => {
|
||||||
if (navigator.sendBeacon) {
|
if (navigator.sendBeacon) {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('events', safeStringify(events));
|
formData.append('events', safeStringify(events));
|
||||||
|
if (SupersetClient.getGuestToken()) {
|
||||||
|
// if we have a guest token, we need to send it for auth via the form
|
||||||
|
formData.append('guest_token', SupersetClient.getGuestToken());
|
||||||
|
}
|
||||||
navigator.sendBeacon(endpoint, formData);
|
navigator.sendBeacon(endpoint, formData);
|
||||||
} else {
|
} else {
|
||||||
SupersetClient.post({
|
SupersetClient.post({
|
||||||
|
|
Loading…
Reference in New Issue