Skip to content

Commit

Permalink
fix: set secretsPathAmplfiyAppId on initEnv when function has secrets (
Browse files Browse the repository at this point in the history
…#9853)

* fix: set secretsPathAppId on env init

* test: add test

* Update packages/amplify-category-function/src/index.ts

Co-authored-by: John Hockett <jhockett@users.noreply.github.com>

Co-authored-by: John Hockett <jhockett@users.noreply.github.com>
  • Loading branch information
edwardfoyle and jhockett committed Feb 25, 2022
1 parent b8028f6 commit 735854f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
54 changes: 51 additions & 3 deletions packages/amplify-category-function/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { initEnv, isMockable } from '..';
import sequential from 'promise-sequential';
import { pathManager, stateManager } from 'amplify-cli-core';
import { stateManager } from 'amplify-cli-core';
import { getLocalFunctionSecretNames } from '../provider-utils/awscloudformation/secrets/functionSecretsStateManager';
import { getAppId, secretsPathAmplifyAppIdKey } from '../provider-utils/awscloudformation/secrets/secretName';

jest.mock('promise-sequential');
jest.mock('amplify-cli-core', () => ({
Expand All @@ -16,11 +18,58 @@ jest.mock('amplify-cli-core', () => ({
},
}));

jest.mock('../provider-utils/awscloudformation/secrets/functionSecretsStateManager');
jest.mock('../provider-utils/awscloudformation/secrets/secretName');

const getLocalFunctionSecretNames_mock = getLocalFunctionSecretNames as jest.MockedFunction<typeof getLocalFunctionSecretNames>;
getLocalFunctionSecretNames_mock.mockReturnValue([]);
const getAppId_mock = getAppId as jest.MockedFunction<typeof getAppId>;

const sequential_mock = sequential as jest.MockedFunction<typeof sequential>;
const stateManager_mock = stateManager as jest.Mocked<typeof stateManager>;

describe('function category provider', () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe('initialize environment', () => {
it('sets secretsPathAmplfiyAppId in team-provider-info if function has secrets configured', async () => {
stateManager_mock.getTeamProviderInfo.mockReturnValueOnce({});
getLocalFunctionSecretNames_mock.mockReturnValueOnce(['TEST_SECRET']);
getAppId_mock.mockReturnValueOnce('testappid');
const contextStub = {
amplify: {
removeResourceParameters: jest.fn(),
getEnvInfo: jest.fn().mockReturnValue({ envName: 'dev' }),
getResourceStatus: () => ({
allResources: [
{
category: 'function',
resourceName: 'testFunction',
},
],
resourcesToBeCreated: [],
resourcesToBeDeleted: [],
resourcesToBeUpdated: [],
}),
},
};
await initEnv(contextStub);
expect(stateManager_mock.setTeamProviderInfo).toBeCalledTimes(1);
expect(stateManager_mock.setTeamProviderInfo.mock.calls[0][1]).toMatchObject({
dev: {
categories: {
function: {
testFunction: {
[secretsPathAmplifyAppIdKey]: 'testappid',
},
},
},
},
});
});

it('only initializes function category resources', async () => {
const resourcesToBeCreated = [
{
Expand Down Expand Up @@ -56,7 +105,7 @@ describe('function category provider', () => {
const contextStub = {
amplify: {
removeResourceParameters: jest.fn(),
getEnvInfo: jest.fn(),
getEnvInfo: jest.fn().mockReturnValue({ envName: 'dev' }),
getResourceStatus: () => ({
allResources: [...resourcesToBeCreated, ...resourcesToBeDeleted, ...resourcesToBeUpdated],
resourcesToBeCreated,
Expand All @@ -65,7 +114,6 @@ describe('function category provider', () => {
}),
},
};
contextStub.amplify.getEnvInfo.mockImplementationOnce(() => 'dev');
await initEnv(contextStub);
expect(contextStub.amplify.removeResourceParameters.mock.calls.length).toBe(1);
expect(contextStub.amplify.getEnvInfo.mock.calls.length).toBe(1);
Expand Down
12 changes: 11 additions & 1 deletion packages/amplify-category-function/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { postPushHandler } from './events/postPushHandler';
import { prePushHandler } from './events/prePushHandler';
import { updateConfigOnEnvInit } from './provider-utils/awscloudformation';
import { cloneSecretsOnEnvInitHandler } from './provider-utils/awscloudformation/secrets/cloneSecretsOnEnvInitHandler';
import { getLocalFunctionSecretNames } from './provider-utils/awscloudformation/secrets/functionSecretsStateManager';
import { getAppId, secretsPathAmplifyAppIdKey } from './provider-utils/awscloudformation/secrets/secretName';
import { buildFunction, buildTypeKeyMap } from './provider-utils/awscloudformation/utils/buildFunction';
import { ServiceName } from './provider-utils/awscloudformation/utils/constants';
import { askEnvironmentVariableCarryOut } from './provider-utils/awscloudformation/utils/environmentVariablesHelper';
Expand All @@ -27,7 +29,10 @@ export { lambdasWithApiDependency } from './provider-utils/awscloudformation/uti
export { hashLayerResource } from './provider-utils/awscloudformation/utils/layerHelpers';
export { migrateLegacyLayer } from './provider-utils/awscloudformation/utils/layerMigrationUtils';
export { packageResource } from './provider-utils/awscloudformation/utils/package';
export { updateDependentFunctionsCfn, addAppSyncInvokeMethodPermission } from './provider-utils/awscloudformation/utils/updateDependentFunctionCfn';
export {
updateDependentFunctionsCfn,
addAppSyncInvokeMethodPermission,
} from './provider-utils/awscloudformation/utils/updateDependentFunctionCfn';
export { loadFunctionParameters } from './provider-utils/awscloudformation/utils/loadFunctionParameters';

export async function add(context, providerName, service, parameters) {
Expand Down Expand Up @@ -156,6 +161,11 @@ export async function initEnv(context) {
_.set(teamProviderInfo, [envName, 'categories', categoryName, resourceName], tpiResourceParams);
_.set(amplifyMeta, [categoryName, resourceName, 's3Bucket'], s3Bucket);
}

// if the function has secrets, set the appId key in team-provider-info
if (getLocalFunctionSecretNames(resourceName, { fromCurrentCloudBackend: true }).length > 0) {
_.set(teamProviderInfo, [envName, 'categories', categoryName, resourceName, secretsPathAmplifyAppIdKey], getAppId());
}
});
resourcesToBeCreated.forEach(resource => {
const { resourceName, service } = resource;
Expand Down

0 comments on commit 735854f

Please sign in to comment.