Skip to content

Commit

Permalink
1st param of getRemediator - idxResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
denysoblohin-okta committed Jun 30, 2023
1 parent f2f7675 commit c5af40c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 56 deletions.
4 changes: 2 additions & 2 deletions lib/idx/remediate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export async function remediate(
return { idxResponse };
}

const remediator = getRemediator(neededToProceed, values, options, context);
const remediator = getRemediator(idxResponse, values, options);

// Try actions in idxResponse first
const actionFromValues = getActionFromValues(values, idxResponse);
Expand Down Expand Up @@ -175,7 +175,7 @@ export async function remediate(
// return nextStep directly
if (options.useGenericRemediator && !idxResponse.interactionCode && !isTerminalResponse(idxResponse)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const gr = getRemediator(idxResponse.neededToProceed, values, options, idxResponse.context)!;
const gr = getRemediator(idxResponse, values, options)!;
const nextStep = getNextStep(authClient, gr, idxResponse);
return {
idxResponse,
Expand Down
6 changes: 3 additions & 3 deletions lib/idx/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,14 @@ function getRemediatorClass(remediation: IdxRemediation, options: RemediateOptio
// Return first match idxRemediation in allowed remediators
// eslint-disable-next-line complexity
export function getRemediator(
idxRemediations: IdxRemediation[],
idxResponse: IdxResponse,
values: RemediationValues,
options: RemediateOptions,
context?: IdxContext,
): Remediator | undefined {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const remediators = options.remediators!;
const useGenericRemediator = options.useGenericRemediator;
const {neededToProceed: idxRemediations, context} = idxResponse;

let remediator: Remediator;
// remediation name specified by caller - fast-track remediator lookup
Expand Down Expand Up @@ -285,7 +285,7 @@ export function handleFailedResponse(
if (terminal) {
return { idxResponse, terminal, messages };
} else {
const remediator = getRemediator(idxResponse.neededToProceed, {}, options, idxResponse.context);
const remediator = getRemediator(idxResponse, {}, options);
const nextStep = remediator && getNextStep(authClient, remediator, idxResponse);
return {
idxResponse,
Expand Down
26 changes: 13 additions & 13 deletions test/spec/idx/remediate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ describe('idx/remediate', () => {
nextStep: {}
});
expect(util.getRemediator).toHaveBeenCalledTimes(2);
expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse.neededToProceed, { resend: true }, {});
expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromAction.neededToProceed, {}, { actions: [] });
expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse, { resend: true }, {});
expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromAction, {}, { actions: [] });
});

it('will handle exceptions', async () => {
Expand Down Expand Up @@ -145,8 +145,8 @@ describe('idx/remediate', () => {
nextStep: {}
});
expect(util.getRemediator).toHaveBeenCalledTimes(2);
expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse.neededToProceed, {}, { actions: ['some-action'] });
expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromAction.neededToProceed, {}, { actions: [] });
expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse, {}, { actions: ['some-action'] });
expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromAction, {}, { actions: [] });
});
it('will handle exceptions', async () => {
const { authClient } = testContext;
Expand Down Expand Up @@ -195,8 +195,8 @@ describe('idx/remediate', () => {
nextStep: {}
});
expect(util.getRemediator).toHaveBeenCalledTimes(2);
expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse.neededToProceed, {}, { actions: ['some-remediation'] });
expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromRemediation.neededToProceed, {}, { actions: [] });
expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse, {}, { actions: ['some-remediation'] });
expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromRemediation, {}, { actions: [] });
});
it('will handle exceptions', async () => {
let { authClient, idxResponse } = testContext;
Expand Down Expand Up @@ -280,8 +280,8 @@ describe('idx/remediate', () => {
nextStep: {}
});
expect(util.getRemediator).toHaveBeenCalledTimes(2);
expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse.neededToProceed, {}, { actions: [action] });
expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromAction.neededToProceed, {}, { actions: [] });
expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse, {}, { actions: [action] });
expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromAction, {}, { actions: [] });
});
it('will handle exceptions', async () => {
let { authClient, idxResponse } = testContext;
Expand Down Expand Up @@ -339,8 +339,8 @@ describe('idx/remediate', () => {
nextStep: {}
});
expect(util.getRemediator).toHaveBeenCalledTimes(2);
expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse.neededToProceed, {}, { actions: [action] });
expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromRemediation.neededToProceed, {}, { actions: [] });
expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse, {}, { actions: [action] });
expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromRemediation, {}, { actions: [] });
});
it('will handle exceptions', async () => {
let { authClient, idxResponse } = testContext;
Expand Down Expand Up @@ -432,7 +432,7 @@ describe('idx/remediate', () => {
},
});
expect(util.getRemediator).toHaveBeenCalledTimes(1);
expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse.neededToProceed, {}, { step: 'some-remediation' });
expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse, {}, { step: 'some-remediation' });
expect(idxResponse.proceed).toHaveBeenCalledWith('some-remediation', {});
});
it('will handle exceptions', async () => {
Expand Down Expand Up @@ -530,8 +530,8 @@ describe('idx/remediate', () => {
});
expect(idxResponse.proceed).toHaveBeenCalledWith(name, data);
expect(util.getRemediator).toHaveBeenCalledTimes(2);
expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse.neededToProceed, {}, {});
expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromProceed.neededToProceed, valuesAfterProceed, {});
expect(util.getRemediator).toHaveBeenNthCalledWith(1, idxResponse, {}, {});
expect(util.getRemediator).toHaveBeenNthCalledWith(2, responseFromProceed, valuesAfterProceed, {});

});

Expand Down
89 changes: 51 additions & 38 deletions test/spec/idx/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,75 +218,83 @@ describe('idx/util', () => {

describe('A Remediator exists that matches one of the idx remediations', () => {
beforeEach(() => {
const idxRemediations = [{
const neededToProceed = [{
name: 'foo'
}, {
name: 'bar'
}];
const idxResponse = {
neededToProceed
} as IdxResponse;
testContext = {
...testContext,
idxRemediations
neededToProceed,
idxResponse
};
});

it('if first Remediator can remediate, returns the first Remediator instance', () => {
const { idxRemediations, values, options, FooRemediator } = testContext;
const { idxResponse, neededToProceed, values, options, FooRemediator } = testContext;
FooRemediator.prototype.canRemediate = jest.fn().mockReturnValue(true);
expect(getRemediator(idxRemediations, values, options)).toBeInstanceOf(FooRemediator);
expect(FooRemediator).toHaveBeenCalledWith(idxRemediations[0], values, options);
expect(getRemediator(idxResponse, values, options)).toBeInstanceOf(FooRemediator);
expect(FooRemediator).toHaveBeenCalledWith(neededToProceed[0], values, options);
});

it('if first matched Remediator cannot remediate, but 2nd Remediator can, returns the 2nd Remediator', () => {
const { idxRemediations, values, options, FooRemediator, BarRemediator } = testContext;
const { idxResponse, neededToProceed, values, options, FooRemediator, BarRemediator } = testContext;
FooRemediator.prototype.canRemediate = jest.fn().mockReturnValue(false);
BarRemediator.prototype.canRemediate = jest.fn().mockReturnValue(true);
expect(getRemediator(idxRemediations, values, options)).toBeInstanceOf(BarRemediator);
expect(BarRemediator).toHaveBeenCalledWith(idxRemediations[1], values, options);
expect(getRemediator(idxResponse, values, options)).toBeInstanceOf(BarRemediator);
expect(BarRemediator).toHaveBeenCalledWith(neededToProceed[1], values, options);
});
it('if no Remediator can remediate, returns the first matching Remediator instance', () => {
const { idxRemediations, values, options, FooRemediator, BarRemediator } = testContext;
const { idxResponse, neededToProceed, values, options, FooRemediator, BarRemediator } = testContext;
FooRemediator.prototype.canRemediate = jest.fn().mockReturnValue(false);
BarRemediator.prototype.canRemediate = jest.fn().mockReturnValue(false);
expect(getRemediator(idxRemediations, values, options)).toBeInstanceOf(FooRemediator);
expect(FooRemediator).toHaveBeenCalledWith(idxRemediations[0], values, options);
expect(getRemediator(idxResponse, values, options)).toBeInstanceOf(FooRemediator);
expect(FooRemediator).toHaveBeenCalledWith(neededToProceed[0], values, options);
});

describe('with options.step', () => {

it('returns a remediator instance if a Remediator exists that matches the idx remediation with the given step name', () => {
const { idxRemediations, values, options, BarRemediator } = testContext;
const { idxResponse, neededToProceed, values, options, BarRemediator } = testContext;
options.step = 'bar';
expect(getRemediator(idxRemediations, values, options)).toBeInstanceOf(BarRemediator);
expect(BarRemediator).toHaveBeenCalledWith(idxRemediations[1], values, options);
expect(getRemediator(idxResponse, values, options)).toBeInstanceOf(BarRemediator);
expect(BarRemediator).toHaveBeenCalledWith(neededToProceed[1], values, options);
});
it('returns undefined if no Remediator could be found matching the idx remediation with the given step name', () => {
const { idxRemediations, values, options } = testContext;
const { idxResponse, values, options } = testContext;
options.step = 'bar';
options.remediators = {
'other': jest.fn()
};
expect(getRemediator(idxRemediations, values, options)).toBe(undefined);
expect(getRemediator(idxResponse, values, options)).toBe(undefined);
});
it('returns undefined if no idx remediation is found matching the given step name', () => {
const { values, options } = testContext;
const { idxResponse, values, options } = testContext;
options.step = 'bar';
const idxRemediations = [{
const neededToProceed = [{
name: 'other'
}];
expect(getRemediator(idxRemediations, values, options)).toBe(undefined);
idxResponse.neededToProceed = neededToProceed;
expect(getRemediator(idxResponse, values, options)).toBe(undefined);
});
});
});

it('returns undefined if no Remediator exists that matches the idx remediations', () => {
const { values, options } = testContext;
const idxRemediations = [{
const neededToProceed = [{
name: 'unknown'
}];
const idxResponse = {
neededToProceed
} as IdxResponse;
options.remediators = {
other: jest.fn()
};
expect(getRemediator(idxRemediations, values, options)).toBe(undefined);
expect(getRemediator(idxResponse, values, options)).toBe(undefined);
});

describe('with options.useGenericRemediator', () => {
Expand All @@ -295,56 +303,61 @@ describe('idx/util', () => {
...testContext.options,
useGenericRemediator: true
};
const idxRemediations = [{
const neededToProceed = [{
name: 'foo'
}, {
name: 'bar'
}];
const idxResponse = {
neededToProceed
} as IdxResponse;
testContext = {
...testContext,
idxRemediations,
neededToProceed,
idxResponse,
options
};
});

describe('with options.step', () => {
it('returns GenericRemediator instance if one idx remediation can match the given step name', () => {
const { idxRemediations, values, options } = testContext;
const { idxResponse, neededToProceed, values, options } = testContext;
options.step = 'bar';
expect(getRemediator(idxRemediations, values, options)).toBeInstanceOf(GenericRemediator);
expect(GenericRemediator).toHaveBeenCalledWith(idxRemediations[1], values, options);
expect(getRemediator(idxResponse, values, options)).toBeInstanceOf(GenericRemediator);
expect(GenericRemediator).toHaveBeenCalledWith(neededToProceed[1], values, options);
});
it('returns undefined if no idx remediation is found matching the given step name', () => {
const { values, options } = testContext;
const { idxResponse, values, options } = testContext;
options.step = 'bar';
const idxRemediations = [{
const neededToProceed = [{
name: 'other'
}];
expect(getRemediator(idxRemediations, values, options)).toBe(undefined);
idxResponse.neededToProceed = neededToProceed;
expect(getRemediator(idxResponse, values, options)).toBe(undefined);
});
});

describe('without options.step', () => {
it('if first Remediator can remediate, returns the first Remediator instance', () => {
const { idxRemediations, values, options, FooRemediator } = testContext;
const { idxResponse, neededToProceed, values, options, FooRemediator } = testContext;
FooRemediator.prototype.canRemediate = jest.fn().mockReturnValue(true);
expect(getRemediator(idxRemediations, values, options)).toBeInstanceOf(GenericRemediator);
expect(GenericRemediator).toHaveBeenCalledWith(idxRemediations[0], values, options);
expect(getRemediator(idxResponse, values, options)).toBeInstanceOf(GenericRemediator);
expect(GenericRemediator).toHaveBeenCalledWith(neededToProceed[0], values, options);
});

it('if first matched Remediator cannot remediate, but 2nd Remediator can, returns the first Remediator instance', () => {
const { idxRemediations, values, options, FooRemediator, BarRemediator } = testContext;
const { idxResponse, neededToProceed, values, options, FooRemediator, BarRemediator } = testContext;
FooRemediator.prototype.canRemediate = jest.fn().mockReturnValue(false);
BarRemediator.prototype.canRemediate = jest.fn().mockReturnValue(true);
expect(getRemediator(idxRemediations, values, options)).toBeInstanceOf(GenericRemediator);
expect(GenericRemediator).toHaveBeenCalledWith(idxRemediations[0], values, options);
expect(getRemediator(idxResponse, values, options)).toBeInstanceOf(GenericRemediator);
expect(GenericRemediator).toHaveBeenCalledWith(neededToProceed[0], values, options);
});
it('if no Remediator can remediate, returns the first Remediator instance', () => {
const { idxRemediations, values, options, FooRemediator, BarRemediator } = testContext;
const { idxResponse, neededToProceed, values, options, FooRemediator, BarRemediator } = testContext;
FooRemediator.prototype.canRemediate = jest.fn().mockReturnValue(false);
BarRemediator.prototype.canRemediate = jest.fn().mockReturnValue(false);
expect(getRemediator(idxRemediations, values, options)).toBeInstanceOf(GenericRemediator);
expect(GenericRemediator).toHaveBeenCalledWith(idxRemediations[0], values, options);
expect(getRemediator(idxResponse, values, options)).toBeInstanceOf(GenericRemediator);
expect(GenericRemediator).toHaveBeenCalledWith(neededToProceed[0], values, options);
});
});

Expand Down

0 comments on commit c5af40c

Please sign in to comment.