Skip to content

Commit

Permalink
[cascading] from release/10.4.0-rc to main (#1893)
Browse files Browse the repository at this point in the history
<!--
{"currentBranch":"release/10.4.0-rc","targetBranch":"main","bypassReviewers":true,"isConflicting":false}
-->

## Cascading from release/10.4.0-rc to main

The configuration requests the cascading to bypass reviewer in case of
CI success.
To not bypass the reviewing process, please check the following
checkbox:

- [ ] <!-- !cancel bypass! --> 🚫 stop reviewing process
bypass for this Pull Request

---

<small>This Pull Request has been generated with ❤️ by the
[Otter](https://github.com/AmadeusITGroup/otter) cascading tool.</small>
  • Loading branch information
mrednic-1A authored Jun 14, 2024
2 parents cf6034d + eb26ddf commit 58ab357
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 8 deletions.
13 changes: 10 additions & 3 deletions packages/@ama-sdk/core/src/plugins/timeout/timeout.fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type TimeoutStatus = 'timeoutStopped' | 'timeoutStarted';
* @param message
*/
function isImpervaCaptchaMessage(message: any): message is ImpervaCaptchaMessageData {
return Object.prototype.hasOwnProperty.call(message, 'impervaChallenge') &&
return !!message && Object.prototype.hasOwnProperty.call(message, 'impervaChallenge') &&
Object.prototype.hasOwnProperty.call(message.impervaChallenge, 'status') &&
Object.prototype.hasOwnProperty.call(message.impervaChallenge, 'type') && message.impervaChallenge.type === 'captcha';
}
Expand Down Expand Up @@ -55,8 +55,15 @@ export const impervaCaptchaEventHandlerFactory: TimeoutPauseEventHandlerFactory<
if (originHostname !== location.hostname && (config?.whiteListedHostNames || []).indexOf(originHostname) === -1) {
return;
}
const message = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
if (message && isImpervaCaptchaMessage(message)) {
let message = event.data;
if (typeof event.data === 'string') {
try {
message = JSON.parse(event.data);
} catch {
// This might not be an imperva message
}
}
if (typeof message === 'object' && isImpervaCaptchaMessage(message)) {
timeoutPauseCallback(message.impervaChallenge.status === 'started' ? 'timeoutStopped' : 'timeoutStarted');
}
});
Expand Down
91 changes: 86 additions & 5 deletions packages/@ama-sdk/core/src/plugins/timeout/timeout.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {EmptyResponseError, ResponseTimeoutError} from '../../fwk/errors';
import {TimeoutFetch, TimeoutStatus} from './timeout.fetch';
import {
impervaCaptchaEventHandlerFactory,
TimeoutFetch,
TimeoutStatus
} from './timeout.fetch';

describe('Timeout Fetch Plugin', () => {

Expand Down Expand Up @@ -44,10 +48,12 @@ describe('Timeout Fetch Plugin', () => {

it('should not reject if the timeout has been paused and reject if restarted', async () => {
const timeoutPauseEvent = {
emitEvent: (_status: TimeoutStatus) => {},
emitEvent: (_status: TimeoutStatus) => {
},
handler: (timeoutPauseCallback: (status: TimeoutStatus) => void) => {
timeoutPauseEvent.emitEvent = timeoutPauseCallback;
return () => {};
return () => {
};
}
};
const plugin = new TimeoutFetch(100, timeoutPauseEvent.handler);
Expand All @@ -65,10 +71,12 @@ describe('Timeout Fetch Plugin', () => {

it('should take into account pause events triggered before the call', async () => {
const timeoutPauseEvent = {
emitEvent: (_status: TimeoutStatus) => {},
emitEvent: (_status: TimeoutStatus) => {
},
handler: (timeoutPauseCallback: (status: TimeoutStatus) => void) => {
timeoutPauseEvent.emitEvent = timeoutPauseCallback;
return () => {};
return () => {
};
}
};
const plugin = new TimeoutFetch(250, timeoutPauseEvent.handler);
Expand All @@ -82,3 +90,76 @@ describe('Timeout Fetch Plugin', () => {
expect(await promise).toEqual({test: true} as any);
});
});

describe('impervaCaptchaEventHandlerFactory', () => {
let postMessageTemp: (msg: any, origin?: string) => any;
beforeAll(() => {
global.location ||= {hostname: 'test'} as any;
global.addEventListener ||= jest.fn().mockImplementation((event, handler) => {
if (event === 'message') {
postMessageTemp = (msg, origin?) => {
const eventObject = {
origin: origin || 'https://test',
data: msg
} as any;
if (typeof handler === 'object') {
handler.handleEvent(eventObject);
} else {
handler(eventObject);
}
};
}
}) as any;
}
);

afterAll(() => {
jest.clearAllMocks();
});

it('should not throw on unexpected messages', () => {
const callback = jest.fn();
impervaCaptchaEventHandlerFactory({whiteListedHostNames: []})(callback, this);
postMessageTemp('pouet');
expect(callback).not.toHaveBeenCalled();
postMessageTemp(JSON.stringify({impervaChallenge: {type: 'incorrectType'}}));
expect(callback).not.toHaveBeenCalled();
postMessageTemp(JSON.stringify({impervaChallenge: {incorrectFormat: true}}));
expect(callback).not.toHaveBeenCalled();
});

it('should not throw on null messages', () => {
const callback = jest.fn();
impervaCaptchaEventHandlerFactory({whiteListedHostNames: []})(callback, this);
postMessageTemp(null);
expect(callback).not.toHaveBeenCalled();
});

it('should trigger a timeoutStopped if the captcha challenge has been started', () => {
const callback = jest.fn();
impervaCaptchaEventHandlerFactory({whiteListedHostNames: []})(callback, this);
postMessageTemp(JSON.stringify({impervaChallenge: {status: 'started', type: 'captcha'}}));
expect(callback).toHaveBeenCalledWith('timeoutStopped');
});

it('should trigger a timeoutStarted if the captcha challenge has been finished', () => {
const callback = jest.fn();
impervaCaptchaEventHandlerFactory({whiteListedHostNames: []})(callback, this);
postMessageTemp({impervaChallenge: {status: 'ended', type: 'captcha'}});
expect(callback).toHaveBeenCalledWith('timeoutStarted');
});

it('should trigger a timeoutStarted if the captcha challenge has been finished on a whitelisted domain', () => {
const callback = jest.fn();
impervaCaptchaEventHandlerFactory({whiteListedHostNames: ['valid.domain']})(callback, this);
postMessageTemp(JSON.stringify({impervaChallenge: {status: 'ended', type: 'captcha'}}), 'http://valid.domain');
expect(callback).toHaveBeenCalledWith('timeoutStarted');
});

it('should ignore postMessage from non whitelisted domain', () => {
const callback = jest.fn();
impervaCaptchaEventHandlerFactory({whiteListedHostNames: []})(callback, this);
postMessageTemp(JSON.stringify({impervaChallenge: {status: 'ended', type: 'captcha'}}), 'http://invalid.domain');
expect(callback).not.toHaveBeenCalled();
});
});

0 comments on commit 58ab357

Please sign in to comment.