forked from MergeFi/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-webhooks.service.spec.ts
More file actions
206 lines (185 loc) · 6.26 KB
/
Copy pathgithub-webhooks.service.spec.ts
File metadata and controls
206 lines (185 loc) · 6.26 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
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { ConfigService } from '@nestjs/config';
import { GithubWebhooksService } from './github-webhooks.service';
import { GithubSyncService } from './github-sync.service';
import { BountiesService } from '../bounties/bounties.service';
import { Bounty, Issue, WebhookEvent } from '../common/entities';
import { WebhookEventStatus } from '../common/enums';
import * as sigUtil from './webhook-signature.util';
describe('GithubWebhooksService', () => {
let service: GithubWebhooksService;
let webhookEventRepo: { create: jest.Mock; save: jest.Mock };
let issueRepo: { findOne: jest.Mock };
let bountyRepo: { findOne: jest.Mock };
let bountiesService: {
markInReview: jest.Mock;
markMergedAndRelease: jest.Mock;
};
let syncService: {
findRepositoryByGithubId: jest.Mock;
upsertIssueRecord: jest.Mock;
};
beforeEach(async () => {
webhookEventRepo = {
create: jest.fn((data: Partial<WebhookEvent>) => ({
id: 'event-1',
...data,
})),
save: jest.fn((data: Partial<WebhookEvent>) => Promise.resolve(data)),
};
issueRepo = { findOne: jest.fn() };
bountyRepo = { findOne: jest.fn() };
bountiesService = {
markInReview: jest.fn().mockResolvedValue(undefined),
markMergedAndRelease: jest.fn().mockResolvedValue(undefined),
};
syncService = {
findRepositoryByGithubId: jest.fn(),
upsertIssueRecord: jest.fn(),
};
const module: TestingModule = await Test.createTestingModule({
providers: [
GithubWebhooksService,
{
provide: ConfigService,
useValue: { get: () => ({ webhookSecret: 'secret' }) },
},
{
provide: getRepositoryToken(WebhookEvent),
useValue: webhookEventRepo,
},
{ provide: getRepositoryToken(Issue), useValue: issueRepo },
{ provide: getRepositoryToken(Bounty), useValue: bountyRepo },
{ provide: BountiesService, useValue: bountiesService },
{ provide: GithubSyncService, useValue: syncService },
],
}).compile();
service = module.get(GithubWebhooksService);
});
it('delegates signature verification to verifyGithubSignature', () => {
const spy = jest
.spyOn(sigUtil, 'verifyGithubSignature')
.mockReturnValue(true);
const result = service.verifySignature(Buffer.from('{}'), 'sha256=abc');
expect(spy).toHaveBeenCalled();
expect(result).toBe(true);
spy.mockRestore();
});
it('records but ignores events with an invalid signature', async () => {
const event = await service.handleEvent(
'pull_request',
'delivery-1',
{},
false,
);
expect(event.status).toBe(WebhookEventStatus.IGNORED);
expect(bountiesService.markMergedAndRelease).not.toHaveBeenCalled();
});
it('processes a merged pull_request event and releases the linked bounty', async () => {
issueRepo.findOne.mockResolvedValue({
id: 'issue-1',
bounty: { id: 'bounty-1' },
});
bountyRepo.findOne.mockResolvedValue({ id: 'bounty-1', status: 'claimed' });
const payload = {
action: 'closed',
number: 7,
pull_request: {
html_url: 'https://github.com/acme/repo/pull/7',
number: 7,
merged: true,
body: 'This closes #42 for good',
},
repository: { id: 999, full_name: 'acme/repo' },
};
const event = await service.handleEvent(
'pull_request',
'delivery-2',
payload,
true,
);
expect(bountiesService.markInReview).toHaveBeenCalledWith(
'bounty-1',
payload.pull_request.html_url,
7,
);
expect(bountiesService.markMergedAndRelease).toHaveBeenCalledWith(
'bounty-1',
);
expect(event.status).toBe(WebhookEventStatus.PROCESSED);
});
it('ignores a closed-but-not-merged pull_request event', async () => {
const payload = {
action: 'closed',
number: 8,
pull_request: {
html_url: 'x',
number: 8,
merged: false,
body: 'closes #1',
},
repository: { id: 1, full_name: 'a/b' },
};
await service.handleEvent('pull_request', 'delivery-3', payload, true);
expect(bountiesService.markMergedAndRelease).not.toHaveBeenCalled();
});
describe('"issues" webhook events (#24)', () => {
const payload = {
action: 'edited',
issue: {
id: 555,
number: 12,
title: 'Updated title',
state: 'open',
html_url: 'https://github.com/acme/widgets/issues/12',
updated_at: '2026-01-10T00:00:00Z',
},
repository: { id: 42, full_name: 'acme/widgets' },
};
it('delegates to the same guarded upsert sync uses, for a tracked repository', async () => {
syncService.findRepositoryByGithubId.mockResolvedValue({ id: 'repo-1' });
syncService.upsertIssueRecord.mockResolvedValue({
issue: { id: 'issue-1' },
applied: true,
});
const event = await service.handleEvent(
'issues',
'delivery-4',
payload,
true,
);
expect(syncService.findRepositoryByGithubId).toHaveBeenCalledWith('42');
expect(syncService.upsertIssueRecord).toHaveBeenCalledWith(
'repo-1',
payload.issue,
);
expect(event.status).toBe(WebhookEventStatus.PROCESSED);
});
it('ignores events for a repository this app is not tracking, without erroring', async () => {
syncService.findRepositoryByGithubId.mockResolvedValue(null);
const event = await service.handleEvent(
'issues',
'delivery-5',
payload,
true,
);
expect(syncService.upsertIssueRecord).not.toHaveBeenCalled();
expect(event.status).toBe(WebhookEventStatus.PROCESSED);
});
it('still marks the event processed when the upsert is rejected as stale', async () => {
syncService.findRepositoryByGithubId.mockResolvedValue({ id: 'repo-1' });
syncService.upsertIssueRecord.mockResolvedValue({
issue: { id: 'issue-1' },
applied: false,
});
const event = await service.handleEvent(
'issues',
'delivery-6',
payload,
true,
);
expect(event.status).toBe(WebhookEventStatus.PROCESSED);
});
});
});