|
| 1 | +/* |
| 2 | + Used to test the gitlab webhooks |
| 3 | + */ |
| 4 | + |
| 5 | +var expect = require('expect.js'); |
| 6 | +var webhooks = require('../lib/webhooks'); |
| 7 | +var util = require('util'); |
| 8 | +var debug = require('debug')('strider-gitlab:test:webhooks'); |
| 9 | + |
| 10 | +describe('receiving webhooks from the gitlab server', function () { |
| 11 | + //-------------------------------------------------------------------------------------- |
| 12 | + describe('receiveWebhook', function () { |
| 13 | + //It takes three parameters, emitter, req and res |
| 14 | + |
| 15 | + 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) { |
| 16 | + var expected_successes_remaining = 2; |
| 17 | + |
| 18 | + //the test should pass only when mockRes.sendStatus gets called with 204 AND when |
| 19 | + //emitter receives this signal with the jobObject, doing assertion counting to |
| 20 | + //ensure both conditions are successful. TODO: Is there a better way to do this with Mocha? |
| 21 | + |
| 22 | + function checkDone() { |
| 23 | + expected_successes_remaining--; |
| 24 | + if (expected_successes_remaining === 0) { |
| 25 | + done(); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + //This will get the job.prepare event when the job is successfully created |
| 30 | + var emitter = new (require('events').EventEmitter); |
| 31 | + |
| 32 | + //This contains the payload received from the gitlab server in |
| 33 | + //body and project information from strider |
| 34 | + var mockReq = require('./mocks/receive_webhooks_req.js'); |
| 35 | + |
| 36 | + //On receiving the payload we are expected to send a 204 immediately (?) |
| 37 | + //perhaps we should send a 400 if we could not parse the payload |
| 38 | + var mockRes = { |
| 39 | + sendStatus: function (code) { |
| 40 | + expect(code).to.eql(204); |
| 41 | + checkDone(); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + emitter.on('job.prepare', function (jobObject) { |
| 46 | + expect(jobObject.type).to.eql("TEST_AND_DEPLOY"); |
| 47 | + expect(jobObject.trigger.type).to.eql("commit"); |
| 48 | + expect(jobObject.ref.branch).to.eql("master"); |
| 49 | + expect(jobObject.ref.id).to.eql("6a00c57e69bd4e269496ca27973191ecafcf8b20"); |
| 50 | + checkDone(); |
| 51 | + }); |
| 52 | + |
| 53 | + webhooks.receiveWebhook(emitter, mockReq, mockRes); |
| 54 | + |
| 55 | + }); |
| 56 | + |
| 57 | + it('should send a 400 error in the res if the payload cannot be parsed'); |
| 58 | + |
| 59 | + }); |
| 60 | + |
| 61 | + //-------------------------------------------------------------------------------------- |
| 62 | + describe('pushJob', function () { |
| 63 | + //pushJob takes one parameter - a payload object |
| 64 | + |
| 65 | + it('should be able to parse the payload and return an object with branch, trigger, deploy and ref', function () { |
| 66 | + var samplePayload = require('./mocks/sample_payload.js'); |
| 67 | + var config = webhooks.pushJob(samplePayload); |
| 68 | + expect(config).to.eql({ |
| 69 | + branch: 'master', |
| 70 | + trigger: { |
| 71 | + type: 'commit', |
| 72 | + author: { |
| 73 | + name: 'Strider Tester', |
| 74 | + username: undefined, |
| 75 | + |
| 76 | + image: 'https://s.gravatar.com/avatar/3f671ed86ed3d21ed3640c7a016b0997' |
| 77 | + }, |
| 78 | + url: 'http://nodev/stridertester/privproject1/commit/352e6fe2ea42d394a21dc7995df2116e86bb0684', |
| 79 | + message: 'updated strider.json\n', |
| 80 | + timestamp: '2015-08-26T20:02:07+09:00', |
| 81 | + source: {type: 'plugin', plugin: 'gitlab'} |
| 82 | + }, |
| 83 | + deploy: true, |
| 84 | + ref: { |
| 85 | + branch: 'master', |
| 86 | + id: '352e6fe2ea42d394a21dc7995df2116e86bb0684' |
| 87 | + } |
| 88 | + }); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | +}); |
0 commit comments