Preview: test-otp-dev.js
Size: 6.32 KB
/home/byroehnu/easepay.easetack.com/test-otp-dev.js
/**
* OTP.dev API Testing Script
* Tests the real OTP.dev integration with your API key
*/
const fetch = require('node-fetch');
const BASE_URL = 'http://localhost:5004';
const API_KEY = '22d4d23b0ff4a8bd5b9835c2d0ab66da';
// Test configuration
const TEST_PHONE = '2348143640561'; // Your phone number
const OTP_DEV_BASE_URL = 'https://api.otp.dev/v1';
console.log('🧪 === OTP.dev API TESTING ===');
console.log(`📱 Test Phone: ${TEST_PHONE}`);
console.log(`🔑 API Key: ${API_KEY.substring(0, 8)}...`);
console.log('');
/**
* Test OTP.dev API directly
*/
async function testOTPDevDirect() {
try {
console.log('📡 Testing OTP.dev API directly...');
const payload = {
data: {
channel: "sms",
sender: "8d0e3f03-d880-423a-9fdf-89b21235cddd",
phone: TEST_PHONE,
template: "b9acb7c4-74f5-45e3-9628-240c763cf9b9",
code_length: 6
}
};
const response = await fetch(`${OTP_DEV_BASE_URL}/verifications`, {
method: 'POST',
headers: {
'X-OTP-Key': API_KEY,
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(payload)
});
const result = await response.json();
if (response.ok) {
console.log('✅ Direct OTP.dev API SUCCESS');
console.log(`📋 Response:`, JSON.stringify(result, null, 2));
return result;
} else {
console.log('❌ Direct OTP.dev API FAILED');
console.log(`📋 Error:`, JSON.stringify(result, null, 2));
return null;
}
} catch (error) {
console.error('❌ Direct API Error:', error.message);
return null;
}
}
/**
* Test sending OTP through your backend
*/
async function testSendOTP() {
try {
console.log('📤 Testing OTP send via backend...');
const response = await fetch(`${BASE_URL}/api/auth/send-otp`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
phone: TEST_PHONE,
method: 'sms'
})
});
const result = await response.json();
if (response.ok && result.success) {
console.log('✅ Backend OTP Send SUCCESS');
console.log(`📋 Response:`, JSON.stringify(result, null, 2));
return result.verificationId;
} else {
console.log('❌ Backend OTP Send FAILED');
console.log(`📋 Error:`, JSON.stringify(result, null, 2));
return null;
}
} catch (error) {
console.error('❌ Backend OTP Send Error:', error.message);
return null;
}
}
/**
* Test OTP verification
*/
async function testVerifyOTP(verificationId) {
try {
console.log('🔐 Testing OTP verification...');
// Get OTP from user input
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
const otp = await new Promise((resolve) => {
readline.question('📱 Enter the OTP you received: ', (answer) => {
readline.close();
resolve(answer.trim());
});
});
if (!otp || otp.length !== 6) {
console.log('❌ Invalid OTP format. Must be 6 digits.');
return false;
}
const response = await fetch(`${BASE_URL}/api/auth/verify-otp`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
phone: TEST_PHONE,
otp: otp,
verificationId: verificationId
})
});
const result = await response.json();
if (response.ok && result.success) {
console.log('✅ OTP Verification SUCCESS');
console.log(`📋 Response:`, JSON.stringify(result, null, 2));
return true;
} else {
console.log('❌ OTP Verification FAILED');
console.log(`📋 Error:`, JSON.stringify(result, null, 2));
return false;
}
} catch (error) {
console.error('❌ OTP Verification Error:', error.message);
return false;
}
}
/**
* Test account info (if supported)
*/
async function testAccountInfo() {
try {
console.log('💰 Testing account info...');
const response = await fetch(`${OTP_DEV_BASE_URL}/account`, {
method: 'GET',
headers: {
'X-OTP-Key': API_KEY,
'Accept': 'application/json',
}
});
const result = await response.json();
if (response.ok) {
console.log('✅ Account Info SUCCESS');
console.log(`📋 Account:`, JSON.stringify(result, null, 2));
} else {
console.log('ℹ️ Account info not available or not supported');
console.log(`📋 Response:`, JSON.stringify(result, null, 2));
}
} catch (error) {
console.error('ℹ️ Account info error (may not be supported):', error.message);
}
}
/**
* Main testing function
*/
async function runTests() {
console.log('🚀 Starting comprehensive OTP.dev tests...\n');
// Test 1: Direct API call
console.log('=== TEST 1: Direct OTP.dev API ===');
const directResult = await testOTPDevDirect();
console.log('');
if (!directResult) {
console.log('❌ Direct API failed, skipping backend tests');
return;
}
// Test 2: Account info
console.log('=== TEST 2: Account Information ===');
await testAccountInfo();
console.log('');
// Test 3: Backend OTP send
console.log('=== TEST 3: Backend OTP Send ===');
const verificationId = await testSendOTP();
console.log('');
if (!verificationId) {
console.log('❌ Backend OTP send failed, skipping verification test');
return;
}
// Test 4: OTP verification
console.log('=== TEST 4: OTP Verification ===');
await testVerifyOTP(verificationId);
console.log('');
console.log('🎉 Testing completed!');
}
// Handle command line arguments
const args = process.argv.slice(2);
if (args.includes('--direct-only')) {
console.log('Running direct API test only...\n');
testOTPDevDirect();
} else if (args.includes('--account-only')) {
console.log('Running account info test only...\n');
testAccountInfo();
} else {
// Run all tests
runTests();
}
// Export for use in other scripts
module.exports = {
testOTPDevDirect,
testSendOTP,
testVerifyOTP,
testAccountInfo
};
Directory Contents
Dirs: 15 × Files: 17