|
| 1 | +const https = require('https') |
| 2 | +const EventEmitter = require('events') |
| 3 | + |
| 4 | +class MockRequest extends EventEmitter { |
| 5 | + constructor () { |
| 6 | + super() |
| 7 | + this._written_data = [] |
| 8 | + } |
| 9 | + |
| 10 | + write (data) { |
| 11 | + this._written_data.push(data) |
| 12 | + } |
| 13 | + |
| 14 | + end () { |
| 15 | + this._ended = true |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +class MockResponse extends EventEmitter { |
| 20 | + setEncoding (encoding) { |
| 21 | + this.encoding = encoding |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +const test = require('ava') |
| 26 | +const sinon = require('sinon') |
| 27 | +const NEW_EC2_INSTANCE = require('./fixtures/new-ec2-instances.json') |
| 28 | +const TEST_NOTIFICATION = require('./fixtures/test-notification.json') |
| 29 | + |
| 30 | +const handler = require('../').handler |
| 31 | + |
| 32 | +test.before((t) => { |
| 33 | + sinon.stub(https, 'request') |
| 34 | +}) |
| 35 | + |
| 36 | +test.beforeEach((t) => { |
| 37 | + https.request.reset() |
| 38 | +}) |
| 39 | + |
| 40 | +test.cb('it should call done in the context', (t) => { |
| 41 | + const request = new MockRequest() |
| 42 | + const response = new MockResponse() |
| 43 | + https.request.returns(request) |
| 44 | + https.request.yieldsAsync(response) |
| 45 | + sinon.stub(request, 'end', () => { |
| 46 | + setTimeout(() => { response.emit('end') }) |
| 47 | + }) |
| 48 | + handler(NEW_EC2_INSTANCE, { |
| 49 | + done: () => { |
| 50 | + t.pass() |
| 51 | + t.end() |
| 52 | + } |
| 53 | + }) |
| 54 | +}) |
| 55 | + |
| 56 | +test.cb('it should post to slack', (t) => { |
| 57 | + const request = new MockRequest() |
| 58 | + const response = new MockResponse() |
| 59 | + https.request.returns(request) |
| 60 | + https.request.yieldsAsync(response) |
| 61 | + sinon.stub(request, 'end', () => { |
| 62 | + setTimeout(() => { response.emit('end') }) |
| 63 | + }) |
| 64 | + handler(NEW_EC2_INSTANCE, { done: () => { |
| 65 | + sinon.assert.calledOnce(https.request) |
| 66 | + sinon.assert.calledWithExactly( |
| 67 | + https.request, |
| 68 | + { |
| 69 | + method: 'POST', |
| 70 | + hostname: 'hooks.slack.com', |
| 71 | + port: 443, |
| 72 | + path: sinon.match(/\/services\/[^\/]+\/[^\/]+\/[^\/]+/) |
| 73 | + }, |
| 74 | + sinon.match.func |
| 75 | + ) |
| 76 | + t.end() |
| 77 | + } }) |
| 78 | +}) |
| 79 | + |
| 80 | +test.cb('it should send the description and cause', (t) => { |
| 81 | + const request = new MockRequest() |
| 82 | + const response = new MockResponse() |
| 83 | + https.request.returns(request) |
| 84 | + https.request.yieldsAsync(response) |
| 85 | + sinon.stub(request, 'end', () => { |
| 86 | + setTimeout(() => { response.emit('end') }) |
| 87 | + }) |
| 88 | + handler(NEW_EC2_INSTANCE, { done: () => { |
| 89 | + t.is(request._written_data.length, 1, 'one write') |
| 90 | + const s = JSON.parse(request._written_data.pop()) |
| 91 | + t.regex(s.attachments[0].text, /Launching a new EC2 instance/) |
| 92 | + t.end() |
| 93 | + } }) |
| 94 | +}) |
| 95 | + |
| 96 | +test.cb('it should send a test message', (t) => { |
| 97 | + const request = new MockRequest() |
| 98 | + const response = new MockResponse() |
| 99 | + https.request.returns(request) |
| 100 | + https.request.yieldsAsync(response) |
| 101 | + sinon.stub(request, 'end', () => { |
| 102 | + setTimeout(() => { response.emit('end') }) |
| 103 | + }) |
| 104 | + handler(TEST_NOTIFICATION, { done: () => { |
| 105 | + t.is(request._written_data.length, 1, 'one write') |
| 106 | + const s = JSON.parse(request._written_data.pop()) |
| 107 | + t.regex(s.attachments[0].text, /test notification :partyparrot:/) |
| 108 | + t.end() |
| 109 | + } }) |
| 110 | +}) |
0 commit comments