Skip to content

Commit

Permalink
fix: apply empty arrays as default values but not undefined values (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardfoyle committed Jan 21, 2021
1 parent 788fb1e commit c2d2a6c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
@@ -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 Down Expand Up @@ -41,4 +45,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([]);
});
});
@@ -1,6 +1,6 @@
import { ServiceQuestionsResult } from '../service-walkthrough-types';
import { verificationBucketName } from './verification-bucket-name';
import { isEmpty, merge } from 'lodash';
import _ from 'lodash';
import { structureOAuthMetadata } from '../service-walkthroughs/auth-questions';
import { removeDeprecatedProps } from './synthesize-resources';
import { immutableAttributes, safeDefaults } from '../constants';
Expand All @@ -18,7 +18,7 @@ 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 = assignDefaults({}, generalDefaults(projectName), result);

await verificationBucketName(result);

Expand All @@ -32,7 +32,7 @@ export const getAddAuthDefaultsApplier = (context: any, defaultValuesFilename: s

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

export const getUpdateAuthDefaultsApplier = (context: any, defaultValuesFilename: string, previousResult: ServiceQuestionsResult) => async (
Expand All @@ -57,8 +57,14 @@ export const getUpdateAuthDefaultsApplier = (context: any, defaultValuesFilename
structureOAuthMetadata(result, context, getAllDefaults, context.amplify); // adds "oauthMetadata" to result

// If there are new trigger selections, make sure they overwrite the previous selections
if (!isEmpty(result.triggers)) {
if (!_.isEmpty(result.triggers)) {
previousResult.triggers = Object.assign({}, result.triggers);
}
return merge(defaults, removeDeprecatedProps(previousResult), result);
return assignDefaults({}, defaults, removeDeprecatedProps(previousResult), result);
};

// same as _.assign except undefined values won't overwrite existing values
// typed to accept up to 4 params but could be typed to accept any number of params
const assignDefaults = _.partialRight(_.assignWith, (objValue: unknown, srcValue: unknown) =>
_.isUndefined(srcValue) ? objValue : srcValue,
) as <T, U, V, W>(a: T, b: U, c?: V, d?: W) => T & U & V & W;

0 comments on commit c2d2a6c

Please sign in to comment.