|
| 1 | +import { Template, Match } from '@aws-cdk/assertions'; |
| 2 | +import * as iam from '@aws-cdk/aws-iam'; |
| 3 | +import * as iot from '@aws-cdk/aws-iot'; |
| 4 | +import * as sqs from '@aws-cdk/aws-sqs'; |
| 5 | +import * as cdk from '@aws-cdk/core'; |
| 6 | +import * as actions from '../../lib'; |
| 7 | + |
| 8 | +test('Default SQS queue action', () => { |
| 9 | + // GIVEN |
| 10 | + const stack = new cdk.Stack(); |
| 11 | + const topicRule = new iot.TopicRule(stack, 'MyTopicRule', { |
| 12 | + sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"), |
| 13 | + }); |
| 14 | + const queue = sqs.Queue.fromQueueArn(stack, 'MyQueue', 'arn:aws:sqs::123456789012:test-queue'); |
| 15 | + |
| 16 | + // WHEN |
| 17 | + topicRule.addAction(new actions.SqsQueueAction(queue)); |
| 18 | + |
| 19 | + // THEN |
| 20 | + Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', { |
| 21 | + TopicRulePayload: { |
| 22 | + Actions: [ |
| 23 | + { |
| 24 | + Sqs: { |
| 25 | + QueueUrl: { |
| 26 | + 'Fn::Join': ['', [ |
| 27 | + 'https://sqs..', |
| 28 | + { Ref: 'AWS::URLSuffix' }, |
| 29 | + '/123456789012/test-queue', |
| 30 | + ]], |
| 31 | + }, |
| 32 | + RoleArn: { |
| 33 | + 'Fn::GetAtt': [ |
| 34 | + 'MyTopicRuleTopicRuleActionRoleCE2D05DA', |
| 35 | + 'Arn', |
| 36 | + ], |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + ], |
| 41 | + }, |
| 42 | + }); |
| 43 | + |
| 44 | + Template.fromStack(stack).hasResourceProperties('AWS::IAM::Role', { |
| 45 | + AssumeRolePolicyDocument: { |
| 46 | + Statement: [ |
| 47 | + { |
| 48 | + Action: 'sts:AssumeRole', |
| 49 | + Effect: 'Allow', |
| 50 | + Principal: { |
| 51 | + Service: 'iot.amazonaws.com', |
| 52 | + }, |
| 53 | + }, |
| 54 | + ], |
| 55 | + Version: '2012-10-17', |
| 56 | + }, |
| 57 | + }); |
| 58 | + |
| 59 | + Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', { |
| 60 | + PolicyDocument: { |
| 61 | + Statement: [ |
| 62 | + { |
| 63 | + Action: 'sqs:SendMessage', |
| 64 | + Effect: 'Allow', |
| 65 | + Resource: 'arn:aws:sqs::123456789012:test-queue', |
| 66 | + }, |
| 67 | + ], |
| 68 | + Version: '2012-10-17', |
| 69 | + }, |
| 70 | + PolicyName: 'MyTopicRuleTopicRuleActionRoleDefaultPolicy54A701F7', |
| 71 | + Roles: [ |
| 72 | + { Ref: 'MyTopicRuleTopicRuleActionRoleCE2D05DA' }, |
| 73 | + ], |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +test('Can set useBase64', () => { |
| 78 | + // GIVEN |
| 79 | + const stack = new cdk.Stack(); |
| 80 | + const topicRule = new iot.TopicRule(stack, 'MyTopicRule', { |
| 81 | + sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"), |
| 82 | + }); |
| 83 | + const queue = sqs.Queue.fromQueueArn(stack, 'MyQueue', 'arn:aws:sqs::123456789012:test-queue'); |
| 84 | + |
| 85 | + // WHEN |
| 86 | + topicRule.addAction(new actions.SqsQueueAction(queue, { |
| 87 | + useBase64: true, |
| 88 | + })); |
| 89 | + |
| 90 | + // THEN |
| 91 | + Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', { |
| 92 | + TopicRulePayload: { |
| 93 | + Actions: [ |
| 94 | + Match.objectLike({ Sqs: { UseBase64: true } }), |
| 95 | + ], |
| 96 | + }, |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +test('Can set role', () => { |
| 101 | + // GIVEN |
| 102 | + const stack = new cdk.Stack(); |
| 103 | + const topicRule = new iot.TopicRule(stack, 'MyTopicRule', { |
| 104 | + sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"), |
| 105 | + }); |
| 106 | + const queue = sqs.Queue.fromQueueArn(stack, 'MyQueue', 'arn:aws:sqs::123456789012:test-queue'); |
| 107 | + const role = iam.Role.fromRoleArn(stack, 'MyRole', 'arn:aws:iam::123456789012:role/ForTest'); |
| 108 | + |
| 109 | + // WHEN |
| 110 | + topicRule.addAction(new actions.SqsQueueAction(queue, { role })); |
| 111 | + |
| 112 | + // THEN |
| 113 | + Template.fromStack(stack).hasResourceProperties('AWS::IoT::TopicRule', { |
| 114 | + TopicRulePayload: { |
| 115 | + Actions: [ |
| 116 | + Match.objectLike({ |
| 117 | + Sqs: { |
| 118 | + RoleArn: 'arn:aws:iam::123456789012:role/ForTest', |
| 119 | + }, |
| 120 | + }), |
| 121 | + ], |
| 122 | + }, |
| 123 | + }); |
| 124 | + |
| 125 | + Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', { |
| 126 | + PolicyName: 'MyRolePolicy64AB00A5', |
| 127 | + Roles: ['ForTest'], |
| 128 | + }); |
| 129 | +}); |
0 commit comments