From 83881767bd19e6bccefc75f770b3bfba0810a2bc Mon Sep 17 00:00:00 2001 From: Aleksa Perovic Date: Tue, 29 Oct 2024 10:53:22 +0100 Subject: [PATCH] test: fix notification & scp test cases (#668) --- .../notification-opsitem-created.test.ts | 30 +++++++++ .../service-control-policies-root.test.ts | 67 +++++++++++++++---- 2 files changed, 84 insertions(+), 13 deletions(-) diff --git a/cdk/test/functions/notification-opsitem-created.test.ts b/cdk/test/functions/notification-opsitem-created.test.ts index 7e1d612f..cc8ad1f8 100644 --- a/cdk/test/functions/notification-opsitem-created.test.ts +++ b/cdk/test/functions/notification-opsitem-created.test.ts @@ -6,6 +6,8 @@ import { handler } from '../../src/functions/notification-opsitem-created'; const snsClientMock = mockClient(SNSClient); const opsItemId = 'message_123'; const title = 'test_message_title'; +const titleLong = 'test_message_title_longer_than_100_characters_which_should_be_cut_off_so_it_has_to_be_very_long'; +const titleLongCutOff = 'test_message_title_longer_than_100_characters_which_should_be_cut_off_so_it_has_to_b...'; const description = 'test_message_description'; const region = 'us-east-1'; const url = `https://${region}.console.aws.amazon.com/systems-manager/opsitems/${opsItemId}`; @@ -50,4 +52,32 @@ describe('notifications_opsitems', () => { TopicArn: process.env.TOPIC_ARN, }); }); + + it('notifications_opsitems_create_cut_off_long_title', async () => { + snsClientMock.on(PublishCommand).resolves({ + MessageId: 'Message_123', + }); + + await handler( + { + detail: { + responseElements: { + opsItemId: opsItemId, + }, + requestParameters: { + description: description, + title: titleLong, + }, + }, + }, + {}, + ); + + expect(snsClientMock).toReceiveCommandTimes(PublishCommand, 1); + expect(snsClientMock).toReceiveCommandWith(PublishCommand, { + Message: `${description}\n\n${url}`, + Subject: `New OpsItem: ${titleLongCutOff}`, + TopicArn: process.env.TOPIC_ARN, + }); + }); }); diff --git a/cdk/test/functions/service-control-policies-root.test.ts b/cdk/test/functions/service-control-policies-root.test.ts index 2804167f..b69e48d1 100644 --- a/cdk/test/functions/service-control-policies-root.test.ts +++ b/cdk/test/functions/service-control-policies-root.test.ts @@ -6,7 +6,6 @@ import { ListPoliciesCommand, ListRootsCommand, OrganizationsClient, - PolicyType, UpdatePolicyCommand, } from '@aws-sdk/client-organizations'; import { CloudFormationCustomResourceCreateEvent, Context } from 'aws-lambda'; @@ -20,7 +19,6 @@ const rootAccountId = 'test-root-id'; const logicalResourceId = 'superwerker - SCPRoot'; const scpName = 'superwerker-root'; const policyId = 'test-policy-id'; -const scpPolicy = '{"Version": "2012-10-17", "Statement": [/* policy document */]}'; const updatedSCPPolicy = '{"Version": "2012-10-17", "Statement": [/* UPDATED policy document */]}'; const description = 'superwerker - SCPRoot'; @@ -70,10 +68,57 @@ describe('service control policies', () => { { RequestType: 'Create', ResourceProperties: { - Type: PolicyType.SERVICE_CONTROL_POLICY, - Description: logicalResourceId, + includeSecHub: 'Yes', + includeBackup: 'Yes', + partition: 'eu-central-1', + scpName: 'superwerker-root', + }, + } as unknown as CloudFormationCustomResourceCreateEvent, + {} as Context, + ); + + expect(response).toMatchObject({ SUCCESS: 'SCPs have been successfully created for Root account' }); + }); + + it('create service control policy sechub', async () => { + organizationClientMock.on(ListRootsCommand).resolves({ + Roots: [ + { + Id: rootAccountId, + Name: rootAccountName, + }, + ], + }); + + organizationClientMock.on(ListPoliciesCommand).resolves({ + Policies: [ + { + Name: scpName, + Id: logicalResourceId, + Description: description, + }, + ], + }); + + organizationClientMock.on(CreatePolicyCommand).resolves({ + Policy: { + PolicySummary: { Name: scpName, - Content: scpPolicy, + Id: policyId, + }, + }, + }); + + organizationClientMock.on(AttachPolicyCommand).resolves({}); + + const response = await handler( + { + RequestType: 'Create', + ResourceProperties: { + includeSecHub: 'No', + includeBackup: 'No', + partition: 'eu-central-1', + scpName: 'superwerker-root', }, } as unknown as CloudFormationCustomResourceCreateEvent, {} as Context, @@ -107,10 +152,10 @@ describe('service control policies', () => { { RequestType: 'Update', ResourceProperties: { - Type: PolicyType.SERVICE_CONTROL_POLICY, - Description: logicalResourceId, - scpName: scpName, - Content: updatedSCPPolicy, + includeSecHub: 'Yes', + includeBackup: 'Yes', + partition: 'eu-central-1', + scpName: 'superwerker-root', }, } as unknown as CloudFormationCustomResourceCreateEvent, {} as Context, @@ -181,10 +226,8 @@ describe('service control policies', () => { { RequestType: 'Create', ResourceProperties: { - Type: PolicyType.SERVICE_CONTROL_POLICY, Description: logicalResourceId, scpName: scpName, - Content: updatedSCPPolicy, }, } as unknown as CloudFormationCustomResourceCreateEvent, {} as Context, @@ -220,10 +263,8 @@ describe('service control policies', () => { { RequestType: 'Update', ResourceProperties: { - Type: PolicyType.SERVICE_CONTROL_POLICY, Description: logicalResourceId, scpName: 'mock-scp-name', //should throw an error since there will be no matching policy for this name - Content: updatedSCPPolicy, }, } as unknown as CloudFormationCustomResourceCreateEvent, {} as Context,