Skip to content

Commit

Permalink
fix: ensure auth selections overwrite defaults (#6071)
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardfoyle committed Dec 11, 2020
1 parent 6fb0b07 commit 4b22fb2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { ServiceQuestionsResult } from '../../../../provider-utils/awscloudformation/service-walkthrough-types';
import { structureOAuthMetadata } from '../../../../provider-utils/awscloudformation/service-walkthroughs/auth-questions';
import { getUpdateAuthDefaultsApplier } from '../../../../provider-utils/awscloudformation/utils/auth-defaults-appliers';
import {
getAddAuthDefaultsApplier,
getUpdateAuthDefaultsApplier,
} from '../../../../provider-utils/awscloudformation/utils/auth-defaults-appliers';

jest.mock(`../../../../provider-utils/awscloudformation/assets/cognito-defaults.js`, () => ({
functionMap: {
userPoolOnly: () => ({ some: 'default value' }),
},
getAllDefaults: jest.fn(),
generalDefaults: jest.fn().mockReturnValue({ requiredAttributes: ['email'] }),
}));

jest.mock('../../../../provider-utils/awscloudformation/service-walkthroughs/auth-questions', () => ({
Expand All @@ -26,4 +30,28 @@ describe('update auth defaults applier', () => {
expect(result).toMatchSnapshot();
expect(structureOAuthMetadata_mock.mock.calls.length).toBe(1);
});

it('overwrites default parameters', async () => {
const stubResult = {
useDefault: 'manual',
authSelections: 'userPoolOnly',
requiredAttributes: [] as string[],
} as ServiceQuestionsResult;

const result = await getUpdateAuthDefaultsApplier({}, 'cognito-defaults.js', {} as ServiceQuestionsResult)(stubResult);
expect(result.requiredAttributes).toEqual([]);
});
});

describe('add auth defaults applier', () => {
it('overwrites default parameters', async () => {
const stubResult: ServiceQuestionsResult = {
useDefault: 'manual',
authSelections: 'userPoolOnly',
requiredAttributes: [] as string[],
} as ServiceQuestionsResult;

const result = await getAddAuthDefaultsApplier({}, 'cognito-defaults.js', 'testProjectName')(stubResult);
expect(result.requiredAttributes).toEqual([]);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ServiceQuestionsResult } from '../service-walkthrough-types';
import { verificationBucketName } from './verification-bucket-name';
import { isEmpty, merge } from 'lodash';
import { assign, isEmpty } from 'lodash';
import { structureOAuthMetadata } from '../service-walkthroughs/auth-questions';
import { removeDeprecatedProps } from './synthesize-resources';
import { immutableAttributes, safeDefaults } from '../constants';
Expand All @@ -17,15 +17,15 @@ export const getAddAuthDefaultsApplier = (context: any, defaultValuesFilename: s
result: ServiceQuestionsResult,
): Promise<ServiceQuestionsResult> => {
const { functionMap, generalDefaults, roles, getAllDefaults } = await import(`../assets/${defaultValuesFilename}`);
result = merge(generalDefaults(projectName), result);
result = assign(generalDefaults(projectName), result);

await verificationBucketName(result);

structureOAuthMetadata(result, context, getAllDefaults, context.amplify); // adds "oauthMetadata" to result

/* merge actual answers object into props object,
* ensuring that manual entries override defaults */
return merge(functionMap[result.authSelections](result.resourceName), result, roles);
return assign(functionMap[result.authSelections](result.resourceName), result, roles);
};

export const getUpdateAuthDefaultsApplier = (context: any, defaultValuesFilename: string, previousResult: ServiceQuestionsResult) => async (
Expand Down Expand Up @@ -53,5 +53,5 @@ export const getUpdateAuthDefaultsApplier = (context: any, defaultValuesFilename
if (!isEmpty(result.triggers)) {
previousResult.triggers = Object.assign({}, result.triggers);
}
return merge(defaults, removeDeprecatedProps(previousResult), result);
return assign(defaults, removeDeprecatedProps(previousResult), result);
};

0 comments on commit 4b22fb2

Please sign in to comment.