Skip to content

Commit

Permalink
fixup! (HP-2023) Action for detecting gdpr callback url
Browse files Browse the repository at this point in the history
  • Loading branch information
NikoHelle committed Oct 5, 2023
1 parent c12c2b0 commit a757722
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 43 deletions.
135 changes: 94 additions & 41 deletions src/gdprApi/actions/__tests__/authCodeCallbackUrlDetector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
keycloakAuthCodeCallbackUrlAction,
isQueueWaitingForAuthCodeCallback,
resumeQueueFromNextCallbackDetector,
getNextAuthCodeCallbackDetector,
} from '../authCodeCallbackUrlDetector';
import {
keycloakAuthCodeRedirectionAction,
Expand Down Expand Up @@ -72,51 +73,103 @@ describe('authCodeRedirectionAction.ts', () => {
jest.resetAllMocks();
});

it('Resolves to true, if authcode action is needed and current path is for the gdpr callback', async () => {
mockedWindowControls.setPath(config.gdprCallbackPath);
const { runner, getTunnistamoAction, getKeycloadAction } = initTests();
const tunnistamoAction = getTunnistamoAction();
const keycloadAction = getKeycloadAction();
const [, resultForTunnistamo] = await to(
tunnistamoAction.executor(tunnistamoAction, runner)
);
expect(resultForTunnistamo).toBeTruthy();
describe('tunnistamoAuthCodeCallbackUrlAction and keycloakAuthCodeCallbackUrlAction', () => {
it('Resolves to true, if authcode action is needed and current path is for the gdpr callback', async () => {
mockedWindowControls.setPath(config.gdprCallbackPath);
const { runner, getTunnistamoAction, getKeycloadAction } = initTests();
const tunnistamoAction = getTunnistamoAction();
const keycloadAction = getKeycloadAction();
const [, resultForTunnistamo] = await to(
tunnistamoAction.executor(tunnistamoAction, runner)
);
expect(resultForTunnistamo).toBeTruthy();

const [, resultForKeycloak] = await to(
keycloadAction.executor(keycloadAction, runner)
);
expect(resultForKeycloak).toBeTruthy();
const [, resultForKeycloak] = await to(
keycloadAction.executor(keycloadAction, runner)
);
expect(resultForKeycloak).toBeTruthy();
});
it('Resolves to false, if authcode action is not needed', async () => {
mockedWindowControls.setPath(config.gdprCallbackPath);
const { runner, getTunnistamoAction, getKeycloadAction } = initTests({
noKeycloakScopes: true,
noTunnistamoScopes: true,
});
const tunnistamoAction = getTunnistamoAction();
const keycloadAction = getKeycloadAction();
const [, resultForTunnistamo] = await to(
tunnistamoAction.executor(tunnistamoAction, runner)
);
expect(resultForTunnistamo).toBeFalsy();
const [, resultForKeycloak] = await to(
keycloadAction.executor(keycloadAction, runner)
);
expect(resultForKeycloak).toBeFalsy();
});
it('When executed and current path is not for the gdpr callback, rejects after a 30sec timeout', async () => {
mockedWindowControls.setPath(config.errorPagePath);
const { runner, getTunnistamoAction, getKeycloadAction } = initTests({
noKeycloakScopes: true,
noTunnistamoScopes: true,
});
const tunnistamoAction = getTunnistamoAction();
const keycloadAction = getKeycloadAction();
const [error1] = await to(
tunnistamoAction.executor(tunnistamoAction, runner)
);
expect(error1).toBeDefined();
const [error2] = await to(
keycloadAction.executor(keycloadAction, runner)
);
expect(error2).toBeDefined();
});
});
it('Resolves to false, if authcode action is not needed', async () => {
mockedWindowControls.setPath(config.gdprCallbackPath);
const { runner, getTunnistamoAction, getKeycloadAction } = initTests({
noKeycloakScopes: true,
noTunnistamoScopes: true,
describe(`getNextAuthCodeCallbackDetector() and isQueueWaitingForAuthCodeCallback() are helpers for determining
what should be done next - usually after a redirection to auth code provider has returned back.`, () => {
it(`getNextAuthCodeCallbackDetector() returns action.type if next action is a detector or undefinded.
isQueueWaitingForAuthCodeCallback() converts same value to a boolean.`, async () => {
const { runner, getTunnistamoAction, getKeycloadAction } = initTests();
expect(getNextAuthCodeCallbackDetector(runner)).toBeUndefined();
expect(isQueueWaitingForAuthCodeCallback(runner)).toBeFalsy();
runner.updateActionAndQueue(tunnistamoAuthCodeRedirectionAction.type, {
complete: true,
});
expect(getNextAuthCodeCallbackDetector(runner)).toBe(
getTunnistamoAction().type
);
expect(isQueueWaitingForAuthCodeCallback(runner)).toBeTruthy();
runner.updateActionAndQueue(getTunnistamoAction().type, {
complete: true,
});
expect(isQueueWaitingForAuthCodeCallback(runner)).toBeFalsy();
runner.updateActionAndQueue(keycloakAuthCodeRedirectionAction.type, {
complete: true,
});
expect(isQueueWaitingForAuthCodeCallback(runner)).toBeTruthy();
expect(getNextAuthCodeCallbackDetector(runner)).toBe(
getKeycloadAction().type
);
runner.updateActionAndQueue(getKeycloadAction().type, {
complete: true,
});
expect(getNextAuthCodeCallbackDetector(runner)).toBeUndefined();
expect(isQueueWaitingForAuthCodeCallback(runner)).toBeFalsy();
});
const tunnistamoAction = getTunnistamoAction();
const keycloadAction = getKeycloadAction();
const [, resultForTunnistamo] = await to(
tunnistamoAction.executor(tunnistamoAction, runner)
);
expect(resultForTunnistamo).toBeFalsy();
const [, resultForKeycloak] = await to(
keycloadAction.executor(keycloadAction, runner)
);
expect(resultForKeycloak).toBeFalsy();
});
it('When executed and current path is not for the gdpr callback, rejects after a 30sec timeout', async () => {
mockedWindowControls.setPath(config.errorPagePath);
const { runner, getTunnistamoAction, getKeycloadAction } = initTests({
noKeycloakScopes: true,
noTunnistamoScopes: true,
describe(`resumeQueueFromNextCallbackDetector() is a helper to resume queue from a detector.`, () => {
it(`it returns the resumed action.type or undefined if no action was resumed.`, async () => {
const { runner, getTunnistamoAction } = initTests();
expect(resumeQueueFromNextCallbackDetector(runner)).toBeUndefined();
runner.updateActionAndQueue(tunnistamoAuthCodeRedirectionAction.type, {
complete: true,
});
expect(resumeQueueFromNextCallbackDetector(runner)).toBe(
getTunnistamoAction().type
);
await waitFor(() => {
expect(runner.isFinished()).toBeTruthy();
});
expect(resumeQueueFromNextCallbackDetector(runner)).toBeUndefined();
});
const tunnistamoAction = getTunnistamoAction();
const keycloadAction = getKeycloadAction();
const [error1] = await to(
tunnistamoAction.executor(tunnistamoAction, runner)
);
expect(error1).toBeDefined();
const [error2] = await to(keycloadAction.executor(keycloadAction, runner));
expect(error2).toBeDefined();
});
});
7 changes: 5 additions & 2 deletions src/gdprApi/actions/authCodeCallbackUrlDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const getNextAuthCodeCallbackDetector = (
) => {
const nextAction = controller.getNext();
if (!nextAction) {
return false;
return undefined;
}
if (nextAction.type === tunnistamoAuthCodeCallbackUrlDetectorType) {
return tunnistamoAuthCodeCallbackUrlDetectorType;
Expand All @@ -55,7 +55,10 @@ export const resumeQueueFromNextCallbackDetector = (
if (!actionType) {
return undefined;
}
return runner.resume(actionType);
if (runner.resume(actionType)) {
return actionType;
}
return undefined;
};

const authCodeCallbackUrlDetectorExecutor: ActionExecutor = async (
Expand Down

0 comments on commit a757722

Please sign in to comment.