Skip to content

Commit

Permalink
fix: e2e test for generating custom policies (#8580)
Browse files Browse the repository at this point in the history
* fix: logical and instead of logical or

* test: increased test coverage
  • Loading branch information
ammarkarachi authored Oct 27, 2021
1 parent 39fbe6f commit b0a17a2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ const cfnTemplate = {
} as unknown as Template;
const templateFormat = CFNTemplateFormat.JSON;

const customPolicies: CustomIAMPolicies = [
{
Action: ['test:test'],
Effect: 'Allow',
Resource: ['arn:aws:s3:us-east-2:012345678910:testResource'],
},
];

readCFNTemplate_mock.mockResolvedValue({
templateFormat,
cfnTemplate,
Expand All @@ -40,7 +32,6 @@ const resourcePath = 'api/resourceName/cfn-template-name.json';

pathManager_mock.getBackendDirPath.mockReturnValue(backendPath);
pathManager_mock.getResourceDirectoryPath.mockReturnValue(backendPath);
stateManager_mock.getCustomPolicies.mockReturnValue(customPolicies);
stateManager_mock.getLocalEnvInfo.mockReturnValue({ envName: 'test' });

describe('preProcessCFNTemplate', () => {
Expand All @@ -65,4 +56,30 @@ describe('preProcessCFNTemplate', () => {
const newPath = await preProcessCFNTemplate(path.join('/something/else', resourcePath));
expect(newPath).toMatchInlineSnapshot(`"/project/amplify/backend/awscloudformation/build/cfn-template-name.json"`);
});

it('test writeCustonPolicies with Lambda function', () => {
writeCustomPoliciesToCFNTemplate('testLambdaResourceName', 'Lambda', '../dummypath', 'function');
expect(pathManager_mock.getResourceDirectoryPath).toBeCalledWith(undefined, 'function', 'testLambdaResourceName');
expect(readCFNTemplate_mock).toBeCalled();
});

it('test writeCustonPolicies with LambdaLayer function', () => {
writeCustomPoliciesToCFNTemplate('testLambdaResourceName', 'LambdaLayer', '../dummypath', 'function');
expect(pathManager_mock.getResourceDirectoryPath).not.toBeCalled();
expect(readCFNTemplate_mock).not.toBeCalled();
});

it('test writeCustonPolicies with Containers Api', () => {
writeCustomPoliciesToCFNTemplate('testApiResourceName', 'ElasticContainer', '../dummypath', 'api');
expect(pathManager_mock.getResourceDirectoryPath).toBeCalledWith(undefined, 'api', 'testApiResourceName');
expect(readCFNTemplate_mock).toBeCalled();
});

it('test writeCustonPolicies with Appsync', () => {
pathManager_mock.getResourceDirectoryPath.mockClear();
readCFNTemplate_mock.mockClear();
writeCustomPoliciesToCFNTemplate('testApiResourceName', 'AppSync', '../dummypath', 'api');
expect(pathManager_mock.getResourceDirectoryPath).not.toBeCalled();
expect(readCFNTemplate_mock).not.toBeCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function preProcessCFNTemplate(filePath: string): Promise<string> {

//get data from custom polcies file and write custom policies to CFN template
export async function writeCustomPoliciesToCFNTemplate(resourceName: string, service: string, cfnFile: string, category: string) {
if ((category !== 'api' && service !== 'ElasticContainer') || (category !== 'function' && service !== 'Lambda')) {
if (!(category === 'api' && service === 'ElasticContainer') && !(category === 'function' && service === 'Lambda')) {
return;
}

Expand Down

0 comments on commit b0a17a2

Please sign in to comment.