forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathairdrops.test.js
More file actions
667 lines (582 loc) · 21.4 KB
/
Copy pathairdrops.test.js
File metadata and controls
667 lines (582 loc) · 21.4 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
'use strict';
const mockStore = new Map();
const mockSets = new Map();
const mockZSets = new Map();
const mockLists = new Map();
const mockCounters = new Map();
const mockRedis = {
smembers: jest.fn(async (key) => [...(mockSets.get(key) || [])]),
sadd: jest.fn(async (key, val) => {
if (!mockSets.has(key)) mockSets.set(key, new Set());
mockSets.get(key).add(val);
}),
srem: jest.fn(async (key, val) => {
mockSets.get(key)?.delete(val);
}),
zadd: jest.fn(async (key, score, member) => {
if (!mockZSets.has(key)) mockZSets.set(key, new Map());
mockZSets.get(key).set(member, Number(score));
}),
zrem: jest.fn(async (key, ...members) => {
const z = mockZSets.get(key);
if (!z) return;
for (const m of members) z.delete(m);
}),
zrevrange: jest.fn(async (key, start, stop) => {
const z = mockZSets.get(key);
if (!z) return [];
const sorted = [...z.entries()].sort((a, b) => b[1] - a[1]).map(([m]) => m);
const end = stop === -1 ? sorted.length : stop + 1;
return sorted.slice(start, end);
}),
zcard: jest.fn(async (key) => (mockZSets.get(key)?.size || 0)),
zscan: jest.fn(async (key, cursor, _countKeyword, count) => {
const entries = [...(mockZSets.get(key)?.entries() || [])];
const batchWithScores = [];
const start = Number(cursor);
for (let i = start; i < start + count && i < entries.length; i += 1) {
batchWithScores.push(entries[i][0], entries[i][1]);
}
const nextCursor = start + count >= entries.length ? '0' : String(start + count);
return [nextCursor, batchWithScores];
}),
llen: jest.fn(async (key) => (mockLists.get(key) || []).length),
lpush: jest.fn(async (key, ...vals) => {
if (!mockLists.has(key)) mockLists.set(key, []);
mockLists.get(key).unshift(...vals);
}),
rpush: jest.fn(async (key, ...vals) => {
if (!mockLists.has(key)) mockLists.set(key, []);
mockLists.get(key).push(...vals);
}),
lrange: jest.fn(async (key, start, end) => {
const list = mockLists.get(key) || [];
const startIdx = start === -1 ? list.length + start : start;
const endIdx = end === -1 ? list.length + end : end;
return list.slice(startIdx, endIdx + 1);
}),
incr: jest.fn(async (key) => {
const count = (mockCounters.get(key) || 0) + 1;
mockCounters.set(key, count);
return count;
}),
expire: jest.fn(async () => 1),
};
jest.mock('../src/services/cache', () => ({
getClient: () => mockRedis,
get: jest.fn(async (key) => {
const v = mockStore.get(key);
return v !== undefined ? JSON.parse(JSON.stringify(v)) : null;
}),
set: jest.fn(async (key, value) => {
mockStore.set(key, JSON.parse(JSON.stringify(value)));
}),
del: jest.fn(async (key) => {
mockStore.delete(key);
mockLists.delete(key);
}),
}));
jest.mock('../src/logger', () => ({
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
}));
const mockLedger = { sequence: 12345 };
jest.mock('@stellar/stellar-sdk', () => ({
Horizon: {
Server: jest.fn(() => ({
ledgers: jest.fn(() => ({
order: jest.fn(() => ({
limit: jest.fn(() => ({
call: jest.fn(async () => ({ records: [mockLedger] })),
})),
})),
})),
})),
},
StrKey: {
isValidEd25519PublicKey: jest.fn((address) => address.startsWith('G') && address.length === 56),
},
rpc: {
Server: jest.fn(() => ({})),
},
}));
const request = require('supertest');
const cache = require('../src/services/cache');
const config = require('../src/config');
let app;
beforeAll(() => {
app = require('../src/index').app;
});
beforeEach(() => {
mockStore.clear();
mockSets.clear();
mockZSets.clear();
mockLists.clear();
mockCounters.clear();
cache.get.mockClear();
cache.set.mockClear();
cache.del.mockClear();
mockRedis.smembers.mockClear();
mockRedis.sadd.mockClear();
mockRedis.srem.mockClear();
mockRedis.zadd.mockClear();
mockRedis.zrem.mockClear();
mockRedis.zcard.mockClear();
mockRedis.zrevrange.mockClear();
mockRedis.zrevrange.mockClear();
mockRedis.zcard.mockClear();
mockRedis.zscan.mockClear();
mockRedis.llen.mockClear();
mockRedis.lpush.mockClear();
mockRedis.rpush.mockClear();
mockRedis.lrange.mockClear();
mockRedis.incr.mockClear();
mockRedis.expire.mockClear();
});
const validAddress1 = 'GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5';
const validAddress2 = 'GDRREYWHQWJDICNH4SAH4TT2JPVYWIX6JEWAHE2W6BZDJBIJ4VSX227Z';
describe('POST /api/v1/airdrops', () => {
test('creates airdrop successfully', async () => {
const response = await request(app)
.post('/api/v1/airdrops')
.send({
name: 'Test Airdrop',
description: 'Test Description',
asset: 'USDC',
asset_issuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335AX2OBFLDTQLNUEHRGPTM6RIA',
total_amount: 100,
expiry_ledger: 123456, // Greater than mockLedger.sequence (12345)
recipients: [
{ address: validAddress1, amount: 50 },
{ address: validAddress2, amount: 50 },
],
});
expect(response.status).toBe(201);
expect(response.body.id).toMatch(/^drop_/);
expect(response.body.name).toBe('Test Airdrop');
});
test('returns validation error for invalid Stellar address', async () => {
const response = await request(app)
.post('/api/v1/airdrops')
.send({
name: 'Test Airdrop',
asset: 'USDC',
asset_issuer: 'invalid',
total_amount: 100,
expiry_ledger: 123456,
});
expect(response.status).toBe(400);
expect(response.body.error.code).toBe('VALIDATION_ERROR');
});
test('returns validation error when sum of recipients does not equal total_amount', async () => {
const response = await request(app)
.post('/api/v1/airdrops')
.send({
name: 'Test Airdrop',
asset: 'USDC',
asset_issuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335AX2OBFLDTQLNUEHRGPTM6RIA',
total_amount: 100,
expiry_ledger: 123456,
recipients: [{ address: validAddress1, amount: 50 }],
});
expect(response.status).toBe(400);
expect(response.body.error).toMatchObject({
code: 'VALIDATION_ERROR',
message: 'Validation failed',
});
expect(response.body.error.details.fields.recipients).toEqual(
expect.arrayContaining([expect.stringContaining('sum of recipient amounts')])
);
});
test('rate limits repeated airdrop creation attempts', async () => {
for (let i = 0; i < config.airdrops.rateLimit.max; i += 1) {
const response = await request(app).post('/api/v1/airdrops').send({});
expect(response.status).toBe(400);
}
const blocked = await request(app).post('/api/v1/airdrops').send({});
expect(blocked.status).toBe(429);
expect(blocked.body.error.code).toBe('RATE_LIMITED');
});
});
describe('GET /api/v1/airdrops', () => {
test('lists airdrops with pagination', async () => {
const res1 = await request(app)
await request(app)
.post('/api/v1/airdrops')
.send({
name: 'Airdrop 1',
asset: 'USDC',
asset_issuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335AX2OBFLDTQLNUEHRGPTM6RIA',
total_amount: 100,
expiry_ledger: 123456,
});
const res2 = await request(app)
await request(app)
.post('/api/v1/airdrops')
.send({
name: 'Airdrop 2',
asset: 'XLM',
asset_issuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335AX2OBFLDTQLNUEHRGPTM6RIA',
total_amount: 200,
expiry_ledger: 123457,
});
const response = await request(app).get('/api/v1/airdrops?page=1&limit=2');
expect(response.status).toBe(200);
// Canonical pagination envelope (#131): array under `data`, not `airdrops`.
expect(response.body.data).toHaveLength(2);
expect(response.body.pagination.total).toBe(2);
expect(response.body.pagination.has_next).toBe(false);
expect(response.body.pagination.has_prev).toBe(false);
});
});
describe('GET /api/v1/airdrops/:id', () => {
test('returns airdrop by id', async () => {
const createResponse = await request(app)
.post('/api/v1/airdrops')
.send({
name: 'Test Airdrop',
asset: 'USDC',
asset_issuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335AX2OBFLDTQLNUEHRGPTM6RIA',
total_amount: 100,
expiry_ledger: 123456,
});
const getResponse = await request(app).get(`/api/v1/airdrops/${createResponse.body.id}`);
expect(getResponse.status).toBe(200);
expect(getResponse.body.id).toBe(createResponse.body.id);
});
test('returns 404 for non-existent airdrop', async () => {
const response = await request(app).get('/api/v1/airdrops/drop_nonexistent');
expect(response.status).toBe(404);
});
});
describe('PATCH /api/v1/airdrops/:id', () => {
test('updates airdrop successfully', async () => {
const createResponse = await request(app)
.post('/api/v1/airdrops')
.send({
name: 'Test Airdrop',
asset: 'USDC',
asset_issuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335AX2OBFLDTQLNUEHRGPTM6RIA',
total_amount: 100,
expiry_ledger: 123456,
});
const updateResponse = await request(app)
.patch(`/api/v1/airdrops/${createResponse.body.id}`)
.send({ name: 'Updated Airdrop', description: 'Updated Description' });
expect(updateResponse.status).toBe(200);
expect(updateResponse.body.name).toBe('Updated Airdrop');
expect(updateResponse.body.description).toBe('Updated Description');
});
});
describe('DELETE /api/v1/airdrops/:id', () => {
test('deletes airdrop successfully', async () => {
const createResponse = await request(app)
.post('/api/v1/airdrops')
.send({
name: 'Test Airdrop',
asset: 'USDC',
asset_issuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335AX2OBFLDTQLNUEHRGPTM6RIA',
total_amount: 100,
expiry_ledger: 123456,
});
const deleteResponse = await request(app).delete(`/api/v1/airdrops/${createResponse.body.id}`);
expect(deleteResponse.status).toBe(200);
expect(deleteResponse.body.deleted).toBe(true);
});
});
describe('POST /api/v1/airdrops/:id/cancel', () => {
test('cancels airdrop successfully', async () => {
const createResponse = await request(app)
.post('/api/v1/airdrops')
.send({
name: 'Test Airdrop',
asset: 'USDC',
asset_issuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335AX2OBFLDTQLNUEHRGPTM6RIA',
total_amount: 100,
expiry_ledger: 123456,
});
const cancelResponse = await request(app).post(`/api/v1/airdrops/${createResponse.body.id}/cancel`);
expect(cancelResponse.status).toBe(200);
expect(cancelResponse.body.status).toBe('cancelled');
});
test('idempotent cancellation', async () => {
const createResponse = await request(app)
.post('/api/v1/airdrops')
.send({
name: 'Test Airdrop',
asset: 'USDC',
asset_issuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335AX2OBFLDTQLNUEHRGPTM6RIA',
total_amount: 100,
expiry_ledger: 123456,
});
await request(app).post(`/api/v1/airdrops/${createResponse.body.id}/cancel`);
const secondCancelResponse = await request(app).post(`/api/v1/airdrops/${createResponse.body.id}/cancel`);
expect(secondCancelResponse.status).toBe(200);
});
});
describe('POST /api/v1/airdrops/:id/recipients', () => {
test('adds recipients successfully', async () => {
const createResponse = await request(app)
.post('/api/v1/airdrops')
.send({
name: 'Test Airdrop',
asset: 'USDC',
asset_issuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335AX2OBFLDTQLNUEHRGPTM6RIA',
total_amount: 100,
expiry_ledger: 123456,
});
const addResponse = await request(app)
.post(`/api/v1/airdrops/${createResponse.body.id}/recipients`)
.send({ recipients: [{ address: validAddress1, amount: 50 }] });
expect(addResponse.status).toBe(201);
expect(addResponse.body.added).toBe(1);
});
test('parses CSV file successfully', async () => {
const createResponse = await request(app)
.post('/api/v1/airdrops')
.send({
name: 'Test Airdrop',
asset: 'USDC',
asset_issuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335AX2OBFLDTQLNUEHRGPTM6RIA',
total_amount: 100,
expiry_ledger: 123456,
});
const csvContent = 'address,amount\n' + validAddress1 + ',50\n' + validAddress2 + ',50';
const addResponse = await request(app)
.post(`/api/v1/airdrops/${createResponse.body.id}/recipients`)
.attach('file', Buffer.from(csvContent), 'recipients.csv');
expect(addResponse.status).toBe(201);
expect(addResponse.body.added).toBe(2);
});
test('rejects a CSV larger than the configured upload limit', async () => {
const createResponse = await request(app)
.post('/api/v1/airdrops')
.send({
name: 'Test Airdrop',
asset: 'USDC',
asset_issuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335AX2OBFLDTQLNUEHRGPTM6RIA',
total_amount: 100,
expiry_ledger: 123456,
});
const oversized = Buffer.alloc(config.airdrops.csvMaxBytes + 1, 'a');
const response = await request(app)
.post(`/api/v1/airdrops/${createResponse.body.id}/recipients`)
.attach('file', oversized, 'recipients.csv');
expect(response.status).toBe(413);
expect(response.body.error).toMatchObject({
code: 'PAYLOAD_TOO_LARGE',
details: { max_bytes: config.airdrops.csvMaxBytes },
});
expect(mockRedis.rpush).not.toHaveBeenCalled();
});
test('stops CSV parsing when the 10,000-row limit is crossed', async () => {
const createResponse = await request(app)
.post('/api/v1/airdrops')
.send({
name: 'Test Airdrop',
asset: 'USDC',
asset_issuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335AX2OBFLDTQLNUEHRGPTM6RIA',
total_amount: 100,
expiry_ledger: 123456,
});
const row = `${validAddress1},1\n`;
const csvContent = `address,amount\n${row.repeat(10001)}`;
const response = await request(app)
.post(`/api/v1/airdrops/${createResponse.body.id}/recipients`)
.attach('file', Buffer.from(csvContent), 'recipients.csv');
expect(response.status).toBe(400);
expect(response.body.error).toMatchObject({
code: 'RECIPIENT_LIMIT_EXCEEDED',
message: 'CSV cannot exceed 10000 recipients',
});
expect(mockRedis.rpush).not.toHaveBeenCalled();
});
test('rejects a CSV file with non-UTF-8 encoding', async () => {
const createResponse = await request(app)
.post('/api/v1/airdrops')
.send({
name: 'Test Airdrop',
asset: 'USDC',
asset_issuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335AX2OBFLDTQLNUEHRGPTM6RIA',
total_amount: 100,
expiry_ledger: 123456,
});
const invalidUtf8Buffer = Buffer.from([0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x2c, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x0a, 0xa0, 0xa1, 0xc0]);
const response = await request(app)
.post(`/api/v1/airdrops/${createResponse.body.id}/recipients`)
.attach('file', invalidUtf8Buffer, 'recipients.csv');
expect(response.status).toBe(400);
expect(response.body.error).toMatchObject({
code: 'CSV_INVALID_ENCODING',
message: expect.stringContaining('UTF-8'),
});
});
test('rate limits repeated recipient additions', async () => {
const createResponse = await request(app)
.post('/api/v1/airdrops')
.send({
name: 'Test Airdrop',
asset: 'USDC',
asset_issuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335AX2OBFLDTQLNUEHRGPTM6RIA',
total_amount: 100,
expiry_ledger: 123456,
});
const endpoint = `/api/v1/airdrops/${createResponse.body.id}/recipients`;
for (let i = 0; i < config.airdrops.rateLimit.max; i += 1) {
const response = await request(app)
.post(endpoint)
.send({ recipients: [{ address: validAddress1, amount: 1 }] });
expect(response.status).toBe(201);
}
const blocked = await request(app)
.post(endpoint)
.send({ recipients: [{ address: validAddress1, amount: 1 }] });
expect(blocked.status).toBe(429);
expect(blocked.body.error.code).toBe('RATE_LIMITED');
});
});
describe('GET /api/v1/airdrops/:id/recipients', () => {
test('lists recipients with pagination', async () => {
const createResponse = await request(app)
.post('/api/v1/airdrops')
.send({
name: 'Test Airdrop',
asset: 'USDC',
asset_issuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335AX2OBFLDTQLNUEHRGPTM6RIA',
total_amount: 100,
expiry_ledger: 123456,
recipients: [
{ address: validAddress1, amount: 50 },
{ address: validAddress2, amount: 50 },
],
});
const listResponse = await request(app).get(`/api/v1/airdrops/${createResponse.body.id}/recipients`);
expect(listResponse.status).toBe(200);
// Canonical pagination envelope (#131): array under `data`, not `recipients`.
expect(listResponse.body.data).toHaveLength(2);
expect(listResponse.body.pagination.total).toBe(2);
});
});
// ---------------------------------------------------------------------------
// CSV structure validation (issue #254)
// ---------------------------------------------------------------------------
describe('POST /api/v1/airdrops/:id/recipients — CSV structure validation', () => {
async function createAirdrop() {
const res = await request(app)
.post('/api/v1/airdrops')
.send({
name: 'Test Airdrop',
asset: 'USDC',
asset_issuer: 'GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335AX2OBFLDTQLNUEHRGPTM6RIA',
total_amount: 100,
expiry_ledger: 123456,
});
return res.body.id;
}
async function uploadCsv(id, content) {
return request(app)
.post(`/api/v1/airdrops/${id}/recipients`)
.attach('file', Buffer.from(content), 'recipients.csv');
}
test('rejects a CSV whose required columns are missing', async () => {
const id = await createAirdrop();
const res = await uploadCsv(id, `wallet,value
${validAddress1},50`);
expect(res.status).toBe(400);
expect(res.body.error.code).toBe('CSV_MISSING_COLUMNS');
expect(res.body.error.details.missing_columns).toEqual(
expect.arrayContaining(['address', 'amount']),
);
});
test('names only the column that is actually missing', async () => {
const id = await createAirdrop();
const res = await uploadCsv(id, `address,value
${validAddress1},50`);
expect(res.body.error.details.missing_columns).toEqual(['amount']);
});
test('accepts columns regardless of case and surrounding whitespace', async () => {
const id = await createAirdrop();
const res = await uploadCsv(id, ` Address , AMOUNT
${validAddress1},50`);
expect(res.status).toBe(201);
});
test('rejects a CSV with no data rows instead of importing nothing silently', async () => {
const id = await createAirdrop();
const res = await uploadCsv(id, 'address,amount');
expect(res.status).toBe(400);
expect(res.body.error.code).toBe('CSV_EMPTY');
});
test('reports rows whose amount is not a number rather than dropping them', async () => {
const id = await createAirdrop();
const res = await uploadCsv(id, `address,amount
${validAddress1},not-a-number`);
expect(res.status).toBe(400);
expect(res.body.error.code).toBe('CSV_MALFORMED');
expect(res.body.error.details.invalid_rows).toEqual([
{ line: 2, reason: 'amount is not a number' },
]);
});
test('reports rows with a missing address', async () => {
const id = await createAirdrop();
const res = await uploadCsv(id, 'address,amount\n,50');
expect(res.body.error.details.invalid_rows).toEqual([
{ line: 2, reason: 'missing address' },
]);
});
test('reports rows whose amount is zero or negative', async () => {
const id = await createAirdrop();
const res = await uploadCsv(id, `address,amount
${validAddress1},0`);
expect(res.body.error.details.invalid_rows).toEqual([
{ line: 2, reason: 'amount must be greater than zero' },
]);
});
test('line numbers account for the header, matching a text editor', async () => {
const id = await createAirdrop();
const res = await uploadCsv(
id,
`address,amount
${validAddress1},50
${validAddress2},bad`,
);
expect(res.body.error.details.invalid_rows).toEqual([
{ line: 3, reason: 'amount is not a number' },
]);
});
test('rejects the whole upload rather than partially importing a mixed file', async () => {
const id = await createAirdrop();
const res = await uploadCsv(
id,
`address,amount
${validAddress1},50
${validAddress2},bad`,
);
expect(res.status).toBe(400);
expect(res.body.error.details.valid_rows).toBe(1);
expect(res.body.error.details.total_rows).toBe(2);
expect(mockRedis.rpush).not.toHaveBeenCalled();
});
test('caps how many invalid rows are echoed back to the uploader', async () => {
const id = await createAirdrop();
const rows = Array.from({ length: 40 }, () => ',0').join('\n');
const res = await uploadCsv(id, `address,amount
${rows}`);
expect(res.status).toBe(400);
expect(res.body.error.code).toBe('CSV_MALFORMED');
expect(res.body.error.details.truncated).toBe(true);
expect(res.body.error.details.invalid_rows.length).toBeLessThanOrEqual(20);
});
test('still accepts a fully valid CSV', async () => {
const id = await createAirdrop();
const res = await uploadCsv(
id,
`address,amount
${validAddress1},50
${validAddress2},25.5`,
);
expect(res.status).toBe(201);
expect(res.body.added).toBe(2);
});
});