UNPKG

6.39 kBJavaScriptView Raw
1var assert = require('assert');
2var sinon = require('sinon');
3var check = require('check-types');
4var wh = require('..').webhooks;
5
6describe('ac hipchat webhooks', function () {
7
8 describe('normalize', function () {
9
10 var baseUrl = 'http://test.com:1000/foo';
11 var defaultMountPath = '/ac-webhook';
12
13 it('should not accept a webhook definition with no event', function () {
14 try {
15 var webhook = wh.normalize({}, baseUrl, defaultMountPath);
16 } catch (e) {
17 assert.ok(e);
18 return;
19 }
20 assert.fail('no error', 'error', 'equal');
21 });
22
23 it('should not accept a webhook definition with an unrecognized event', function () {
24 try {
25 var webhook = wh.normalize({
26 event: 'fail'
27 }, baseUrl, defaultMountPath);
28 } catch (e) {
29 assert.ok(e);
30 return;
31 }
32 assert.fail('no error', 'error', 'equal');
33 });
34
35 it('should not accept a webhook definition with event=room_message lacking a pattern', function () {
36 try {
37 var webhook = wh.normalize({
38 event: 'room_message'
39 }, baseUrl, defaultMountPath);
40 } catch (e) {
41 assert.ok(e);
42 return;
43 }
44 assert.fail('no error', 'error', 'equal');
45 });
46
47 it('should accept a webhook definition with event=room_message with a string pattern', function () {
48 var webhook = wh.normalize({
49 event: 'room_message',
50 pattern: '(?i)^/foo(?:\s(.*)|$)',
51 url: 'http://test.com:1000/foo/ac-webhook'
52 }, baseUrl, defaultMountPath);
53 assert.equal(webhook.event, 'room_message');
54 assert.equal(webhook.pattern, '(?i)^/foo(?:\s(.*)|$)');
55 assert.equal(webhook.url, 'http://test.com:1000/foo/ac-webhook?name=649cdac0ba07ecc9b42320ce23f55360223f04ae');
56 });
57
58 it('should accept a webhook definition with event=room_message with a regex pattern', function () {
59 var webhook = wh.normalize({
60 event: 'room_message',
61 pattern: /^\/foo(?:\s(.*)|$)/i,
62 url: 'http://test.com:1000/foo/ac-webhook'
63 }, baseUrl, defaultMountPath);
64 assert.equal(webhook.event, 'room_message');
65 assert.equal(webhook.pattern, '(?i)^\\/foo(?:\\s(.*)|$)');
66 assert.equal(webhook.url, 'http://test.com:1000/foo/ac-webhook?name=6f0ce5209ca96a4c726ff5830d56ce353b9131f5');
67 });
68
69 it('should accept a webhook definition with a full url', function () {
70 var webhook = wh.normalize({
71 event: 'room_enter',
72 url: 'http://test.com:1000/foo/ac-webhook'
73 }, baseUrl, defaultMountPath);
74 assert.equal(webhook.event, 'room_enter');
75 assert.equal(webhook.url, 'http://test.com:1000/foo/ac-webhook?name=2c8bf398f560e208070d3860ed30c6fb7fa0c4f5');
76 });
77
78 it('should complete a webhook definition with a relative url', function () {
79 var webhook = wh.normalize({
80 event: 'room_enter',
81 url: '/foo/ac-webhook'
82 }, baseUrl, defaultMountPath);
83 assert.equal(webhook.event, 'room_enter');
84 assert.equal(webhook.url, 'http://test.com:1000/foo/ac-webhook?name=2c8bf398f560e208070d3860ed30c6fb7fa0c4f5');
85 });
86
87 it('should complete a webhook definition with no url', function () {
88 var webhook = wh.normalize({
89 event: 'room_enter'
90 }, baseUrl, defaultMountPath);
91 assert.equal(webhook.event, 'room_enter');
92 assert.equal(webhook.url, 'http://test.com:1000/ac-webhook?name=47523e29d46514060efdc0becdfdf3879d90d12e');
93 });
94
95 it('should complete a webhook definition with a bad url by fixing it', function () {
96 var webhook = wh.normalize({
97 event: 'room_enter',
98 url: 'https://wrong.com:2000/ac-webhook'
99 }, baseUrl, defaultMountPath);
100 assert.equal(webhook.event, 'room_enter');
101 assert.equal(webhook.url, 'http://test.com:1000/ac-webhook?name=47523e29d46514060efdc0becdfdf3879d90d12e');
102 });
103
104 it('should complete a webhook definition with no name with a stable, generated name', function () {
105 var webhook = wh.normalize({
106 event: 'room_message',
107 pattern: /^!foo\s+(.+)/
108 }, baseUrl, defaultMountPath);
109 assert.ok(check.string(webhook.name));
110 var name = webhook.name;
111 delete webhook.name;
112 webhook = wh.normalize(webhook, baseUrl, defaultMountPath);
113 assert.equal(name, webhook.name);
114 delete webhook.name;
115 webhook.pattern = /^!bar\s+(.+)/;
116 webhook = wh.normalize(webhook, baseUrl, defaultMountPath);
117 assert.ok(check.string(webhook.name));
118 assert.notEqual(name, webhook.name);
119 });
120
121 });
122
123 describe('parseUrl', function () {
124
125 it('should return mountPath and fullPath url elements for a full url', function () {
126 var parsed = wh.parseUrl('https://wrong.com:2000/addon/ac-webhook');
127 assert.equal(parsed.fullPath, '/addon/ac-webhook');
128 assert.equal(parsed.mountPath, '/addon/ac-webhook');
129 });
130
131 it('should return mountPath and fullPath url elements for a partial url', function () {
132 var parsed = wh.parseUrl('/addon/ac-webhook');
133 assert.equal(parsed.fullPath, '/addon/ac-webhook');
134 assert.equal(parsed.mountPath, '/addon/ac-webhook');
135 });
136
137 it('should return mountPath and fullPath url elements for a url with a query string', function () {
138 var parsed = wh.parseUrl('https://wrong.com:2000/addon/ac-webhook?foo=bar');
139 assert.equal(parsed.fullPath, '/addon/ac-webhook?foo=bar');
140 assert.equal(parsed.mountPath, '/addon/ac-webhook');
141 });
142
143 });
144
145 describe('regExpToPattern', function () {
146
147 it('should return a python-compatible regex string given a js regexp object', function () {
148 var pattern = wh.regExpToPattern(/^\/foo\u1FFF(?:\s(.*)|$)/gi);
149 assert.equal(pattern, '(?gi)^\\/foo\\x{1FFF}(?:\\s(.*)|$)');
150 });
151
152 });
153
154 describe('patternToRegExp', function () {
155
156 it('should return a javascript-compatible regex object given a python regexp string', function () {
157 var re = wh.patternToRegExp('(?i)^\\/foo\\x{1FFF}(?:\\s(.*)|$)');
158 assert.equal(re.toString(), /^\/foo\u1FFF(?:\s(.*)|$)/i.toString());
159 });
160
161 });
162
163 describe('digest', function () {
164
165 it('should digest a webhook definition', function () {
166 var digest = wh.digest({
167 event: 'room_message',
168 pattern: /^\/foo(?:\s(.*)|$)/i,
169 url: 'https://wrong.com:2000/ac-webhook'
170 });
171 });
172
173 });
174
175});