|
1 | 1 | const request = require('supertest'); |
| 2 | +const { |
| 3 | + app, |
| 4 | + listings, |
| 5 | + resetListings, |
| 6 | + hasPositiveTrustline, |
| 7 | +} = require('../index'); |
| 8 | + |
| 9 | +describe('LeaseFlow Backend API', () => { |
| 10 | + beforeEach(() => { |
| 11 | + resetListings(); |
| 12 | + global.fetch = jest.fn(); |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + jest.resetAllMocks(); |
| 17 | + }); |
| 18 | + |
2 | 19 |
|
3 | 20 | const { createApp } = require('../index'); |
4 | 21 | const { loadConfig } = require('../src/config'); |
@@ -87,6 +104,187 @@ describe('LeaseFlow Backend API', () => { |
87 | 104 | }); |
88 | 105 | }); |
89 | 106 |
|
| 107 | + it('should require listing fields on POST /listings', async () => { |
| 108 | + const response = await request(app).post('/listings').send({ lister: 'GABC' }); |
| 109 | + |
| 110 | + expect(response.status).toBe(400); |
| 111 | + expect(response.body).toEqual({ |
| 112 | + error: 'Missing required fields', |
| 113 | + required: ['lister', 'assetCode', 'assetIssuer', 'price'], |
| 114 | + }); |
| 115 | + expect(listings).toHaveLength(0); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should create a listing when Horizon shows a positive trustline', async () => { |
| 119 | + global.fetch.mockResolvedValue({ |
| 120 | + ok: true, |
| 121 | + status: 200, |
| 122 | + json: async () => ({ |
| 123 | + balances: [ |
| 124 | + { |
| 125 | + asset_type: 'credit_alphanum12', |
| 126 | + asset_code: 'LEASE-NFT-1', |
| 127 | + asset_issuer: 'GISSUER123', |
| 128 | + balance: '1.0000000', |
| 129 | + }, |
| 130 | + ], |
| 131 | + }), |
| 132 | + }); |
| 133 | + |
| 134 | + const response = await request(app).post('/listings').send({ |
| 135 | + lister: 'GLISTER123', |
| 136 | + assetCode: 'LEASE-NFT-1', |
| 137 | + assetIssuer: 'GISSUER123', |
| 138 | + price: '1500', |
| 139 | + metadata: { leaseId: 'lease-001' }, |
| 140 | + }); |
| 141 | + |
| 142 | + expect(response.status).toBe(201); |
| 143 | + expect(response.body.listing).toMatchObject({ |
| 144 | + lister: 'GLISTER123', |
| 145 | + assetCode: 'LEASE-NFT-1', |
| 146 | + assetIssuer: 'GISSUER123', |
| 147 | + price: '1500', |
| 148 | + metadata: { leaseId: 'lease-001' }, |
| 149 | + }); |
| 150 | + expect(listings).toHaveLength(1); |
| 151 | + expect(global.fetch).toHaveBeenCalledWith( |
| 152 | + 'https://horizon.stellar.org/accounts/GLISTER123' |
| 153 | + ); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should reject listings when the trustline is missing', async () => { |
| 157 | + global.fetch.mockResolvedValue({ |
| 158 | + ok: true, |
| 159 | + status: 200, |
| 160 | + json: async () => ({ |
| 161 | + balances: [ |
| 162 | + { |
| 163 | + asset_type: 'native', |
| 164 | + balance: '100.0', |
| 165 | + }, |
| 166 | + ], |
| 167 | + }), |
| 168 | + }); |
| 169 | + |
| 170 | + const response = await request(app).post('/listings').send({ |
| 171 | + lister: 'GLISTER456', |
| 172 | + assetCode: 'LEASE-NFT-2', |
| 173 | + assetIssuer: 'GISSUER456', |
| 174 | + price: '2200', |
| 175 | + }); |
| 176 | + |
| 177 | + expect(response.status).toBe(403); |
| 178 | + expect(response.body).toEqual({ |
| 179 | + error: 'Lister does not own the NFT on-chain', |
| 180 | + reason: 'TRUSTLINE_MISSING_OR_EMPTY', |
| 181 | + }); |
| 182 | + expect(listings).toHaveLength(0); |
| 183 | + }); |
| 184 | + |
| 185 | + it('should reject listings when the trustline exists with zero balance', async () => { |
| 186 | + global.fetch.mockResolvedValue({ |
| 187 | + ok: true, |
| 188 | + status: 200, |
| 189 | + json: async () => ({ |
| 190 | + balances: [ |
| 191 | + { |
| 192 | + asset_type: 'credit_alphanum12', |
| 193 | + asset_code: 'LEASE-NFT-3', |
| 194 | + asset_issuer: 'GISSUER789', |
| 195 | + balance: '0.0000000', |
| 196 | + }, |
| 197 | + ], |
| 198 | + }), |
| 199 | + }); |
| 200 | + |
| 201 | + const response = await request(app).post('/listings').send({ |
| 202 | + lister: 'GLISTER789', |
| 203 | + assetCode: 'LEASE-NFT-3', |
| 204 | + assetIssuer: 'GISSUER789', |
| 205 | + price: '3000', |
| 206 | + }); |
| 207 | + |
| 208 | + expect(response.status).toBe(403); |
| 209 | + expect(response.body.reason).toBe('TRUSTLINE_MISSING_OR_EMPTY'); |
| 210 | + expect(listings).toHaveLength(0); |
| 211 | + }); |
| 212 | + |
| 213 | + it('should reject listings when Horizon account lookup returns 404', async () => { |
| 214 | + global.fetch.mockResolvedValue({ |
| 215 | + ok: false, |
| 216 | + status: 404, |
| 217 | + }); |
| 218 | + |
| 219 | + const response = await request(app).post('/listings').send({ |
| 220 | + lister: 'GMISSINGACCOUNT', |
| 221 | + assetCode: 'LEASE-NFT-4', |
| 222 | + assetIssuer: 'GISSUER404', |
| 223 | + price: '3500', |
| 224 | + }); |
| 225 | + |
| 226 | + expect(response.status).toBe(403); |
| 227 | + expect(response.body.reason).toBe('ACCOUNT_NOT_FOUND'); |
| 228 | + expect(listings).toHaveLength(0); |
| 229 | + }); |
| 230 | + |
| 231 | + it('should surface Horizon failures without inserting a listing', async () => { |
| 232 | + global.fetch.mockResolvedValue({ |
| 233 | + ok: false, |
| 234 | + status: 503, |
| 235 | + }); |
| 236 | + |
| 237 | + const response = await request(app).post('/listings').send({ |
| 238 | + lister: 'GUPSTREAMFAIL', |
| 239 | + assetCode: 'LEASE-NFT-5', |
| 240 | + assetIssuer: 'GISSUER500', |
| 241 | + price: '4100', |
| 242 | + }); |
| 243 | + |
| 244 | + expect(response.status).toBe(502); |
| 245 | + expect(response.body).toEqual({ |
| 246 | + error: 'Unable to verify ownership against Horizon', |
| 247 | + details: 'Horizon lookup failed with status 503', |
| 248 | + }); |
| 249 | + expect(listings).toHaveLength(0); |
| 250 | + }); |
| 251 | + |
| 252 | + it('should detect matching trustlines with positive balances only', () => { |
| 253 | + expect( |
| 254 | + hasPositiveTrustline( |
| 255 | + [ |
| 256 | + { |
| 257 | + asset_type: 'credit_alphanum12', |
| 258 | + asset_code: 'LEASE-NFT-6', |
| 259 | + asset_issuer: 'GISSUERTRUST', |
| 260 | + balance: '1.0000000', |
| 261 | + }, |
| 262 | + { |
| 263 | + asset_type: 'credit_alphanum12', |
| 264 | + asset_code: 'LEASE-NFT-6', |
| 265 | + asset_issuer: 'GISSUERTRUST', |
| 266 | + balance: '0.0000000', |
| 267 | + }, |
| 268 | + ], |
| 269 | + 'LEASE-NFT-6', |
| 270 | + 'GISSUERTRUST' |
| 271 | + ) |
| 272 | + ).toBe(true); |
| 273 | + |
| 274 | + expect( |
| 275 | + hasPositiveTrustline( |
| 276 | + [ |
| 277 | + { |
| 278 | + asset_type: 'credit_alphanum12', |
| 279 | + asset_code: 'LEASE-NFT-6', |
| 280 | + asset_issuer: 'GISSUERTRUST', |
| 281 | + balance: '0.0000000', |
| 282 | + }, |
| 283 | + ], |
| 284 | + 'LEASE-NFT-6', |
| 285 | + 'GISSUERTRUST' |
| 286 | + ) |
| 287 | + ).toBe(false); |
90 | 288 | it('generates a proposal for an active lease expiring in 60 days', () => { |
91 | 289 | seedEligibleLease(); |
92 | 290 |
|
|
0 commit comments