Skip to content

Commit

Permalink
test: fix notification & scp test cases (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
aperov9 authored Oct 29, 2024
1 parent d9d1786 commit 8388176
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 13 deletions.
30 changes: 30 additions & 0 deletions cdk/test/functions/notification-opsitem-created.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down Expand Up @@ -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,
});
});
});
67 changes: 54 additions & 13 deletions cdk/test/functions/service-control-policies-root.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
ListPoliciesCommand,
ListRootsCommand,
OrganizationsClient,
PolicyType,
UpdatePolicyCommand,
} from '@aws-sdk/client-organizations';
import { CloudFormationCustomResourceCreateEvent, Context } from 'aws-lambda';
Expand All @@ -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';

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8388176

Please sign in to comment.