|
| 1 | +import { describe, it, expect, beforeAll } from 'vitest'; |
| 2 | +import request from 'supertest'; |
| 3 | +import { app } from '../index.js'; |
| 4 | + |
| 5 | +const VALID_ADDRESS = 'GBAHQCUPC7G2B4D2F2I2K2M2O2Q2S2U2W2Y2A2C2E2G2I2K2M2O2Q2S2'; |
| 6 | +const INVALID_ADDRESS = 'invalid-stellar-address'; |
| 7 | + |
| 8 | +describe('Stellar Address Validation', () => { |
| 9 | + beforeAll(() => { |
| 10 | + process.env.NODE_ENV = 'test'; |
| 11 | + }); |
| 12 | + |
| 13 | + describe('POST /api/risk/evaluate', () => { |
| 14 | + it('should accept a valid Stellar address', async () => { |
| 15 | + const res = await request(app) |
| 16 | + .post('/api/risk/evaluate') |
| 17 | + .send({ walletAddress: VALID_ADDRESS }); |
| 18 | + expect(res.status).toBe(200); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should reject an invalid Stellar address', async () => { |
| 22 | + const res = await request(app) |
| 23 | + .post('/api/risk/evaluate') |
| 24 | + .send({ walletAddress: INVALID_ADDRESS }); |
| 25 | + expect(res.status).toBe(400); |
| 26 | + expect(res.body.error).toBe('Validation failed'); |
| 27 | + expect(res.body.details[0].message).toBe('Invalid Stellar wallet address'); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('POST /api/credit/lines', () => { |
| 32 | + it('should accept a valid Stellar address', async () => { |
| 33 | + const res = await request(app) |
| 34 | + .post('/api/credit/lines') |
| 35 | + .send({ walletAddress: VALID_ADDRESS, requestedLimit: '1000' }); |
| 36 | + expect(res.status).toBe(201); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should reject an invalid Stellar address', async () => { |
| 40 | + const res = await request(app) |
| 41 | + .post('/api/credit/lines') |
| 42 | + .send({ walletAddress: INVALID_ADDRESS, requestedLimit: '1000' }); |
| 43 | + expect(res.status).toBe(400); |
| 44 | + expect(res.body.error).toBe('Validation failed'); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('GET /api/credit/wallet/:walletAddress/lines', () => { |
| 49 | + it('should accept a valid Stellar address', async () => { |
| 50 | + const res = await request(app).get(`/api/credit/wallet/${VALID_ADDRESS}/lines`); |
| 51 | + expect(res.status).toBe(200); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should reject an invalid Stellar address', async () => { |
| 55 | + const res = await request(app).get(`/api/credit/wallet/${INVALID_ADDRESS}/lines`); |
| 56 | + expect(res.status).toBe(400); |
| 57 | + expect(res.body.error).toBe('Validation failed'); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('GET /api/risk/wallet/:walletAddress/latest', () => { |
| 62 | + it('should accept a valid Stellar address', async () => { |
| 63 | + const res = await request(app).get(`/api/risk/wallet/${VALID_ADDRESS}/latest`); |
| 64 | + // It might return 404 if not found, but it should pass validation |
| 65 | + expect([200, 404]).toContain(res.status); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should reject an invalid Stellar address', async () => { |
| 69 | + const res = await request(app).get(`/api/risk/wallet/${INVALID_ADDRESS}/latest`); |
| 70 | + expect(res.status).toBe(400); |
| 71 | + expect(res.body.error).toBe('Validation failed'); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('GET /api/risk/wallet/:walletAddress/history', () => { |
| 76 | + it('should accept a valid Stellar address', async () => { |
| 77 | + const res = await request(app).get(`/api/risk/wallet/${VALID_ADDRESS}/history`); |
| 78 | + expect(res.status).toBe(200); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should reject an invalid Stellar address', async () => { |
| 82 | + const res = await request(app).get(`/api/risk/wallet/${INVALID_ADDRESS}/history`); |
| 83 | + expect(res.status).toBe(400); |
| 84 | + expect(res.body.error).toBe('Validation failed'); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments