forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks.controller.spec.ts
More file actions
207 lines (168 loc) · 7.63 KB
/
Copy pathwebhooks.controller.spec.ts
File metadata and controls
207 lines (168 loc) · 7.63 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
199
200
201
202
203
204
205
206
207
import { Test, TestingModule } from '@nestjs/testing';
import { WebhooksController } from './webhooks.controller';
import { WebhooksService } from './webhooks.service';
import { WebhookDeliveryService } from './webhook-delivery.service';
import { WebhookPolicyService } from './webhook-policy.service';
import { TestWebhookDispatcher } from './test-webhook-dispatcher';
import { CreateWebhookDto } from './dto/create-webhook.dto';
import { UpdateWebhookDto } from './dto/update-webhook.dto';
import { TestWebhookDto } from './dto/test-webhook.dto';
import { WebhookEventType } from './webhook.entity';
describe('WebhooksController', () => {
let controller: WebhooksController;
let webhooksService: WebhooksService;
let deliveryService: WebhookDeliveryService;
let policyService: WebhookPolicyService;
let testDispatcher: TestWebhookDispatcher;
const mockWebhooksService = {
create: jest.fn(),
findAll: jest.fn(),
findOne: jest.fn(),
update: jest.fn(),
remove: jest.fn(),
findByUserId: jest.fn(),
};
const mockDeliveryService = {
getDeliveryLogs: jest.fn(),
getRecentDeliveries: jest.fn(),
getDeliveryStats: jest.fn(),
};
const mockPolicyService = {
assertOwnership: jest.fn(),
};
const mockTestDispatcher = {
dispatchTest: jest.fn(),
};
const mockUser = {
id: 'user-123',
walletAddress: 'WALLET123',
raw: {},
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [WebhooksController],
providers: [
{
provide: WebhooksService,
useValue: mockWebhooksService,
},
{ provide: WebhookPolicyService, useValue: mockPolicyService },
{
provide: WebhookDeliveryService,
useValue: mockDeliveryService,
},
{
provide: TestWebhookDispatcher,
useValue: mockTestDispatcher,
},
],
}).compile();
controller = module.get<WebhooksController>(WebhooksController);
webhooksService = module.get<WebhooksService>(WebhooksService);
deliveryService = module.get<WebhookDeliveryService>(WebhookDeliveryService);
policyService = module.get<WebhookPolicyService>(WebhookPolicyService);
testDispatcher = module.get<TestWebhookDispatcher>(TestWebhookDispatcher);
});
afterEach(() => {
jest.clearAllMocks();
});
describe('create', () => {
it('should create a webhook and set userId from auth', async () => {
const createDto: CreateWebhookDto = {
url: 'https://example.com/webhook',
events: [WebhookEventType.SPLIT_CREATED],
secret: 'test-secret',
};
const webhook = { id: 'webhook-123', userId: mockUser.id, ...createDto };
mockWebhooksService.create.mockResolvedValue(webhook);
const result = await controller.create(createDto, mockUser);
expect(createDto.userId).toBe(mockUser.id);
expect(mockWebhooksService.create).toHaveBeenCalledWith(createDto);
expect(result).toEqual(webhook);
});
});
describe('findAll', () => {
it('should return webhooks for the authenticated user', async () => {
const webhooks = [{ id: 'webhook-1', userId: mockUser.id }];
mockWebhooksService.findByUserId.mockResolvedValue(webhooks);
const result = await controller.findAll(mockUser);
expect(mockWebhooksService.findByUserId).toHaveBeenCalledWith(mockUser.id);
expect(result).toEqual(webhooks);
});
});
describe('findOne', () => {
it('should return a webhook by id and check ownership', async () => {
const webhook = { id: 'webhook-123', userId: mockUser.id };
mockWebhooksService.findOne.mockResolvedValue(webhook);
const result = await controller.findOne('webhook-123', mockUser);
expect(mockWebhooksService.findOne).toHaveBeenCalledWith('webhook-123');
expect(mockPolicyService.assertOwnership).toHaveBeenCalledWith(mockUser, webhook);
expect(result).toEqual(webhook);
});
});
describe('update', () => {
it('should update a webhook and check ownership', async () => {
const updateDto: UpdateWebhookDto = { url: 'https://newurl.com/webhook' };
const webhook = { id: 'webhook-123', userId: mockUser.id };
const updatedWebhook = { ...webhook, ...updateDto };
mockWebhooksService.findOne.mockResolvedValue(webhook);
mockWebhooksService.update.mockResolvedValue(updatedWebhook);
const result = await controller.update('webhook-123', updateDto, mockUser);
expect(mockWebhooksService.findOne).toHaveBeenCalledWith('webhook-123');
expect(mockPolicyService.assertOwnership).toHaveBeenCalledWith(mockUser, webhook);
expect(mockWebhooksService.update).toHaveBeenCalledWith('webhook-123', updateDto);
expect(result).toEqual(updatedWebhook);
});
});
describe('remove', () => {
it('should delete a webhook and check ownership', async () => {
const webhook = { id: 'webhook-123', userId: mockUser.id };
mockWebhooksService.findOne.mockResolvedValue(webhook);
mockWebhooksService.remove.mockResolvedValue(undefined);
await controller.remove('webhook-123', mockUser);
expect(mockWebhooksService.findOne).toHaveBeenCalledWith('webhook-123');
expect(mockPolicyService.assertOwnership).toHaveBeenCalledWith(mockUser, webhook);
expect(mockWebhooksService.remove).toHaveBeenCalledWith('webhook-123');
});
});
describe('testWebhook', () => {
it('should trigger a test webhook and check ownership', async () => {
const testDto: TestWebhookDto = {
eventType: WebhookEventType.SPLIT_CREATED,
payload: { test: true },
};
const webhook = { id: 'webhook-123', userId: mockUser.id };
mockWebhooksService.findOne.mockResolvedValue(webhook);
mockTestDispatcher.dispatchTest.mockResolvedValue(undefined);
const result = await controller.testWebhook('webhook-123', testDto, mockUser);
expect(mockWebhooksService.findOne).toHaveBeenCalledWith('webhook-123');
expect(mockPolicyService.assertOwnership).toHaveBeenCalledWith(mockUser, webhook);
expect(mockTestDispatcher.dispatchTest).toHaveBeenCalledWith(webhook, testDto.eventType, testDto.payload);
expect(result.message).toBe('Test webhook triggered');
});
});
describe('getDeliveries', () => {
it('should return delivery logs and check ownership', async () => {
const deliveries = [{ id: 'delivery-1' }];
const webhook = { id: 'webhook-123', userId: mockUser.id };
mockWebhooksService.findOne.mockResolvedValue(webhook);
mockDeliveryService.getRecentDeliveries.mockResolvedValue(deliveries);
const result = await controller.getDeliveries('webhook-123', mockUser);
expect(mockPolicyService.assertOwnership).toHaveBeenCalledWith(mockUser, webhook);
expect(mockDeliveryService.getRecentDeliveries).toHaveBeenCalledWith('webhook-123', 50);
expect(result).toEqual(deliveries);
});
});
describe('getStats', () => {
it('should return delivery statistics and check ownership', async () => {
const stats = { total: 100, success: 95, failed: 5, pending: 0, successRate: 95.0 };
const webhook = { id: 'webhook-123', userId: mockUser.id };
mockWebhooksService.findOne.mockResolvedValue(webhook);
mockDeliveryService.getDeliveryStats.mockResolvedValue(stats);
const result = await controller.getStats('webhook-123', mockUser);
expect(mockPolicyService.assertOwnership).toHaveBeenCalledWith(mockUser, webhook);
expect(mockDeliveryService.getDeliveryStats).toHaveBeenCalledWith('webhook-123');
expect(result).toEqual(stats);
});
});
});