This repository has been archived by the owner on Dec 8, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmiddleware-test.js
259 lines (215 loc) · 9.51 KB
/
middleware-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
'use strict';
var Middleware = require('../lib/middleware');
var Config = require('../lib/config');
var Rule = require('../lib/rule');
var GitHubClient = require('../lib/github-client');
var SlackClient = require('../lib/slack-client');
var Logger = require('../lib/logger');
var helpers = require('./helpers');
var chai = require('chai');
var sinon = require('sinon');
var chaiAsPromised = require('chai-as-promised');
var chaiThings = require('chai-things');
var expect = chai.expect;
chai.should();
chai.use(chaiAsPromised);
chai.use(chaiThings);
describe('Middleware', function() {
var config, slackClient, githubClient, logger, middleware;
beforeEach(function() {
config = new Config(helpers.baseConfig());
slackClient = new SlackClient(undefined, config);
githubClient = new GitHubClient(config);
logger = new Logger(console);
middleware = new Middleware(config, slackClient, githubClient, logger);
});
describe('findMatchingRule', function() {
var getChannelName, message;
beforeEach(function() {
getChannelName = sinon.stub(slackClient, 'getChannelName');
getChannelName.returns('not-any-channel-from-any-config-rule');
message = helpers.reactionAddedMessage();
});
afterEach(function() {
getChannelName.restore();
});
it('should find the rule matching the message', function() {
var expected = config.rules[1],
result = middleware.findMatchingRule(message);
result.reactionName.should.equal(expected.reactionName);
result.githubRepository.should.equal(expected.githubRepository);
result.should.not.have.property('channelName');
});
it('should ignore a message if it is undefined', function() {
// When execute() tries to pass context.response.message.rawMessage from
// a message that doesn't have one, the argument to findMatchingRule()
// will be undefined.
expect(middleware.findMatchingRule(undefined)).to.be.undefined;
});
it('should ignore a message if its type does not match', function() {
message.type = 'hello';
expect(middleware.findMatchingRule(message)).to.be.undefined;
});
it('should ignore a message if its item type does not match', function() {
message.item.type = 'file';
expect(middleware.findMatchingRule(message)).to.be.undefined;
});
it('should ignore messages that do not match any rule', function() {
message.reaction = 'sad-face';
expect(middleware.findMatchingRule(message)).to.be.undefined;
});
});
describe('parseMetadata', function() {
var getChannelName;
beforeEach(function() {
getChannelName = sinon.stub(slackClient, 'getChannelName');
getChannelName.returns('handbook');
});
afterEach(function() {
getChannelName.restore();
});
it('should parse GitHub request metadata from a message', function() {
middleware.parseMetadata(helpers.messageWithReactions())
.should.eql(helpers.metadata());
getChannelName.calledOnce.should.be.true;
getChannelName.args.should.have.deep.property('[0]')
.that.deep.equals([helpers.CHANNEL_ID]);
});
});
describe('execute', function() {
var context, next, hubotDone, checkErrorResponse;
beforeEach(function() {
context = {
response: {
message: helpers.fullReactionAddedMessage(),
reply: sinon.spy()
}
};
next = sinon.spy();
hubotDone = sinon.spy();
slackClient = sinon.stub(slackClient);
githubClient = sinon.stub(githubClient);
logger = sinon.stub(logger);
slackClient.getChannelName.returns('handbook');
slackClient.getTeamDomain.returns('18f');
slackClient.getReactions
.returns(Promise.resolve(helpers.messageWithReactions()));
githubClient.fileNewIssue.returns(Promise.resolve(helpers.ISSUE_URL));
slackClient.addSuccessReaction
.returns(Promise.resolve(helpers.ISSUE_URL));
});
it('should receive a message and file an issue', function() {
return middleware.execute(context, next, hubotDone)
.should.become(helpers.ISSUE_URL).then(function() {
var matchingRule = new Rule(helpers.baseConfig().rules[1]);
context.response.reply.args.should.eql([
['created: ' + helpers.ISSUE_URL]
]);
next.calledWith(hubotDone).should.be.true;
logger.info.args.should.eql([
helpers.logArgs('matches rule:', matchingRule),
helpers.logArgs('getting reactions for', helpers.PERMALINK),
helpers.logArgs('making GitHub request for', helpers.PERMALINK),
helpers.logArgs('adding', helpers.baseConfig().successReaction),
helpers.logArgs('created: ' + helpers.ISSUE_URL)
]);
});
});
it('should ignore messages that do not match', function() {
delete context.response.message.rawMessage;
expect(middleware.execute(context, next, hubotDone)).to.be.undefined;
next.calledWith(hubotDone).should.be.true;
});
it('should not file another issue for the same message when ' +
'one is in progress', function() {
var result;
result = middleware.execute(context, next, hubotDone);
expect(middleware.execute(context, next, hubotDone)).to.eql(undefined,
'middleware.execute did not prevent filing a second issue ' +
'when one was already in progress');
return result.should.become(helpers.ISSUE_URL).then(function() {
logger.info.args.should.include.something.that.deep.equals(
helpers.logArgs('already in progress'));
// Make another call to ensure that the ID is cleaned up. Normally the
// message will have a successReaction after the first successful
// request, but we'll test that in another case.
return middleware.execute(context, next, hubotDone)
.should.become(helpers.ISSUE_URL);
});
});
it('should not file another issue for the same message when ' +
'one is already filed ', function() {
var message = helpers.messageWithReactions();
message.message.reactions.push({
name: config.successReaction,
count: 1,
users: [ helpers.USER_ID ]
});
slackClient.getReactions.returns(Promise.resolve(message));
return middleware.execute(context, next, hubotDone)
.should.be.rejectedWith('already processed').then(function() {
slackClient.getReactions.calledOnce.should.be.true;
githubClient.fileNewIssue.called.should.be.false;
slackClient.addSuccessReaction.called.should.be.false;
context.response.reply.called.should.be.false;
logger.info.args.should.include.something.that.deep.equals(
helpers.logArgs('already processed ' + helpers.PERMALINK));
});
});
checkErrorResponse = function(errorMessage) {
context.response.reply.args.should.have.deep.property(
'[0][0].message', errorMessage);
logger.error.args.should.have.deep.property('[0][0]', helpers.MESSAGE_ID);
logger.error.args.should.have.deep.property('[0][1]', errorMessage);
};
it('should receive a message but fail to get reactions', function() {
var errorMessage = 'failed to get reactions for ' + helpers.PERMALINK +
': test failure';
slackClient.getReactions
.returns(Promise.reject(new Error('test failure')));
return middleware.execute(context, next, hubotDone)
.should.be.rejectedWith(errorMessage).then(function() {
slackClient.getReactions.calledOnce.should.be.true;
githubClient.fileNewIssue.called.should.be.false;
slackClient.addSuccessReaction.called.should.be.false;
checkErrorResponse(errorMessage);
});
});
it('should get reactions but fail to file an issue', function() {
var errorMessage = 'failed to create a GitHub issue in 18F/handbook: ' +
'test failure';
githubClient.fileNewIssue
.returns(Promise.reject(new Error('test failure')));
return middleware.execute(context, next, hubotDone)
.should.be.rejectedWith(errorMessage).then(function() {
slackClient.getReactions.calledOnce.should.be.true;
githubClient.fileNewIssue.calledOnce.should.be.true;
slackClient.addSuccessReaction.called.should.be.false;
checkErrorResponse(errorMessage);
});
});
it('should file an issue but fail to add a reaction', function() {
var errorMessage = 'created ' + helpers.ISSUE_URL +
' but failed to add ' + helpers.baseConfig().successReaction +
': test failure';
slackClient.addSuccessReaction
.returns(Promise.reject(new Error('test failure')));
return middleware.execute(context, next, hubotDone)
.should.be.rejectedWith(errorMessage).then(function() {
slackClient.getReactions.calledOnce.should.be.true;
githubClient.fileNewIssue.calledOnce.should.be.true;
slackClient.addSuccessReaction.calledOnce.should.be.true;
checkErrorResponse(errorMessage);
});
});
it('should catch and log unanticipated errors', function() {
var errorMessage = 'unhandled error: Error\nmessage: ' +
JSON.stringify(helpers.reactionAddedMessage(), null, 2);
slackClient.getChannelName.throws();
expect(middleware.execute(context, next, hubotDone)).to.be.undefined;
next.calledWith(hubotDone).should.be.true;
context.response.reply.args.should.eql([[errorMessage]]);
logger.error.args.should.eql([[null, errorMessage]]);
});
});
});