forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks.routes.test.js
More file actions
177 lines (147 loc) · 5.81 KB
/
Copy pathwebhooks.routes.test.js
File metadata and controls
177 lines (147 loc) · 5.81 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
'use strict';
const express = require('express');
const request = require('supertest');
const { createCacheMock } = require('./helpers/cacheMock');
const mockHelper = createCacheMock();
const { reset } = mockHelper;
jest.mock('../src/services/cache', () => mockHelper.cacheMock);
jest.mock('../src/logger', () => ({
info: jest.fn(), warn: jest.fn(), error: jest.fn(), debug: jest.fn(),
}));
const mockAxiosPost = jest.fn();
jest.mock('axios', () => ({ post: (...args) => mockAxiosPost(...args) }));
const webhooksRouter = require('../src/routes/webhooks');
function buildApp() {
const app = express();
app.use(express.json());
app.use('/api/v1', webhooksRouter);
return app;
}
beforeEach(() => {
reset();
mockAxiosPost.mockReset();
});
describe('POST /api/v1/webhooks', () => {
const app = buildApp();
test('creates a webhook with valid input', async () => {
const res = await request(app)
.post('/api/v1/webhooks')
.send({
url: 'https://example.com/hook',
events: ['pool.assets_locked'],
secret: 'whsec_user_supplied_secret_long_enough',
});
expect(res.status).toBe(201);
expect(res.body.id).toMatch(/^wh_/);
expect(res.body.events).toEqual(['pool.assets_locked']);
expect(res.body.secret).toBe('whsec_user_supplied_secret_long_enough');
expect(res.body.secret_warning).toMatch(/Store this secret/);
});
test('generates a secret when none provided', async () => {
const res = await request(app)
.post('/api/v1/webhooks')
.send({ url: 'https://example.com/hook', events: ['*'] });
expect(res.status).toBe(201);
expect(res.body.secret).toMatch(/^whsec_[0-9a-f]+$/);
});
test('rejects invalid url', async () => {
const res = await request(app)
.post('/api/v1/webhooks')
.send({ url: 'not-a-url', events: ['*'] });
expect(res.status).toBe(400);
});
test('rejects unknown event types', async () => {
const res = await request(app)
.post('/api/v1/webhooks')
.send({ url: 'https://example.com/hook', events: ['totally.fake'] });
expect(res.status).toBe(400);
});
test('rejects too-short secret', async () => {
const res = await request(app)
.post('/api/v1/webhooks')
.send({ url: 'https://example.com/hook', events: ['*'], secret: 'short' });
expect(res.status).toBe(400);
});
});
describe('GET /api/v1/webhooks', () => {
const app = buildApp();
test('lists registered webhooks without leaking full secrets', async () => {
await request(app).post('/api/v1/webhooks').send({
url: 'https://a.com', events: ['*'], secret: 'whsec_aaaaaaaaaaaaaaaa',
});
const res = await request(app).get('/api/v1/webhooks');
expect(res.status).toBe(200);
expect(res.body.webhooks).toHaveLength(1);
expect(res.body.webhooks[0].secret_preview).toMatch(/^whsec_/);
expect(res.body.webhooks[0]).not.toHaveProperty('secret');
});
});
describe('GET /api/v1/webhooks/:id', () => {
const app = buildApp();
test('returns 404 for unknown webhook', async () => {
const res = await request(app).get('/api/v1/webhooks/wh_nope');
expect(res.status).toBe(404);
});
});
describe('DELETE /api/v1/webhooks/:id', () => {
const app = buildApp();
test('deletes a registered webhook', async () => {
const created = await request(app).post('/api/v1/webhooks').send({
url: 'https://example.com/hook', events: ['*'], secret: 'whsec_aaaaaaaaaaaaaaaa',
});
const del = await request(app).delete(`/api/v1/webhooks/${created.body.id}`);
expect(del.status).toBe(200);
expect(del.body.deleted).toBe(true);
const list = await request(app).get('/api/v1/webhooks');
expect(list.body.webhooks).toHaveLength(0);
});
test('returns 404 when deleting unknown id', async () => {
const res = await request(app).delete('/api/v1/webhooks/wh_nope');
expect(res.status).toBe(404);
});
});
describe('PATCH /api/v1/webhooks/:id', () => {
const app = buildApp();
test('updates active status', async () => {
const created = await request(app).post('/api/v1/webhooks').send({
url: 'https://example.com/hook', events: ['*'], secret: 'whsec_aaaaaaaaaaaaaaaa',
});
const res = await request(app)
.patch(`/api/v1/webhooks/${created.body.id}`)
.send({ active: false });
expect(res.status).toBe(200);
expect(res.body.active).toBe(false);
});
});
describe('POST /api/v1/webhooks/:id/test', () => {
const app = buildApp();
test('sends a test delivery and returns delivery summary', async () => {
const created = await request(app).post('/api/v1/webhooks').send({
url: 'https://example.com/hook', events: ['*'], secret: 'whsec_aaaaaaaaaaaaaaaa',
});
mockAxiosPost.mockResolvedValueOnce({ status: 200 });
const res = await request(app).post(`/api/v1/webhooks/${created.body.id}/test`);
expect(res.status).toBe(202);
expect(res.body.delivery_id).toMatch(/^dlv_/);
expect(res.body.status).toBe('success');
expect(mockAxiosPost).toHaveBeenCalledTimes(1);
});
test('returns 404 when webhook does not exist', async () => {
const res = await request(app).post('/api/v1/webhooks/wh_nope/test');
expect(res.status).toBe(404);
});
});
describe('GET /api/v1/webhooks/:id/deliveries', () => {
const app = buildApp();
test('lists deliveries for a webhook', async () => {
const created = await request(app).post('/api/v1/webhooks').send({
url: 'https://example.com/hook', events: ['*'], secret: 'whsec_aaaaaaaaaaaaaaaa',
});
mockAxiosPost.mockResolvedValue({ status: 200 });
await request(app).post(`/api/v1/webhooks/${created.body.id}/test`);
const res = await request(app).get(`/api/v1/webhooks/${created.body.id}/deliveries`);
expect(res.status).toBe(200);
expect(Array.isArray(res.body.deliveries)).toBe(true);
expect(res.body.deliveries.length).toBeGreaterThan(0);
});
});