-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest_webhooks.js
126 lines (106 loc) · 4.46 KB
/
test_webhooks.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
'use strict';
/*
Used to test the gitlab webhooks
*/
const expect = require('expect.js');
const webhooks = require('../lib/webhooks');
let util = require('util');
let debug = require('debug')('strider-gitlab:test:webhooks');
describe('receiving webhooks from the gitlab server', function() {
//--------------------------------------------------------------------------------------
describe('receiveWebhook', function() {
//It takes three parameters, emitter, req and res
it('should emit a job.prepare event on emitter AND return a 204 response if the payload was parsed successfully and a job could be created', function(done) {
let expected_successes_remaining = 2;
//the test should pass only when mockRes.sendStatus gets called with 204 AND when
//emitter receives this signal with the jobObject, doing assertion counting to
//ensure both conditions are successful. TODO: Is there a better way to do this with Mocha?
function checkDone() {
expected_successes_remaining--;
if(expected_successes_remaining === 0) {
done();
}
}
//This will get the job.prepare event when the job is successfully created
const emitter = new (require('events').EventEmitter);
//This contains the payload received from the gitlab server in
//body and project information from strider
const mockReq = require('./mocks/receive_webhooks_req.js');
//On receiving the payload we are expected to send a 204 immediately (?)
//perhaps we should send a 400 if we could not parse the payload
const mockRes = {
sendStatus: function(code) {
expect(code).to.eql(204);
checkDone();
}
};
emitter.on('job.prepare', function(jobObject) {
expect(jobObject.type).to.eql("TEST_AND_DEPLOY");
expect(jobObject.trigger.type).to.eql("commit");
expect(jobObject.ref.branch).to.eql("master");
expect(jobObject.ref.id).to.eql("6a00c57e69bd4e269496ca27973191ecafcf8b20");
checkDone();
});
webhooks.receiveWebhook(emitter, mockReq, mockRes);
});
it('should send a 400 error in the res if the payload cannot be parsed');
});
//--------------------------------------------------------------------------------------
describe('pushJob', function() {
//pushJob takes one parameter - a payload object
it('should be able to parse the payload and return an object with branch, trigger, deploy and ref', function() {
const samplePayload = require('./mocks/sample_payload.js');
const config = webhooks.pushJob(samplePayload);
expect(config).to.eql({
branch: 'master',
trigger: {
type: 'commit',
author: {
name: 'Strider Tester',
username: undefined,
email: '[email protected]',
image: 'https://s.gravatar.com/avatar/3f671ed86ed3d21ed3640c7a016b0997'
},
url: 'http://nodev/stridertester/privproject1/commit/352e6fe2ea42d394a21dc7995df2116e86bb0684',
message: 'updated strider.json\n',
timestamp: '2015-08-26T20:02:07+09:00',
source: {type: 'plugin', plugin: 'gitlab'}
},
deploy: true,
ref: {
branch: 'master',
id: '352e6fe2ea42d394a21dc7995df2116e86bb0684'
}
});
});
});
//--------------------------------------------------------------------------------------
describe('pushJob', function() {
//pushJob takes one parameter - a payload object
it('should be able to parse the payload and return an object with branch, trigger, deploy and ref by gitlab v7', function() {
const samplePayload = require('./mocks/sample_payload_tag_push_with_v7.x.js');
const config = webhooks.pushJob(samplePayload);
expect(config).to.eql({
branch: 'tag-1.1.2',
trigger: {
type: 'tag push',
author: {
name: 'Strider Tester',
username: undefined,
email: undefined,
image: 'https://s.gravatar.com/avatar/d415f0e30c471dfdd9bc4f827329ef48'
},
url: 'http://nodev/stridertester/privproject1/commit/352e6fe2ea42d394a21dc7995df2116e86bb0684',
message: 'tag push',
timestamp: config.trigger.timestamp,
source: { type: 'plugin', plugin: 'gitlab' }
},
deploy: true,
ref: {
branch: 'tag-1.1.2',
id: '352e6fe2ea42d394a21dc7995df2116e86bb0684'
}
});
});
});
});