Skip to content

Commit c7782fc

Browse files
committed
webhook tests
1 parent 82387e8 commit c7782fc

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

src/controllers/webhook.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { chain } from '~/helpers/promise';
22
import { githubWebhook } from '~/services/webhook';
33

4-
export const github = (info, params) => {
4+
export const github = (info) => {
55
// TODO: restrict to GitHub IPs only.
6+
// TODO. Restrict to exact URL to avoid duplicated requests with URL variants.
67
return chain
8+
.then(() => githubWebhook(info))
79
.then(() => {
8-
// A trivial way to avoid duplicates is to restrict the URL to a single one (no parameters).
9-
// GitHub already prevents duplicates.
10-
if (!(Object.keys(params).length === 0 && params.constructor === Object)) {
11-
throw 'GitHub webhooks URL can\'t have params.';
12-
}
13-
})
14-
.then(() => githubWebhook(info));
10+
return { message: 'OK' };
11+
});
1512
}

src/routes/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ router.delete('/messages/:_id', isAuthenticated, isMessageIdValid, isMessageOwne
9797
/**
9898
* Webhooks
9999
*/
100-
router.post('/webhooks/github', c(webhook.github, (req) => [req.body, req.query]));
100+
router.post('/webhooks/github', c(webhook.github, (req) => [req.body]));
101101

102102
/**
103103
* Default.

src/services/webhook.js

-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,5 @@ export function githubWebhook(info) {
2525
default: return; // Given action is not supported yet (exit without saving).
2626
}
2727

28-
console.info(webhook);
2928
return webhook.save();
3029
}

test/api/webhook.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { expect } from 'chai';
2+
3+
import Webhook from '~/models/Webhook';
4+
5+
describe('Webhook', () => {
6+
describe('POST /webhooks/github', () => {
7+
// it('should return an error if the url contains parameters', (done) => {
8+
// request
9+
// .post(`${server}/webhooks/github?`)
10+
// .send({ data: 'lots of github data' })
11+
// .end((err, res) => {
12+
// expect(res.status).to.equal(500);
13+
// expect(res.body.message).to.equal('Provided GitHub webhooks URL is invalid');
14+
// });
15+
// });
16+
17+
it('should create a Webhook entry if the request is valid (comment test)', (done) => {
18+
request
19+
.post(`${server}/webhooks/github`)
20+
.send({
21+
action: 'created',
22+
repository: { full_name: 'github/repo' },
23+
issue: {
24+
number: 1,
25+
title: 'issueTitle',
26+
user: { login: 'issueUser' },
27+
},
28+
comment: {
29+
id: 111,
30+
user: { login: 'commentUser' },
31+
body: 'body',
32+
},
33+
})
34+
.end((err, res) => {
35+
expect(res.status).to.equal(200);
36+
expect(res.body).to.eql({ message: 'OK' });
37+
Webhook.findOne({}).exec().then((webhook) => {
38+
expect(webhook.github.repository).to.equal('github/repo');
39+
expect(webhook.github.action).to.equal('created');
40+
done();
41+
});
42+
});
43+
});
44+
});
45+
});

0 commit comments

Comments
 (0)