forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook.service.spec.ts
More file actions
86 lines (72 loc) · 2.36 KB
/
Copy pathwebhook.service.spec.ts
File metadata and controls
86 lines (72 loc) · 2.36 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
import { Test, TestingModule } from '@nestjs/testing';
import { WebhookService } from '../webhook.service';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Webhook } from '../webhook.entity';
import { WebhookDelivery } from '../webhook-delivery.entity';
describe('WebhookService', () => {
let service: WebhookService;
const mockWebhookRepository = {
create: jest.fn(),
save: jest.fn(),
find: jest.fn(),
findOne: jest.fn(),
count: jest.fn(),
update: jest.fn(),
};
const mockDeliveryRepository = {
create: jest.fn(),
save: jest.fn(),
find: jest.fn(),
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
WebhookService,
{
provide: getRepositoryToken(Webhook),
useValue: mockWebhookRepository,
},
{
provide: getRepositoryToken(WebhookDelivery),
useValue: mockDeliveryRepository,
},
],
}).compile();
service = module.get<WebhookService>(WebhookService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
describe('registerWebhook', () => {
it('should register a new webhook', async () => {
const mockWebhook = {
id: '123',
merchantId: 'merchant123',
url: 'https://example.com/webhook',
secret: 'secret123',
isActive: true,
};
mockWebhookRepository.count.mockResolvedValue(0);
mockWebhookRepository.create.mockReturnValue(mockWebhook);
mockWebhookRepository.save.mockResolvedValue(mockWebhook);
const result = await service.registerWebhook(
'merchant123',
'https://example.com/webhook',
);
expect(result).toEqual(mockWebhook);
expect(mockWebhookRepository.count).toHaveBeenCalled();
});
it('should throw error if more than 5 webhooks', async () => {
mockWebhookRepository.count.mockResolvedValue(5);
await expect(
service.registerWebhook('merchant123', 'https://example.com/webhook'),
).rejects.toThrow('Maximum of 5 webhook URLs allowed');
});
it('should throw error for invalid URL', async () => {
mockWebhookRepository.count.mockResolvedValue(0);
await expect(
service.registerWebhook('merchant123', 'invalid-url'),
).rejects.toThrow('Invalid URL format');
});
});
});