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
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { verifyHCaptcha } from '../src/integrations/hcaptcha.js';
import { sendNtfyNotification } from '../src/integrations/ntfy.js';
import type { 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');
});
});
|