forked from CurrentDao-org/CurrentDao-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook.service.spec.ts
More file actions
198 lines (174 loc) · 6.31 KB
/
webhook.service.spec.ts
File metadata and controls
198 lines (174 loc) · 6.31 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { HttpModule } from '@nestjs/axios';
import { ScheduleModule } from '@nestjs/schedule';
import { Repository } from 'typeorm';
import { WebhookService } from './webhook.service';
import { Webhook } from './entities/webhook.entity';
import { WebhookDelivery, DeliveryStatus } from './entities/webhook-delivery.entity';
import { HmacAuthService } from './auth/hmac.auth';
import { EventFilterService } from './filters/event.filter';
import { CreateWebhookDto, TriggerWebhookDto } from './dto/webhook.dto';
describe('WebhookService', () => {
let service: WebhookService;
let webhookRepository: Repository<Webhook>;
let deliveryRepository: Repository<WebhookDelivery>;
let hmacAuthService: HmacAuthService;
let eventFilterService: EventFilterService;
const mockWebhookRepository = {
create: jest.fn(),
save: jest.fn(),
find: jest.fn(),
findOne: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
increment: jest.fn(),
};
const mockDeliveryRepository = {
create: jest.fn(),
save: jest.fn(),
find: jest.fn(),
findOne: jest.fn(),
update: jest.fn(),
count: jest.fn(),
};
const mockHmacAuthService = {
generateSignature: jest.fn(),
verifySignature: jest.fn(),
signWebhook: jest.fn(),
generateTimestamp: jest.fn(() => Date.now()),
verifyTimestamp: jest.fn(() => true),
};
const mockEventFilterService = {
matchesFilters: jest.fn(),
matchesFilter: jest.fn(),
matchesObjectFilter: jest.fn(),
filterByEventType: jest.fn(),
filterByTimeRange: jest.fn(),
filterByAmount: jest.fn(),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [HttpModule, ScheduleModule.forRoot()],
providers: [
WebhookService,
HmacAuthService,
EventFilterService,
{
provide: getRepositoryToken(Webhook),
useValue: mockWebhookRepository,
},
{
provide: getRepositoryToken(WebhookDelivery),
useValue: mockDeliveryRepository,
},
],
})
.overrideProvider(HmacAuthService)
.useValue(mockHmacAuthService)
.overrideProvider(EventFilterService)
.useValue(mockEventFilterService)
.compile();
service = module.get<WebhookService>(WebhookService);
webhookRepository = module.get<Repository<Webhook>>(getRepositoryToken(Webhook));
deliveryRepository = module.get<Repository<WebhookDelivery>>(getRepositoryToken(WebhookDelivery));
hmacAuthService = module.get<HmacAuthService>(HmacAuthService);
eventFilterService = module.get<EventFilterService>(EventFilterService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
describe('create', () => {
it('should create a webhook', async () => {
const createWebhookDto: CreateWebhookDto = {
url: 'https://example.com/webhook',
secret: 'test-secret',
events: ['transaction.created'],
active: true,
maxRetries: 3,
timeoutMs: 5000,
filters: {},
};
const expectedWebhook = { id: '1', ...createWebhookDto };
mockWebhookRepository.create.mockReturnValue(expectedWebhook);
mockWebhookRepository.save.mockResolvedValue(expectedWebhook);
const result = await service.create(createWebhookDto);
expect(mockWebhookRepository.create).toHaveBeenCalledWith(createWebhookDto);
expect(mockWebhookRepository.save).toHaveBeenCalledWith(expectedWebhook);
expect(result).toEqual(expectedWebhook);
});
});
describe('triggerWebhook', () => {
it('should trigger webhooks for matching events', async () => {
const triggerDto: TriggerWebhookDto = {
eventType: 'transaction.created',
data: { amount: 100, currency: 'USD' },
transactionId: 'tx123',
};
const webhooks = [
{
id: '1',
url: 'https://example.com/webhook1',
events: ['transaction.created'],
active: true,
filters: {},
},
{
id: '2',
url: 'https://example.com/webhook2',
events: ['payment.completed'],
active: true,
filters: {},
},
];
mockWebhookRepository.find.mockResolvedValue(webhooks);
mockEventFilterService.matchesFilters.mockReturnValue(true);
mockDeliveryRepository.create.mockReturnValue({ id: 'delivery1' });
mockDeliveryRepository.save.mockResolvedValue({ id: 'delivery1' });
await service.triggerWebhook(triggerDto);
expect(mockWebhookRepository.find).toHaveBeenCalledWith({ where: { active: true } });
expect(mockEventFilterService.matchesFilters).toHaveBeenCalledTimes(1);
expect(mockDeliveryRepository.create).toHaveBeenCalledTimes(1);
expect(mockDeliveryRepository.save).toHaveBeenCalledTimes(1);
});
});
describe('getDeliveryStats', () => {
it('should return delivery statistics', async () => {
const mockStats = {
total: 100,
success: 95,
failed: 3,
pending: 2,
successRate: 95,
};
mockDeliveryRepository.count
.mockResolvedValueOnce(100)
.mockResolvedValueOnce(95)
.mockResolvedValueOnce(3)
.mockResolvedValueOnce(2);
const result = await service.getDeliveryStats();
expect(result).toEqual(mockStats);
expect(mockDeliveryRepository.count).toHaveBeenCalledTimes(4);
});
});
describe('calculateBackoffDelay', () => {
it('should calculate exponential backoff with jitter', async () => {
const service = new WebhookService(
mockWebhookRepository as any,
mockDeliveryRepository as any,
mockHmacAuthService,
mockEventFilterService as unknown as EventFilterService,
null as any,
);
const delay1 = (service as any).calculateBackoffDelay(1);
const delay2 = (service as any).calculateBackoffDelay(2);
const delay3 = (service as any).calculateBackoffDelay(3);
expect(delay1).toBeGreaterThanOrEqual(1000);
expect(delay1).toBeLessThan(2000);
expect(delay2).toBeGreaterThanOrEqual(2000);
expect(delay2).toBeLessThan(3000);
expect(delay3).toBeGreaterThanOrEqual(4000);
expect(delay3).toBeLessThan(5000);
});
});
});