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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { verifyHCaptcha } from '../src/integrations/hcaptcha.js';
import { sendNtfyNotification } from '../src/integrations/ntfy.js';
import { sendEmailNotification } from '../src/integrations/email.js';
import type { EmailConfig, NtfyConfig, StoredRequest } from '../src/types/index.js';
describe('verifyHCaptcha', () => {
beforeEach(() => {
vi.stubGlobal('fetch', vi.fn());
});
afterEach(() => {
vi.unstubAllGlobals();
});
it('returns success boolean when response is ok', async () => {
const fetchMock = vi.mocked(fetch);
fetchMock.mockResolvedValue({
ok: true,
statusText: 'OK',
json: async () => ({ success: true }),
} as Response);
const result = await verifyHCaptcha('token', 'secret');
expect(result.left().present()).toBe(false);
expect(result.right().get()).toBe(true);
expect(fetchMock).toHaveBeenCalledTimes(1);
const [url, init] = fetchMock.mock.calls[0]!;
expect(url).toBe('https://hcaptcha.com/siteverify');
expect(init?.method).toBe('POST');
expect(init?.headers).toEqual({ 'Content-Type': 'application/x-www-form-urlencoded' });
expect(init?.body).toBeInstanceOf(URLSearchParams);
expect((init?.body as URLSearchParams).get('secret')).toBe('secret');
expect((init?.body as URLSearchParams).get('response')).toBe('token');
});
it('returns error when response is not ok', async () => {
const fetchMock = vi.mocked(fetch);
fetchMock.mockResolvedValue({
ok: false,
statusText: 'Bad Request',
} as Response);
const result = await verifyHCaptcha('token', 'secret');
expect(result.left().present()).toBe(true);
expect(result.left().get().message).toBe('hCaptcha verification failed: Bad Request');
});
});
describe('sendNtfyNotification', () => {
beforeEach(() => {
vi.stubGlobal('fetch', vi.fn());
});
afterEach(() => {
vi.unstubAllGlobals();
});
it('is a no-op when not enabled or misconfigured', async () => {
const fetchMock = vi.mocked(fetch);
const config: NtfyConfig = { enabled: false };
const request: StoredRequest = {
timestamp: 1,
uuid: 'uuid',
routeName: 'route1',
method: 'POST',
headers: {},
body: {},
};
const result = await sendNtfyNotification(config, request);
expect(result.left().present()).toBe(false);
expect(fetchMock).not.toHaveBeenCalled();
});
it('posts a notification to the configured server/topic', async () => {
const fetchMock = vi.mocked(fetch);
fetchMock.mockResolvedValue({ ok: true, statusText: 'OK' } as Response);
const config: NtfyConfig = { enabled: true, server: 'https://ntfy.example.com', topic: 'topic1' };
const request: StoredRequest = {
timestamp: Date.parse('2020-01-01T00:00:00.000Z'),
uuid: 'uuid',
routeName: 'route1',
method: 'POST',
headers: {},
body: {},
};
const result = await sendNtfyNotification(config, request);
expect(result.left().present()).toBe(false);
expect(fetchMock).toHaveBeenCalledTimes(1);
const [url, init] = fetchMock.mock.calls[0]!;
expect(url).toBe('https://ntfy.example.com/topic1');
expect(init?.method).toBe('POST');
expect(init?.headers).toEqual({
Title: 'Webhook received: route1',
Tags: 'webhook,posthook',
Priority: '3',
});
expect(init?.body).toContain('Method: POST');
expect(init?.body).toContain('Timestamp: 2020-01-01T00:00:00.000Z');
expect(init?.body).toContain('UUID: uuid');
});
it('returns an error when ntfy responds with non-2xx', async () => {
const fetchMock = vi.mocked(fetch);
fetchMock.mockResolvedValue({ ok: false, statusText: 'Unauthorized' } as Response);
const config: NtfyConfig = { enabled: true, server: 'https://ntfy.example.com', topic: 'topic1' };
const request: StoredRequest = {
timestamp: 1,
uuid: 'uuid',
routeName: 'route1',
method: 'POST',
headers: {},
body: {},
};
const result = await sendNtfyNotification(config, request);
expect(result.left().present()).toBe(true);
expect(result.left().get().message).toBe('ntfy notification failed: Unauthorized');
});
});
describe('sendEmailNotification', () => {
beforeEach(() => {
vi.mock('nodemailer', () => ({
default: {
createTransport: vi.fn(() => ({
sendMail: vi.fn().mockResolvedValue(undefined),
})),
},
}));
});
afterEach(() => {
vi.clearAllMocks();
});
it('is a no-op when not enabled or misconfigured', async () => {
const { default: nodemailer } = await import('nodemailer');
const config: EmailConfig = { enabled: false };
const request: StoredRequest = {
timestamp: 1,
uuid: 'uuid',
routeName: 'route1',
method: 'POST',
headers: {},
body: {},
};
const result = await sendEmailNotification(config, request);
expect(result.left().present()).toBe(false);
expect(nodemailer.createTransport).not.toHaveBeenCalled();
});
it('sends an email with the configured settings', async () => {
const { default: nodemailer } = await import('nodemailer');
const sendMailMock = vi.fn().mockResolvedValue(undefined);
vi.mocked(nodemailer.createTransport).mockReturnValue({
sendMail: sendMailMock,
} as any);
const config: EmailConfig = {
enabled: true,
to: 'admin@example.com',
from: 'webhook@example.com',
host: 'smtp.example.com',
port: 587,
secure: true,
username: 'user',
password: 'pass',
subject: 'Test Subject',
includeBody: true,
includeHeaders: false,
};
const request: StoredRequest = {
timestamp: Date.parse('2020-01-01T00:00:00.000Z'),
uuid: 'test-uuid',
routeName: 'test-route',
method: 'POST',
headers: { 'content-type': 'application/json' },
body: { test: 'data' },
};
const result = await sendEmailNotification(config, request);
expect(result.left().present()).toBe(false);
expect(nodemailer.createTransport).toHaveBeenCalledWith({
host: 'smtp.example.com',
port: 587,
secure: true,
auth: {
user: 'user',
pass: 'pass',
},
});
expect(sendMailMock).toHaveBeenCalledTimes(1);
const mailOptions = sendMailMock.mock.calls[0][0];
expect(mailOptions.from).toBe('webhook@example.com');
expect(mailOptions.to).toBe('admin@example.com');
expect(mailOptions.subject).toBe('Test Subject');
expect(mailOptions.html).toContain('test-route');
expect(mailOptions.html).toContain('POST');
expect(mailOptions.html).toContain('2020-01-01T00:00:00.000Z');
expect(mailOptions.html).toContain('test-uuid');
expect(mailOptions.html).toContain('"test": "data"');
});
it('uses default subject when not configured', async () => {
const { default: nodemailer } = await import('nodemailer');
const sendMailMock = vi.fn().mockResolvedValue(undefined);
vi.mocked(nodemailer.createTransport).mockReturnValue({
sendMail: sendMailMock,
} as any);
const config: EmailConfig = {
enabled: true,
to: 'admin@example.com',
from: 'webhook@example.com',
};
const request: StoredRequest = {
timestamp: 1,
uuid: 'uuid',
routeName: 'my-route',
method: 'POST',
headers: {},
body: {},
};
await sendEmailNotification(config, request);
const mailOptions = sendMailMock.mock.calls[0][0];
expect(mailOptions.subject).toBe('Webhook received: my-route');
});
it('returns an error when email sending fails', async () => {
const { default: nodemailer } = await import('nodemailer');
const sendMailMock = vi.fn().mockRejectedValue(new Error('SMTP error'));
vi.mocked(nodemailer.createTransport).mockReturnValue({
sendMail: sendMailMock,
} as any);
const config: EmailConfig = {
enabled: true,
to: 'admin@example.com',
from: 'webhook@example.com',
};
const request: StoredRequest = {
timestamp: 1,
uuid: 'uuid',
routeName: 'route1',
method: 'POST',
headers: {},
body: {},
};
const result = await sendEmailNotification(config, request);
expect(result.left().present()).toBe(true);
expect(result.left().get().message).toBe('SMTP error');
});
});
|