1 | 'use strict'
|
2 |
|
3 | const assert = require('chai').assert
|
4 | const path = require('path')
|
5 | const aws = require('aws-sdk-mock')
|
6 | aws.setSDK(path.resolve('node_modules/aws-sdk'))
|
7 | const ScheduleEvents = require(path.join('..', 'lib', 'schedule_events'))
|
8 |
|
9 | const params = {
|
10 | FunctionArn: 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function',
|
11 | ScheduleName: 'node-lambda-test-schedule',
|
12 | ScheduleState: 'ENABLED',
|
13 | ScheduleExpression: 'rate(1 hour)',
|
14 | ScheduleDescription: null
|
15 | }
|
16 |
|
17 | const mockResponse = {
|
18 | putRule: {
|
19 | RuleArn: 'arn:aws:events:hoge:fuga'
|
20 | },
|
21 |
|
22 | addPermission: {
|
23 | Statement: JSON.stringify({
|
24 | Sid: 'node-lambda-test-schedule',
|
25 | Resource: 'arn:aws:lambda:piyo',
|
26 | Effect: 'Allow',
|
27 | Principal: { Service: 'events.amazonaws.com' },
|
28 | Action: [ 'lambda:InvokeFunction' ],
|
29 | Condition: { ArnLike: { 'AWS:SourceArn': 'arn:aws:events:hoge:fuga' } }
|
30 | })
|
31 | },
|
32 |
|
33 | putTargets: {
|
34 | FailedEntries: [],
|
35 | FailedEntryCount: 0
|
36 | }
|
37 | }
|
38 |
|
39 | var schedule = null
|
40 |
|
41 |
|
42 | describe('lib/schedule_events', () => {
|
43 | before(() => {
|
44 | aws.mock('CloudWatchEvents', 'putRule', (params, callback) => {
|
45 | callback(null, mockResponse.putRule)
|
46 | })
|
47 | aws.mock('CloudWatchEvents', 'putTargets', (params, callback) => {
|
48 | callback(null, mockResponse.putTargets)
|
49 | })
|
50 | aws.mock('Lambda', 'addPermission', (params, callback) => {
|
51 | callback(null, mockResponse.addPermission)
|
52 | })
|
53 |
|
54 | schedule = new ScheduleEvents(require('aws-sdk'))
|
55 | })
|
56 |
|
57 | after(() => {
|
58 | aws.restore('CloudWatchEvents')
|
59 | aws.restore('Lambda')
|
60 | })
|
61 |
|
62 | describe('_ruleDescription (default)', () => {
|
63 | it('correct value', () => {
|
64 | assert.equal(
|
65 | schedule._ruleDescription(params),
|
66 | 'node-lambda-test-schedule - rate(1 hour)'
|
67 | )
|
68 | })
|
69 | })
|
70 |
|
71 | describe('_ruleDescription (custom)', () => {
|
72 | before(() => {
|
73 | params.ScheduleDescription = 'Run node-lambda-test-function once per hour'
|
74 | })
|
75 |
|
76 | after(() => {
|
77 | params.ScheduleDescription = null
|
78 | })
|
79 |
|
80 | it('correct value', () => {
|
81 | assert.equal(
|
82 | schedule._ruleDescription(params),
|
83 | 'Run node-lambda-test-function once per hour'
|
84 | )
|
85 | })
|
86 | })
|
87 |
|
88 | describe('_functionName', () => {
|
89 | it('correct value', () => {
|
90 | assert.equal(
|
91 | schedule._functionName(params),
|
92 | 'node-lambda-test-function'
|
93 | )
|
94 | })
|
95 | })
|
96 |
|
97 | describe('_putRulePrams', () => {
|
98 | it('correct value', () => {
|
99 | const expected = {
|
100 | Name: 'node-lambda-test-schedule',
|
101 | Description: 'node-lambda-test-schedule - rate(1 hour)',
|
102 | State: 'ENABLED',
|
103 | ScheduleExpression: 'rate(1 hour)'
|
104 | }
|
105 | assert.deepEqual(schedule._putRulePrams(params), expected)
|
106 | })
|
107 | })
|
108 |
|
109 | describe('_addPermissionParams', () => {
|
110 | it('correct value', () => {
|
111 | const expected = {
|
112 | Action: 'lambda:InvokeFunction',
|
113 | FunctionName: 'node-lambda-test-function',
|
114 | Principal: 'events.amazonaws.com',
|
115 | SourceArn: 'arn:aws:events:hoge:fuga',
|
116 | StatementId: 'node-lambda-test-schedule'
|
117 | }
|
118 | const _params = Object.assign(params, mockResponse.putRule)
|
119 | assert.deepEqual(schedule._addPermissionParams(_params), expected)
|
120 | })
|
121 | })
|
122 |
|
123 | describe('_putTargetsParams', () => {
|
124 | it('correct value (No "Input" setting)', () => {
|
125 | const expected = {
|
126 | Rule: 'node-lambda-test-schedule',
|
127 | Targets: [{
|
128 | Arn: 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function',
|
129 | Id: 'node-lambda-test-function',
|
130 | Input: ''
|
131 | }]
|
132 | }
|
133 | assert.deepEqual(schedule._putTargetsParams(params), expected)
|
134 | })
|
135 |
|
136 | it('correct value ("Input" setting)', () => {
|
137 | const expected = {
|
138 | Rule: 'node-lambda-test-schedule',
|
139 | Targets: [{
|
140 | Arn: 'arn:aws:lambda:us-west-2:XXX:function:node-lambda-test-function',
|
141 | Id: 'node-lambda-test-function',
|
142 | Input: '{"key":"value"}'
|
143 | }]
|
144 | }
|
145 | assert.deepEqual(
|
146 | schedule._putTargetsParams(Object.assign({Input: {key: 'value'}}, params)),
|
147 | expected
|
148 | )
|
149 | })
|
150 | })
|
151 |
|
152 | describe('_putRule', () => {
|
153 | it('using mock', () => {
|
154 | return schedule._putRule(params).then((data) => {
|
155 | assert.deepEqual(data, mockResponse.putRule)
|
156 | })
|
157 | })
|
158 | })
|
159 |
|
160 | describe('_addPermission', () => {
|
161 | it('using mock', () => {
|
162 | const _params = Object.assign(params, mockResponse.putTargets)
|
163 | return schedule._addPermission(_params).then((data) => {
|
164 | assert.deepEqual(data, mockResponse.addPermission)
|
165 | })
|
166 | })
|
167 | })
|
168 |
|
169 | describe('_putTargets', () => {
|
170 | it('using mock', () => {
|
171 | return schedule._putTargets(params).then((data) => {
|
172 | assert.deepEqual(data, mockResponse.putTargets)
|
173 | })
|
174 | })
|
175 | })
|
176 |
|
177 | describe('add', () => {
|
178 | it('using mock', () => {
|
179 | return schedule.add(params).then((data) => {
|
180 | assert.deepEqual(data, mockResponse.putTargets)
|
181 | })
|
182 | })
|
183 | })
|
184 | })
|