|
1 | 1 | import request from 'supertest'; |
2 | 2 | import app from '../app'; |
3 | 3 |
|
| 4 | +const VALID_ADDRESS = 'GDZZJ3UPZZCKY5DBH6ZGMPMRORRBG4ECIORASBUAXPPNCL4SYRHNLYU2'; |
| 5 | + |
4 | 6 | describe('Validation Middleware', () => { |
5 | 7 | describe('Prepare Validation (GET /api/lending/prepare/:operation)', () => { |
6 | 8 | it('should reject empty userAddress', async () => { |
7 | 9 | const response = await request(app) |
8 | 10 | .get('/api/lending/prepare/deposit') |
9 | | - .send({ amount: '1000000' }); |
| 11 | + .query({ amount: '1000000' }); |
| 12 | + |
| 13 | + expect(response.status).toBe(400); |
| 14 | + expect(response.body.error).toBeDefined(); |
| 15 | + expect(response.body.error).toContain('User address is required'); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should reject invalid Stellar public key', async () => { |
| 19 | + const response = await request(app).get('/api/lending/prepare/deposit').query({ |
| 20 | + userAddress: 'invalid-address', |
| 21 | + assetAddress: 'G...', |
| 22 | + amount: '100', |
| 23 | + }); |
| 24 | + |
| 25 | + expect(response.status).toBe(400); |
| 26 | + expect(response.body.error).toBeDefined(); |
| 27 | + expect(response.body.error).toContain('Invalid Stellar address'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should reject Stellar address with wrong prefix', async () => { |
| 31 | + const response = await request(app).get('/api/lending/prepare/deposit').query({ |
| 32 | + userAddress: 'S...', // Secret key prefix |
| 33 | + assetAddress: 'G...', |
| 34 | + amount: '100', |
| 35 | + }); |
| 36 | + |
| 37 | + expect(response.status).toBe(400); |
| 38 | + expect(response.body.error).toBeDefined(); |
| 39 | + expect(response.body.error).toContain('Invalid Stellar address'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should accept valid Stellar public key', async () => { |
| 43 | + const response = await request(app).get('/api/lending/prepare/deposit').query({ |
| 44 | + userAddress: VALID_ADDRESS, |
| 45 | + assetAddress: 'G...', |
| 46 | + amount: '100', |
| 47 | + }); |
| 48 | + |
| 49 | + expect(response.status).not.toBe(400); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should reject missing amount', async () => { |
| 53 | + const response = await request(app).get('/api/lending/prepare/deposit').query({ |
| 54 | + userAddress: VALID_ADDRESS, |
| 55 | + assetAddress: 'G...', |
| 56 | + }); |
10 | 57 |
|
11 | 58 | expect(response.status).toBe(400); |
| 59 | + expect(response.body.error).toContain('Amount is required'); |
12 | 60 | }); |
13 | 61 |
|
14 | 62 | it('should reject zero amount', async () => { |
15 | | - const response = await request(app).get('/api/lending/prepare/deposit').send({ |
16 | | - userAddress: 'GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
| 63 | + const response = await request(app).get('/api/lending/prepare/deposit').query({ |
| 64 | + userAddress: VALID_ADDRESS, |
| 65 | + assetAddress: 'G...', |
17 | 66 | amount: '0', |
18 | 67 | }); |
19 | 68 |
|
20 | 69 | expect(response.status).toBe(400); |
| 70 | + expect(response.body.error).toBeDefined(); |
| 71 | + expect(response.body.error).toContain('Amount must be greater than 0'); |
21 | 72 | }); |
22 | 73 |
|
23 | 74 | it('should reject negative amount', async () => { |
24 | | - const response = await request(app).get('/api/lending/prepare/deposit').send({ |
25 | | - userAddress: 'GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
26 | | - amount: '-1000', |
| 75 | + const response = await request(app).get('/api/lending/prepare/deposit').query({ |
| 76 | + userAddress: VALID_ADDRESS, |
| 77 | + assetAddress: 'G...', |
| 78 | + amount: '-1', |
27 | 79 | }); |
28 | 80 |
|
29 | 81 | expect(response.status).toBe(400); |
| 82 | + expect(response.body.error).toBeDefined(); |
| 83 | + expect(response.body.error).toContain('Amount must be greater than 0'); |
30 | 84 | }); |
31 | 85 |
|
32 | 86 | it('should reject invalid operation', async () => { |
33 | | - const response = await request(app).get('/api/lending/prepare/invalid_op').send({ |
34 | | - userAddress: 'GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
| 87 | + const response = await request(app).get('/api/lending/prepare/invalid_op').query({ |
| 88 | + userAddress: 'GDH7NBM22WUCYOLJJZ7ALN3QZ6W2G5YCHDP2YQWJZ76L2GZFPHSYZ4Y3', |
35 | 89 | amount: '1000000', |
36 | 90 | }); |
37 | 91 |
|
38 | 92 | expect(response.status).toBe(400); |
39 | 93 | }); |
40 | | - |
41 | | - it('should not require userSecret', async () => { |
42 | | - // Sending userSecret should not cause a validation error (it's simply ignored) |
43 | | - // The route should still validate normally without it |
44 | | - const response = await request(app) |
45 | | - .get('/api/lending/prepare/deposit') |
46 | | - .send({ amount: '1000000' }); // missing userAddress — should still be 400 |
47 | | - |
48 | | - expect(response.status).toBe(400); |
49 | | - }); |
50 | 94 | }); |
51 | 95 |
|
52 | 96 | describe('Submit Validation (POST /api/lending/submit)', () => { |
|
0 commit comments