forked from MergeFi/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathescrow-fk-integrity.integration.spec.ts
More file actions
303 lines (274 loc) · 9.74 KB
/
Copy pathescrow-fk-integrity.integration.spec.ts
File metadata and controls
303 lines (274 loc) · 9.74 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import { DataSource, Repository } from 'typeorm';
import { entities } from '../common/entities/typeorm-entities';
import {
Bounty,
Escrow,
Milestone,
Payment,
Repository as Repo,
Issue,
User,
} from '../common/entities';
import {
AssetType,
BountyStatus,
EscrowStatus,
PaymentStatus,
} from '../common/enums';
import { SponsorsService } from '../sponsors/sponsors.service';
/**
* Integration tests for #27: these hit a real Postgres (see
* .github/workflows/ci.yml's `postgres` service / docker-compose.yml's `db`
* service — DATABASE_URL must point at a real, disposable database). Every
* other .spec.ts in this repo mocks its repositories; the whole point of
* this bug is DB-level FK/CHECK behavior that a mocked repository can't
* exercise, so this file uses `synchronize: true` against a real connection
* to build the schema straight from the (now-fixed) entity decorators.
*/
describe('Escrow FK integrity + sponsor dashboard reconciliation (integration)', () => {
let dataSource: DataSource;
let bountyRepo: Repository<Bounty>;
let milestoneRepo: Repository<Milestone>;
let paymentRepo: Repository<Payment>;
let escrowRepo: Repository<Escrow>;
let repoRepo: Repository<Repo>;
let issueRepo: Repository<Issue>;
let userRepo: Repository<User>;
let sponsorsService: SponsorsService;
beforeAll(async () => {
dataSource = new DataSource({
type: 'postgres',
url:
process.env.DATABASE_URL ??
'postgresql://postgres:postgres@localhost:5432/mergefi',
entities,
synchronize: true,
dropSchema: true,
});
await dataSource.initialize();
bountyRepo = dataSource.getRepository(Bounty);
milestoneRepo = dataSource.getRepository(Milestone);
paymentRepo = dataSource.getRepository(Payment);
escrowRepo = dataSource.getRepository(Escrow);
repoRepo = dataSource.getRepository(Repo);
issueRepo = dataSource.getRepository(Issue);
userRepo = dataSource.getRepository(User);
sponsorsService = new SponsorsService(
bountyRepo,
milestoneRepo,
paymentRepo,
escrowRepo,
);
}, 30_000);
afterAll(async () => {
if (dataSource?.isInitialized) await dataSource.destroy();
});
afterEach(async () => {
// Delete in child-to-parent order — payments/escrows first now that
// their FKs are SET NULL/RESTRICT instead of CASCADE, they won't be
// cleaned up automatically by deleting bounties/milestones.
await paymentRepo.query('DELETE FROM payments');
await escrowRepo.query('DELETE FROM escrows');
await bountyRepo.query('DELETE FROM bounties');
await issueRepo.query('DELETE FROM issues');
await milestoneRepo.query('DELETE FROM milestones');
await repoRepo.query('DELETE FROM repositories');
await userRepo.query('DELETE FROM users');
});
async function makeSponsor(): Promise<User> {
return userRepo.save(
userRepo.create({ username: `sponsor-${Date.now()}-${Math.random()}` }),
);
}
async function makeBounty(
sponsorId: string,
amount = '100',
): Promise<Bounty> {
const repository = await repoRepo.save(
repoRepo.create({
githubRepoId: `repo-${Date.now()}-${Math.random()}`,
owner: 'octocat',
name: `repo-${Math.random()}`,
fullName: 'octocat/repo',
}),
);
const issue = await issueRepo.save(
issueRepo.create({
repositoryId: repository.id,
githubIssueId: `issue-${Date.now()}-${Math.random()}`,
number: 1,
title: 'Fix the bug',
githubUrl: 'https://github.com/octocat/repo/issues/1',
}),
);
return bountyRepo.save(
bountyRepo.create({
issueId: issue.id,
sponsorId,
amount,
asset: AssetType.USDC,
status: BountyStatus.FUNDED,
}),
);
}
async function makeMilestone(
sponsorId: string,
budget = '100',
): Promise<Milestone> {
const repository = await repoRepo.save(
repoRepo.create({
githubRepoId: `repo-${Date.now()}-${Math.random()}`,
owner: 'octocat',
name: `repo-${Math.random()}`,
fullName: 'octocat/repo',
}),
);
return milestoneRepo.save(
milestoneRepo.create({
repositoryId: repository.id,
sponsorId,
title: 'Q1 roadmap',
budget,
asset: AssetType.USDC,
}),
);
}
describe('CHK_escrow_at_most_one_parent', () => {
it('rejects an escrow with more than one parent set', async () => {
const sponsor = await makeSponsor();
// Both parents reference genuinely existing rows, so the only way
// this insert can fail is the CHECK constraint — not a coincidental
// FK violation on a dangling id.
const bounty = await makeBounty(sponsor.id);
const milestone = await makeMilestone(sponsor.id);
await expect(
escrowRepo.query(
`INSERT INTO escrows (id, "bountyId", "milestoneId", amount, asset, status)
VALUES (gen_random_uuid(), $1, $2, '10', 'USDC', 'pending')`,
[bounty.id, milestone.id],
),
).rejects.toThrow(/CHK_escrow_at_most_one_parent/);
});
it('allows an escrow with exactly one parent set', async () => {
const sponsor = await makeSponsor();
const bounty = await makeBounty(sponsor.id);
const escrow = await escrowRepo.save(
escrowRepo.create({
bountyId: bounty.id,
sponsorId: sponsor.id,
amount: '10',
asset: AssetType.USDC,
status: EscrowStatus.LOCKED,
}),
);
expect(escrow.id).toBeDefined();
});
it('allows an escrow with zero parents set (the orphaned-by-deletion state)', async () => {
// The DB-level constraint deliberately allows this — it's exactly
// the state ON DELETE SET NULL produces when an escrow's parent is
// deleted (see the next describe block). "Exactly one" is an
// application-level rule enforced at creation time in
// EscrowService.assertExactlyOneParent, not a DB invariant, because
// the DB has no way to distinguish "never had a parent" from
// "orphaned by a legitimate deletion".
const escrow = await escrowRepo.save(
escrowRepo.create({
amount: '10',
asset: AssetType.USDC,
status: EscrowStatus.LOCKED,
}),
);
expect(escrow.id).toBeDefined();
});
});
describe('parent deletion no longer destroys the escrow ledger row', () => {
it('SET NULLs escrows.bountyId instead of deleting the row when the bounty is deleted', async () => {
const sponsor = await makeSponsor();
const bounty = await makeBounty(sponsor.id);
const escrow = await escrowRepo.save(
escrowRepo.create({
bountyId: bounty.id,
sponsorId: sponsor.id,
amount: '250',
asset: AssetType.USDC,
status: EscrowStatus.LOCKED,
}),
);
await bountyRepo.delete(bounty.id);
const survived = await escrowRepo.findOne({ where: { id: escrow.id } });
expect(survived).not.toBeNull();
expect(survived?.bountyId).toBeNull();
expect(survived?.status).toBe(EscrowStatus.LOCKED);
expect(Number(survived?.amount)).toBe(250);
});
it('RESTRICTs deleting an escrow that still has payment records', async () => {
const sponsor = await makeSponsor();
const bounty = await makeBounty(sponsor.id);
const escrow = await escrowRepo.save(
escrowRepo.create({
bountyId: bounty.id,
sponsorId: sponsor.id,
amount: '250',
asset: AssetType.USDC,
status: EscrowStatus.RELEASED,
}),
);
await paymentRepo.save(
paymentRepo.create({
escrowId: escrow.id,
recipientAddress: 'GRECIPIENT',
amount: '250',
asset: AssetType.USDC,
status: PaymentStatus.CONFIRMED,
}),
);
await expect(escrowRepo.delete(escrow.id)).rejects.toThrow();
});
});
describe('sponsor dashboard figures survive parent bounty deletion (#27 acceptance criterion)', () => {
it('budgetLocked keeps counting a stranded escrow after its bounty is deleted', async () => {
const sponsor = await makeSponsor();
const bounty = await makeBounty(sponsor.id, '400');
await escrowRepo.save(
escrowRepo.create({
bountyId: bounty.id,
sponsorId: sponsor.id,
amount: '400',
asset: AssetType.USDC,
status: EscrowStatus.LOCKED,
}),
);
expect(await sponsorsService.budgetLocked(sponsor.id)).toBe(400);
await bountyRepo.delete(bounty.id);
// The old implementation summed Bounty.amount by Bounty.status, so
// this figure would silently drop to 0 the instant the bounty row
// was gone — even though the funds are still LOCKED on-chain.
expect(await sponsorsService.budgetLocked(sponsor.id)).toBe(400);
});
it('totalSpend keeps counting a confirmed payment after its bounty is deleted', async () => {
const sponsor = await makeSponsor();
const bounty = await makeBounty(sponsor.id, '150');
const escrow = await escrowRepo.save(
escrowRepo.create({
bountyId: bounty.id,
sponsorId: sponsor.id,
amount: '150',
asset: AssetType.USDC,
status: EscrowStatus.RELEASED,
}),
);
await paymentRepo.save(
paymentRepo.create({
escrowId: escrow.id,
recipientAddress: 'GRECIPIENT',
amount: '150',
asset: AssetType.USDC,
status: PaymentStatus.CONFIRMED,
}),
);
expect(await sponsorsService.totalSpend(sponsor.id)).toBe(150);
await bountyRepo.delete(bounty.id);
expect(await sponsorsService.totalSpend(sponsor.id)).toBe(150);
});
});
});