Files
whattoplay/features/keycrow/tests/api.test.ts
2026-03-10 17:03:13 +01:00

95 lines
2.8 KiB
TypeScript

import request from 'supertest';
import app from '../src/index';
describe('KeyCrow API', () => {
let sellerId: string;
let buyerId: string;
let listingId: string;
let transactionId: string;
const testKey = 'ABCD-EFGH-IJKL-MNOP';
describe('Auth Flow', () => {
it('should register a seller', async () => {
const res = await request(app)
.post('/auth/register')
.send({ username: 'seller1', email: 'seller@test.com' });
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
sellerId = res.body.data.user.id;
});
it('should register a buyer with steam', async () => {
const res = await request(app)
.post('/auth/auth/steam/login')
.send({ steamId: '76561198000000001', username: 'buyer1' });
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
buyerId = res.body.data.user.id;
});
});
describe('Listings Flow', () => {
it('should create a listing', async () => {
const res = await request(app)
.post('/listings')
.set('x-user-id', sellerId)
.send({
gameTitle: 'Test Game',
platform: 'STEAM',
price: 9.99,
currency: 'EUR',
key: testKey,
});
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
listingId = res.body.data.listing.id;
});
it('should get active listings', async () => {
const res = await request(app).get('/listings');
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.data.listings.length).toBeGreaterThan(0);
});
});
describe('Transaction Flow (Escrow)', () => {
it('should create a purchase with escrow hold', async () => {
const res = await request(app)
.post('/transactions')
.set('x-user-id', buyerId)
.send({ listingId });
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.data.transaction.escrowStatus).toBe('HELD');
transactionId = res.body.data.transaction.id;
});
it('should deliver key to buyer', async () => {
const res = await request(app)
.get(`/transactions/${transactionId}/key`)
.set('x-user-id', buyerId);
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.data.key).toBe(testKey);
});
it('should confirm success and release escrow', async () => {
const res = await request(app)
.post(`/transactions/${transactionId}/confirm`)
.set('x-user-id', buyerId)
.send({ status: 'SUCCESS' });
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.data.escrowStatus).toBe('RELEASED');
});
});
});