ironGym/webapp/test-api.js
2025-07-03 02:10:40 +03:00

64 lines
2.3 KiB
JavaScript

/**
* Test script for the member lookup API
* This script tests the encryption/decryption and API functionality
*/
const crypto = require('crypto');
// Same encryption configuration as in the utils
const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY || 'your-32-character-secret-key-here!';
const ALGORITHM = 'aes-256-cbc';
// Encryption function (same as in utils/encryption.ts)
function encryptMemberId(memberId) {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipher(ALGORITHM, ENCRYPTION_KEY);
let encrypted = cipher.update(memberId, 'utf8', 'hex');
encrypted += cipher.final('hex');
return iv.toString('hex') + ':' + encrypted;
}
// Decryption function (same as in utils/encryption.ts)
function decryptMemberId(encryptedMemberId) {
try {
const textParts = encryptedMemberId.split(':');
const iv = Buffer.from(textParts.shift(), 'hex');
const encryptedText = textParts.join(':');
const decipher = crypto.createDecipher(ALGORITHM, ENCRYPTION_KEY);
let decrypted = decipher.update(encryptedText, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
} catch (error) {
throw new Error('Invalid encrypted member ID');
}
}
// Test the encryption/decryption
const testMemberId = '507f1f77bcf86cd799439011'; // Example MongoDB ObjectId
console.log('Original Member ID:', testMemberId);
const encrypted = encryptMemberId(testMemberId);
console.log('Encrypted Member ID:', encrypted);
const decrypted = decryptMemberId(encrypted);
console.log('Decrypted Member ID:', decrypted);
console.log('Encryption/Decryption Test:', testMemberId === decrypted ? 'PASSED' : 'FAILED');
// Test API URL
const apiUrl = `http://localhost:3000/api/member-lookup?encryptedId=${encodeURIComponent(encrypted)}`;
console.log('\nTest API URL:', apiUrl);
console.log('\nTo test the API, make a GET request to the above URL after starting the development server.');
console.log('Expected response format:');
console.log(JSON.stringify({
success: true,
message: "Member information retrieved successfully",
data: {
name: "John Doe",
gender: "m",
planDelay: 1,
planStart: "Mon, 01 Jan 2024 00:00:00 GMT",
planStatus: "active",
planExpAt: "Thu, 01 Feb 2024 00:00:00 GMT"
}
}, null, 2));