forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhookRepository.test.js
More file actions
78 lines (65 loc) · 3.15 KB
/
Copy pathwebhookRepository.test.js
File metadata and controls
78 lines (65 loc) · 3.15 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
'use strict';
const { createCacheMock } = require('./helpers/cacheMock');
const mockHelper = createCacheMock();
const { reset } = mockHelper;
jest.mock('../src/services/cache', () => mockHelper.cacheMock);
jest.mock('../src/logger', () => ({
info: jest.fn(), warn: jest.fn(), error: jest.fn(), debug: jest.fn(),
}));
const webhookRepo = require('../src/repositories/webhookRepository');
const events = require('../src/services/webhookEvents');
beforeEach(() => reset());
describe('webhookRepository', () => {
test('create persists a webhook with generated id and active=true', async () => {
const w = await webhookRepo.create({
url: 'https://example.com/hook',
events: ['pool.assets_locked'],
secret: 'whsec_aaaaaaaaaaaaaaaa',
});
expect(w.id).toMatch(/^wh_/);
expect(w.active).toBe(true);
expect(w.events).toEqual(['pool.assets_locked']);
});
test('findById returns the stored webhook', async () => {
const created = await webhookRepo.create({
url: 'https://example.com/hook',
events: ['*'],
secret: 'whsec_aaaaaaaaaaaaaaaa',
});
const found = await webhookRepo.findById(created.id);
expect(found.id).toBe(created.id);
});
test('findById returns null when missing', async () => {
expect(await webhookRepo.findById('wh_nope')).toBeNull();
});
test('list returns all created webhooks', async () => {
await webhookRepo.create({ url: 'https://a.com', events: ['*'], secret: 'whsec_aaaaaaaaaaaaaaaa' });
await webhookRepo.create({ url: 'https://b.com', events: ['*'], secret: 'whsec_bbbbbbbbbbbbbbbb' });
const all = await webhookRepo.list();
expect(all).toHaveLength(2);
});
test('update merges patch and bumps updated_at', async () => {
const w = await webhookRepo.create({ url: 'https://a.com', events: ['*'], secret: 'whsec_aaaaaaaaaaaaaaaa' });
const updated = await webhookRepo.update(w.id, { active: false });
expect(updated.active).toBe(false);
expect(updated.created_at).toBe(w.created_at);
expect(updated.updated_at >= w.updated_at).toBe(true);
});
test('remove deletes and returns the previous record', async () => {
const w = await webhookRepo.create({ url: 'https://a.com', events: ['*'], secret: 'whsec_aaaaaaaaaaaaaaaa' });
const removed = await webhookRepo.remove(w.id);
expect(removed.id).toBe(w.id);
expect(await webhookRepo.list()).toHaveLength(0);
});
test('listActiveForEvent filters by subscription and active flag', async () => {
const a = await webhookRepo.create({ url: 'https://a.com', events: ['pool.assets_locked'], secret: 'whsec_aaaaaaaaaaaaaaaa' });
const b = await webhookRepo.create({ url: 'https://b.com', events: ['pool.closed'], secret: 'whsec_bbbbbbbbbbbbbbbb' });
const c = await webhookRepo.create({ url: 'https://c.com', events: ['*'], secret: 'whsec_cccccccccccccccc' });
await webhookRepo.update(c.id, { active: false });
const result = await webhookRepo.listActiveForEvent('pool.assets_locked', events.matchesSubscription);
const ids = result.map((w) => w.id).sort();
expect(ids).toEqual([a.id].sort());
expect(ids).not.toContain(b.id);
expect(ids).not.toContain(c.id);
});
});