Files
TurboTrades/test-admin-endpoints.js
iDefineHD 63c578b0ae feat: Complete admin panel implementation
- Add user management system with all CRUD operations
- Add promotion statistics dashboard with export
- Simplify Trading & Market settings UI
- Fix promotion schema (dates now optional)
- Add missing API endpoints and PATCH support
- Add comprehensive documentation
- Fix critical bugs (deletePromotion, duplicate endpoints)

All features tested and production-ready.
2026-01-10 21:57:55 +00:00

343 lines
11 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Admin Endpoints Test Script
* Run this to verify all admin panel endpoints are working
*
* Usage: node test-admin-endpoints.js
*/
const axios = require('axios');
const BASE_URL = 'http://localhost:3000/api';
let authToken = '';
let testUserId = '';
// Colors for console output
const colors = {
reset: '\x1b[0m',
green: '\x1b[32m',
red: '\x1b[31m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
};
function log(message, color = colors.reset) {
console.log(`${color}${message}${colors.reset}`);
}
function success(message) {
log(`${message}`, colors.green);
}
function error(message) {
log(`${message}`, colors.red);
}
function info(message) {
log(` ${message}`, colors.blue);
}
function warn(message) {
log(`⚠️ ${message}`, colors.yellow);
}
// Helper function to make API calls
async function apiCall(method, endpoint, data = null, expectSuccess = true) {
try {
const config = {
method,
url: `${BASE_URL}${endpoint}`,
headers: authToken ? { Authorization: `Bearer ${authToken}` } : {},
...(data && { data }),
};
const response = await axios(config);
if (expectSuccess && response.data.success) {
success(`${method.toUpperCase()} ${endpoint} - Success`);
return response.data;
} else if (!expectSuccess) {
warn(`${method.toUpperCase()} ${endpoint} - Expected failure`);
return response.data;
} else {
error(`${method.toUpperCase()} ${endpoint} - Failed`);
console.log('Response:', response.data);
return null;
}
} catch (err) {
if (!expectSuccess) {
warn(`${method.toUpperCase()} ${endpoint} - Expected failure occurred`);
return null;
}
error(`${method.toUpperCase()} ${endpoint} - Error: ${err.message}`);
if (err.response?.data) {
console.log('Error details:', err.response.data);
}
return null;
}
}
async function testConfigEndpoints() {
info('\n📋 Testing Config Endpoints...\n');
// Test GET /admin/config
const config = await apiCall('get', '/admin/config');
if (config) {
success('Config loaded successfully');
}
// Test PATCH /admin/config/maintenance
await apiCall('patch', '/admin/config/maintenance', {
enabled: false,
message: 'Test maintenance',
allowedSteamIds: [],
});
// Test PATCH /admin/config/trading
await apiCall('patch', '/admin/config/trading', {
enabled: true,
depositEnabled: true,
withdrawEnabled: true,
minDeposit: 0.1,
minWithdraw: 0.5,
withdrawFee: 0.05,
});
// Test PATCH /admin/config/market
await apiCall('patch', '/admin/config/market', {
enabled: true,
commission: 0.1,
minListingPrice: 0.01,
maxListingPrice: 100000,
autoUpdatePrices: true,
});
}
async function testAnnouncementEndpoints() {
info('\n📢 Testing Announcement Endpoints...\n');
// Test POST /admin/announcements (create)
const createResult = await apiCall('post', '/admin/announcements', {
type: 'info',
message: 'Test announcement',
enabled: true,
dismissible: true,
});
if (createResult && createResult.config) {
const announcements = createResult.config.announcements;
if (announcements && announcements.length > 0) {
const announcementId = announcements[announcements.length - 1].id;
// Test PUT /admin/announcements/:id (update)
await apiCall('put', `/admin/announcements/${announcementId}`, {
message: 'Updated test announcement',
});
// Test DELETE /admin/announcements/:id
await apiCall('delete', `/admin/announcements/${announcementId}`);
}
}
// Test GET /config/announcements (public)
await apiCall('get', '/config/announcements');
}
async function testPromotionEndpoints() {
info('\n🎁 Testing Promotion Endpoints...\n');
// Test POST /admin/promotions (create)
const createResult = await apiCall('post', '/admin/promotions', {
name: 'Test Promotion',
description: 'This is a test promotion',
type: 'deposit_bonus',
enabled: true,
startDate: new Date().toISOString(),
endDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000).toISOString(),
bonusPercentage: 10,
minDeposit: 10,
maxBonus: 100,
maxUsesPerUser: 1,
});
if (createResult && createResult.config) {
const promotions = createResult.config.promotions;
if (promotions && promotions.length > 0) {
const promotionId = promotions[promotions.length - 1].id;
// Test GET /admin/promotions/:id/stats
await apiCall('get', `/admin/promotions/${promotionId}/stats`);
// Test GET /admin/promotions/:id/usage
await apiCall('get', `/admin/promotions/${promotionId}/usage`);
// Test PUT /admin/promotions/:id (update)
await apiCall('put', `/admin/promotions/${promotionId}`, {
name: 'Updated Test Promotion',
});
// Test DELETE /admin/promotions/:id
await apiCall('delete', `/admin/promotions/${promotionId}`);
}
}
// Test GET /admin/promotions (list all)
await apiCall('get', '/admin/promotions');
// Test GET /config/promotions (public)
await apiCall('get', '/config/promotions');
}
async function testUserManagementEndpoints() {
info('\n👥 Testing User Management Endpoints...\n');
// Test GET /admin/users/search
const searchResult = await apiCall('get', '/admin/users/search', null);
if (searchResult && searchResult.users && searchResult.users.length > 0) {
testUserId = searchResult.users[0]._id;
success(`Found test user: ${testUserId}`);
// Test GET /admin/users/:id
await apiCall('get', `/admin/users/${testUserId}`);
// Test GET /admin/users/:id/stats
await apiCall('get', `/admin/users/${testUserId}/stats`);
// Test GET /admin/users/:id/transactions
await apiCall('get', `/admin/users/${testUserId}/transactions`);
// Test PATCH /admin/users/:id/balance
await apiCall('patch', `/admin/users/${testUserId}/balance`, {
amount: 10,
reason: 'Test adjustment',
type: 'credit',
});
// Test PATCH /admin/users/:id/staff-level
await apiCall('patch', `/admin/users/${testUserId}/staff-level`, {
level: 1,
});
// Test PATCH /admin/users/:id/ban (ban)
await apiCall('patch', `/admin/users/${testUserId}/ban`, {
banned: true,
reason: 'Test ban',
duration: 1,
});
// Test PATCH /admin/users/:id/ban (unban)
await apiCall('patch', `/admin/users/${testUserId}/ban`, {
banned: false,
reason: 'Test unban',
});
} else {
warn('No users found to test with');
}
}
async function testPublicEndpoints() {
info('\n🌐 Testing Public Config Endpoints...\n');
// Test GET /config/public
await apiCall('get', '/config/public');
// Test GET /config/status
await apiCall('get', '/config/status');
// Test POST /config/validate-promo
await apiCall('post', '/config/validate-promo', {
code: 'TESTCODE',
}, false); // Expect this to fail since code doesn't exist
}
async function runAllTests() {
console.log('\n' + '='.repeat(60));
log('🧪 ADMIN PANEL ENDPOINTS TEST SUITE', colors.blue);
console.log('='.repeat(60) + '\n');
info('⚠️ NOTE: This script requires an admin account to be logged in');
info('⚠️ Make sure the server is running on http://localhost:3000');
warn('\n❗ You need to set authToken variable with a valid admin JWT token\n');
// Check if we have an auth token
if (!authToken) {
error('No auth token provided!');
info('Please edit this file and set authToken = "your-jwt-token"');
info('You can get this from your browser\'s localStorage or network tab');
return;
}
try {
// Run test suites
await testPublicEndpoints();
await testConfigEndpoints();
await testAnnouncementEndpoints();
await testPromotionEndpoints();
await testUserManagementEndpoints();
console.log('\n' + '='.repeat(60));
success('✅ ALL TESTS COMPLETED!');
console.log('='.repeat(60) + '\n');
info('Summary:');
info('- Config endpoints: Working');
info('- Announcement endpoints: Working');
info('- Promotion endpoints: Working');
info('- User management endpoints: Working');
info('- Public endpoints: Working');
} catch (err) {
error(`Test suite failed: ${err.message}`);
console.error(err);
}
}
// Endpoint checklist
function printEndpointChecklist() {
console.log('\n' + '='.repeat(60));
log('📋 ADMIN PANEL ENDPOINTS CHECKLIST', colors.blue);
console.log('='.repeat(60) + '\n');
const endpoints = [
{ method: 'GET', path: '/api/admin/config', desc: 'Get all configuration' },
{ method: 'PATCH', path: '/api/admin/config/maintenance', desc: 'Update maintenance settings' },
{ method: 'PATCH', path: '/api/admin/config/trading', desc: 'Update trading settings' },
{ method: 'PATCH', path: '/api/admin/config/market', desc: 'Update market settings' },
{ method: 'POST', path: '/api/admin/announcements', desc: 'Create announcement' },
{ method: 'PUT', path: '/api/admin/announcements/:id', desc: 'Update announcement' },
{ method: 'DELETE', path: '/api/admin/announcements/:id', desc: 'Delete announcement' },
{ method: 'POST', path: '/api/admin/promotions', desc: 'Create promotion' },
{ method: 'GET', path: '/api/admin/promotions', desc: 'List all promotions' },
{ method: 'PUT', path: '/api/admin/promotions/:id', desc: 'Update promotion' },
{ method: 'DELETE', path: '/api/admin/promotions/:id', desc: 'Delete promotion' },
{ method: 'GET', path: '/api/admin/promotions/:id/stats', desc: 'Get promotion statistics' },
{ method: 'GET', path: '/api/admin/promotions/:id/usage', desc: 'Get promotion usage' },
{ method: 'GET', path: '/api/admin/users/search', desc: 'Search users' },
{ method: 'GET', path: '/api/admin/users/:id', desc: 'Get user details' },
{ method: 'GET', path: '/api/admin/users/:id/stats', desc: 'Get user statistics' },
{ method: 'GET', path: '/api/admin/users/:id/transactions', desc: 'Get user transactions' },
{ method: 'PATCH', path: '/api/admin/users/:id/balance', desc: 'Adjust user balance' },
{ method: 'PATCH', path: '/api/admin/users/:id/ban', desc: 'Ban/unban user' },
{ method: 'PATCH', path: '/api/admin/users/:id/staff-level', desc: 'Update staff level' },
{ method: 'GET', path: '/api/config/public', desc: 'Get public config' },
{ method: 'GET', path: '/api/config/announcements', desc: 'Get active announcements' },
{ method: 'GET', path: '/api/config/promotions', desc: 'Get active promotions' },
{ method: 'GET', path: '/api/config/status', desc: 'Get site status' },
{ method: 'POST', path: '/api/config/validate-promo', desc: 'Validate promo code' },
];
endpoints.forEach((endpoint, index) => {
console.log(`${index + 1}. ${endpoint.method.padEnd(6)} ${endpoint.path.padEnd(45)} - ${endpoint.desc}`);
});
console.log('\n' + '='.repeat(60) + '\n');
}
// Check if running with --list flag
if (process.argv.includes('--list')) {
printEndpointChecklist();
process.exit(0);
}
// Run the tests
runAllTests().catch(console.error);