diff --git a/packages/amplify-category-auth/src/__tests__/provider-utils/awscloudformation/utils/auth-defaults-appliers.test.ts b/packages/amplify-category-auth/src/__tests__/provider-utils/awscloudformation/utils/auth-defaults-appliers.test.ts index b481f2677f1..047eb302234 100644 --- a/packages/amplify-category-auth/src/__tests__/provider-utils/awscloudformation/utils/auth-defaults-appliers.test.ts +++ b/packages/amplify-category-auth/src/__tests__/provider-utils/awscloudformation/utils/auth-defaults-appliers.test.ts @@ -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', () => ({ @@ -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([]); + }); }); diff --git a/packages/amplify-category-auth/src/provider-utils/awscloudformation/utils/auth-defaults-appliers.ts b/packages/amplify-category-auth/src/provider-utils/awscloudformation/utils/auth-defaults-appliers.ts index c2eca7f1161..14006f24678 100644 --- a/packages/amplify-category-auth/src/provider-utils/awscloudformation/utils/auth-defaults-appliers.ts +++ b/packages/amplify-category-auth/src/provider-utils/awscloudformation/utils/auth-defaults-appliers.ts @@ -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'; @@ -18,7 +18,7 @@ export const getAddAuthDefaultsApplier = (context: any, defaultValuesFilename: s result: ServiceQuestionsResult, ): Promise => { const { functionMap, generalDefaults, roles, getAllDefaults } = await import(`../assets/${defaultValuesFilename}`); - result = merge(generalDefaults(projectName), result); + result = assignDefaults({}, generalDefaults(projectName), result); await verificationBucketName(result); @@ -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 ( @@ -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 (a: T, b: U, c?: V, d?: W) => T & U & V & W;