forked from Philmod/google-cloud-build-slack
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
234 lines (216 loc) · 7.11 KB
/
test.js
File metadata and controls
234 lines (216 loc) · 7.11 KB
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
'use strict';
const fs = require('fs');
const should = require('should');
const async = require('async');
const lib = require('./index.js');
const base64Build = 'eyJpZCI6IjE3NDBjZTJhLTYxZDktNGE1OC1iM2M3LWNmYWQ5OWRiOGQwYSIsInByb2plY3RJZCI6Im5vZGUtZXhhbXBsZS1na2UiLCJzdGF0dXMiOiJTVUNDRVNTIiwic291cmNlIjp7InJlcG9Tb3VyY2UiOnsicHJvamVjdElkIjoibm9kZS1leGFtcGxlLWdrZSIsInJlcG9OYW1lIjoibm9kZS1leGFtcGxlLWZyb250ZW5kIiwiYnJhbmNoTmFtZSI6Im1hc3RlciJ9fSwic3RlcHMiOlt7Im5hbWUiOiJnY3IuaW8vY2xvdWQtYnVpbGRlcnMvZG9ja2VyIiwiYXJncyI6WyJidWlsZCIsIi10IiwiZ2NyLmlvL25vZGUtZXhhbXBsZS1na2Uv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MzJiLWFlOGYtYzQ0MGUwY2M0MDg5IiwibG9nVXJsIjoiaHR0cHM6Ly9jb25zb2xlLmRldmVsb3BlcnMuZ29vZ2xlLmNvbS9sb2dzL3ZpZXdlcj9wcm9qZWN0PW5vZGUtZXhhbXBsZS1na2VcdTAwMjZyZXNvdXJjZS5sYWJlbHMuYnVpbGRfaWQ9MTc0MGNlMmEtNjFkOS00YTU4LWIzYzctY2ZhZDk5ZGI4ZDBhIn0=';
const nbCommonFields = 2;
const MS_PER_MINUTE = 60000;
describe('eventToBuild', () => {
it('should transform a base64 build to an object', () => {
let build = lib.eventToBuild(base64Build);
should.exist(build.projectId);
build.projectId.should.equal('node-example-gke');
});
});
describe('createSlackMessage', () => {
it('should create a slack message', () => {
let build = {
id: 'build-id',
logUrl: 'https://logurl.com',
status: 'SUCCESS',
finishTime: '2017-03-19T00:08:12.220502Z',
};
let message = lib.createSlackMessage(build);
message.text.should.equal("Build `build-id` finished");
should.exist(message.attachments);
message.attachments.should.have.length(1);
let attachment = message.attachments[0];
attachment.title_link.should.equal(build.logUrl);
attachment.ts.should.equal(1489882092);
attachment.fields.should.have.length(nbCommonFields);
attachment.fields[0].value.should.equal(build.status);
});
it('should include the build duration as a field', () => {
let now = Date.now();
let deltaInMinutes = 11;
let build = {
id: 'build-id',
logUrl: 'https://logurl.com',
status: 'SUCCESS',
startTime: new Date(now - deltaInMinutes*MS_PER_MINUTE),
finishTime: now,
};
let message = lib.createSlackMessage(build);
let attachment = message.attachments[0];
attachment.fields[1].value.should.equal(deltaInMinutes + ' minutes');
});
it('should create a slack message with images', () => {
let build = {
id: 'build-id',
logUrl: 'https://logurl.com',
status: 'SUCCESS',
finishTime: Date.now(),
images: ['image-1', 'image-2'],
};
let message = lib.createSlackMessage(build);
let attachment = message.attachments[0];
attachment.fields.should.have.length(nbCommonFields + build.images.length);
attachment.fields[nbCommonFields].value.should.equal('image-1');
attachment.fields[nbCommonFields+1].value.should.equal('image-2');
});
it('should use the right color depending on the status', () => {
let build = {
id: 'build-id',
finishTime: Date.now(),
};
let testCases = [
{
status: 'QUEUED',
want: '#4285F4',
},
{
status: 'WORKING',
want: '#4285F4',
},
{
status: 'SUCCESS',
want: '#34A853',
},
{
status: 'FAILURE',
want: '#EA4335',
},
{
status: 'INTERNAL_ERROR',
want: '#EA4335',
},
{
status: 'TIMEOUT',
want: '#FBBC05',
},
];
testCases.forEach(function(tc) {
build.status = tc.status;
let message = lib.createSlackMessage(build);
message.attachments[0].color.should.equal(tc.want, tc.status);
});
});
});
function cleanConfig(callback) {
let config = {
GITHUB_ACCESS_TOKEN: 'myaccesstoken',
}
fs.writeFile('config.json', JSON.stringify(config), 'utf8', callback);
}
describe('subscribe', () => {
beforeEach(cleanConfig);
afterEach(cleanConfig);
beforeEach(() => {
this.webhookCalled = false;
lib.webhook.send = (message, callback) => {
this.webhookCalled = true;
callback()
}
});
it('should subscribe to pubsub message and send a slack message', (done) => {
let event = {
data: {
data: base64Build
}
};
lib.subscribe(event, () => {
this.webhookCalled.should.be.true();
done();
});
});
it('should not send a message for non final status (by default)', (done) => {
let testCases = [
{
status: 'QUEUED',
want: false,
},
{
status: 'WORKING',
want: false,
},
{
status: 'SUCCESS',
want: true,
},
{
status: 'FAILURE',
want: true,
},
{
status: 'INTERNAL_ERROR',
want: true,
},
{
status: 'TIMEOUT',
want: true,
},
];
async.forEach(testCases, (tc, doneEach) => {
this.webhookCalled = false;
let event = {
data: {
data: new Buffer(JSON.stringify({
status: tc.status,
})).toString('base64')
}
};
lib.subscribe(event, () => {
this.webhookCalled.should.equal(tc.want);
doneEach();
});
}, done);
});
it('should a message only for specified status', (done) => {
lib.status = ['FAILURE', 'INTERNAL_ERROR'];
let testCases = [
{
status: 'QUEUED',
want: false,
},
{
status: 'WORKING',
want: false,
},
{
status: 'SUCCESS',
want: false,
},
{
status: 'FAILURE',
want: true,
},
{
status: 'INTERNAL_ERROR',
want: true,
},
{
status: 'TIMEOUT',
want: false,
},
];
async.forEach(testCases, (tc, doneEach) => {
this.webhookCalled = false;
let event = {
data: {
data: new Buffer(JSON.stringify({
status: tc.status,
})).toString('base64')
}
};
lib.subscribe(event, () => {
this.webhookCalled.should.equal(tc.want, tc.status);
doneEach();
});
}, function() {
// clean the status list.
lib.GC_GITHUB_STATUS = null;
done();
});
});
});