Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 223 additions & 0 deletions stellar-payment-platform/integration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
'use strict';

// Mock cleanup-cron so background cron doesn't interfere during tests
jest.mock('./src/cleanup-cron', () => ({ scheduleCleanupJob: jest.fn() }));

// Mock @stellar/stellar-sdk to prevent Jest from trying to parse ESM dependencies
jest.mock('@stellar/stellar-sdk', () => ({
Horizon: { Server: jest.fn() },
StrKey: { isValidEd25519PublicKey: jest.fn(() => true) },
}));

jest.mock('pdfkit', () => jest.fn());

// ---------------------------------------------------------------------------
// In-memory mock database for integration lifecycle tests
// ---------------------------------------------------------------------------
const mockDb = new Map();

jest.mock('./prismaClient', () => {
const prisma = {
user: {
findUnique: jest.fn(async ({ where, select }) => {
let row = null;
if (where.username) {
for (const entry of mockDb.values()) {
if (entry.username === where.username) {
row = entry;
break;
}
}
} else if (where.address) {
row = mockDb.get(where.address) || null;
}
if (!row) return null;
if (select) {
const result = {};
for (const key of Object.keys(select)) {
if (select[key]) result[key] = row[key];
}
return result;
}
return { ...row };
}),
findFirst: jest.fn(async ({ where, select }) => {
const addr = where?.address?.equals;
if (!addr) return null;
for (const entry of mockDb.values()) {
if (entry.address.toLowerCase() === addr.toLowerCase()) {
if (select) {
const result = {};
for (const key of Object.keys(select)) {
if (select[key]) result[key] = entry[key];
}
return result;
}
return { ...entry };
}
}
return null;
}),
findMany: jest.fn(async ({ where, skip, take } = {}) => {
let results = Array.from(mockDb.values());
if (where?.OR) {
results = results.filter((row) =>
where.OR.some((cond) => {
for (const [field, filter] of Object.entries(cond)) {
if (filter.contains) {
const hay = row[field] || '';
const needle = filter.contains;
if (filter.mode === 'insensitive') {
return hay.toLowerCase().includes(needle.toLowerCase());
}
return hay.includes(needle);
}
}
return false;
}),
);
}
if (typeof skip === 'number') results = results.slice(skip);
if (typeof take === 'number') results = results.slice(0, take);
return results;
}),
count: jest.fn(async ({ where } = {}) => {
if (!where) return mockDb.size;
let results = Array.from(mockDb.values());
if (where?.OR) {
results = results.filter((row) =>
where.OR.some((cond) => {
for (const [field, filter] of Object.entries(cond)) {
if (filter.contains) {
const hay = row[field] || '';
const needle = filter.contains;
if (filter.mode === 'insensitive') {
return hay.toLowerCase().includes(needle.toLowerCase());
}
return hay.includes(needle);
}
}
return false;
}),
);
}
return results.length;
}),
create: jest.fn(async ({ data }) => {
for (const entry of mockDb.values()) {
if (entry.username === data.username) {
const err = new Error('Unique constraint failed on the fields: (`username`)');
err.code = 'SQLITE_CONSTRAINT';
throw err;
}
}
const row = {
username: data.username,
address: data.address,
memoType: data.memoType || null,
memo: data.memo || null,
createdAt: new Date(),
};
mockDb.set(data.address, row);
return row;
}),
},
$transaction: jest.fn(async (ops) => Promise.all(ops)),
$disconnect: jest.fn().mockResolvedValue(undefined),
};
return { prisma };
});

// Mock multi-signer verifier (loaded by v1 userRoutes)
jest.mock('./src/multisigner-verifier', () => ({
verifyMultiSignerThreshold: jest.fn().mockResolvedValue({ success: true }),
isSingleSignerAccount: jest.fn().mockReturnValue(true),
}));

const request = require('supertest');
const { app } = require('./server');

describe('API Integration Lifecycle Suite', () => {
const user1 = {
username: 'integration_user*localhost',
address: 'GAPUQZH3WZUXHEMUGZN5ZYU4D4GHCFEMOGUINU6MF345GBD2QXNYYIEQ',
};

const user2 = {
username: 'integration_user*localhost',
address: 'GBDQD3WTQ6W2VQ2W4V74UZ5WYF6B72GZ6EHD7I3L3WYH357Y4K5H3E4W',
};

beforeAll(() => {
// Clear mock database rows automatically before running
mockDb.clear();
});

afterAll(() => {
// Clear mock database rows automatically after running
mockDb.clear();
});

test('Full lifecycle: register a user, query the user, and attempt to register a duplicate username', async () => {
// 1. Register a user hitting /api/v1/register
const registerRes = await request(app)
.post('/api/v1/register')
.send({
username: user1.username,
address: user1.address,
});

expect(registerRes.status).toBe(201);
expect(registerRes.body).toMatchObject({
ok: true,
username: user1.username.toLowerCase(),
address: user1.address,
});

// 2. Query the user via /api/v1/lookup
const queryRes = await request(app)
.get(`/api/v1/lookup?address=${user1.address}`);

expect(queryRes.status).toBe(200);
expect(queryRes.body).toMatchObject({
username: user1.username.toLowerCase(),
address: user1.address,
});

// Also query the user via search parameter on /api/v1/lookup
const searchRes = await request(app)
.get('/api/v1/lookup?search=integration_user');

expect(searchRes.status).toBe(200);
expect(searchRes.body.data).toEqual(
expect.arrayContaining([
expect.objectContaining({
username: user1.username.toLowerCase(),
address: user1.address,
}),
]),
);

// Also query the user via /api/v1/federation
const fedRes = await request(app)
.get(`/api/v1/federation?q=${user1.username}&type=name`);

expect(fedRes.status).toBe(200);
expect(fedRes.body).toMatchObject({
stellar_address: user1.address,
account_id: user1.address,
});

// 3. Attempt to register a duplicate username hitting /api/v1/register
const duplicateRes = await request(app)
.post('/api/v1/register')
.send({
username: user2.username,
address: user2.address,
});

// Assess expected JSON status returns (409 Conflict for duplicate username)
expect(duplicateRes.status).toBe(409);
expect(duplicateRes.body).toHaveProperty("error");
});
});
13 changes: 9 additions & 4 deletions stellar-payment-platform/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,9 @@ app.use((err, _req, _res, next) => {
next(err);
});

app.use((err, _req, res, next) => {
// Global error handling middleware
// eslint-disable-next-line no-unused-vars
app.use((err, _req, res, _next) => {
const statusCode = err.statusCode || 500;
const errorMessage = err.message || 'Internal server error';

Expand All @@ -531,12 +533,11 @@ app.use((err, _req, res, next) => {
return res.status(statusCode).json({
success: false,
error: errorMessage,
statusCode,
statusCode: statusCode,
});
});

const SHUTDOWN_TIMEOUT_MS = parseInt(process.env.SHUTDOWN_TIMEOUT_MS, 10) || 10_000;

let isShuttingDown = false;

const gracefulShutdown = (server, pool, signal) => {
Expand All @@ -561,6 +562,9 @@ const gracefulShutdown = (server, pool, signal) => {
process.exit(0);
});
};



app.use((err, req, res) => {
console.error(err.stack);
const statusCode = err.statusCode || 500;
Expand All @@ -571,6 +575,7 @@ app.use((err, req, res) => {
detail: process.env.NODE_ENV === 'development' ? err.stack : 'Check server logs for details'
});
});

if (require.main === module) {
const server = app.listen(PORT, '0.0.0.0', () => {
console.log(`Server successfully initialized on port ${PORT}`);
Expand All @@ -592,4 +597,4 @@ if (require.main === module) {
process.on('SIGINT', (sig) => gracefulShutdown(server, prismaPool, sig));
}

module.exports = { app, prisma, gracefulShutdown, rejectNestedObjects };
module.exports = { app, gracefulShutdown, rejectNestedObjects };
Loading