test-web-client.js•5.15 kB
// Enhanced test for SpiderFoot web client
import SpiderFootWebClient from './spiderfoot-web-client.js';
import * as cheerio from 'cheerio';
async function testWebClient() {
const client = new SpiderFootWebClient();
const target = 'example.com';
const scanName = `test-scan-${Date.now()}`;
// We'll populate this after getting the available modules
let modules = [];
try {
console.log('=== Testing SpiderFoot Web Client ===');
// 1. List existing scans
console.log('\n1. Listing existing scans...');
const scans = await client.listScans();
console.log('Existing scans:', JSON.stringify(scans, null, 2));
// 2. Get available modules and save the page for debugging
console.log('\n2. Getting available modules...');
const modulesPage = await client.get('/newscan');
// Save the page content for debugging
const fs = await import('fs');
await fs.promises.writeFile('newscan-page.html', modulesPage.data);
console.log('Saved newscan page to newscan-page.html for inspection');
const $ = cheerio.load(modulesPage.data);
// Find all checkboxes with IDs starting with 'type_'
const availableModules = [];
$('input[type="checkbox"][id^="type_"]').each((i, el) => {
const moduleId = $(el).attr('id');
if (moduleId && moduleId.startsWith('type_')) {
availableModules.push(moduleId);
}
});
console.log(`Found ${availableModules.length} available modules`);
// Use the first 2 modules for testing
modules = availableModules.slice(0, 2);
if (modules.length === 0) {
throw new Error('No modules found on the new scan page');
}
console.log(`Using modules for scan:`, modules);
// 3. Start a new scan
console.log(`\n3. Starting a new scan on ${target}...`);
console.log('Target:', target);
console.log('Modules:', modules);
console.log('Scan type: domain');
console.log('Scan name:', scanName);
const scanResult = await client.startScan(target, modules, 'domain', scanName);
if (!scanResult.success) {
console.error('❌ Failed to start scan:', scanResult.error);
if (scanResult.response) {
console.error('Response status:', scanResult.response.status);
console.error('Response data (first 500 chars):',
typeof scanResult.response.data === 'string'
? scanResult.response.data.substring(0, 500)
: JSON.stringify(scanResult.response.data).substring(0, 500));
}
return;
}
const scanId = scanResult.scanId;
console.log(`✅ Scan started with ID: ${scanId}`);
if (!scanId) {
console.log('No scan ID returned, cannot continue with status checks');
return;
}
// 3. Check scan status
console.log('\n3. Checking scan status...');
const status = await client.getScanStatus(scanId);
console.log('Initial scan status:', status);
// 4. Wait for scan to complete (simplified for testing)
console.log('\n4. Waiting for scan to complete (will check up to 10 times with 5s delay)...');
// Simple polling mechanism
const checkStatus = async () => {
const status = await client.getScanStatus(scanId);
console.log(`Current status: ${status.status}`);
return status.status === 'FINISHED' || status.status === 'ERROR' || status.status === 'ABORTED';
};
// Check every 5 seconds, up to 10 times
const maxChecks = 10;
let checks = 0;
let isDone = false;
while (!isDone && checks < maxChecks) {
checks++;
isDone = await checkStatus();
if (!isDone) {
await new Promise(resolve => setTimeout(resolve, 5000));
}
}
if (!isDone) {
console.log('\n⚠️ Scan is taking too long. Stopping checks.');
} else {
console.log('\n✅ Scan completed!');
}
// 5. Get final status and results
console.log('\n5. Getting final scan status...');
const finalStatus = await client.getScanStatus(scanId);
console.log('Final status:', finalStatus);
if (finalStatus.status === 'FINISHED') {
console.log('\n6. Getting scan results...');
try {
const results = await client.getScanResults(scanId, 'summary');
console.log('Results (first 500 chars):',
typeof results === 'string'
? results.substring(0, 500) + (results.length > 500 ? '...' : '')
: JSON.stringify(results, null, 2).substring(0, 500) + '...');
} catch (error) {
console.error('Error getting scan results:', error.message);
}
}
console.log('\n✅ Test completed successfully!');
} catch (error) {
console.error('\n❌ Test failed:', error.message);
if (error.response) {
console.error('Response status:', error.response.status);
console.error('Response data (first 500 chars):',
typeof error.response.data === 'string'
? error.response.data.substring(0, 500)
: JSON.stringify(error.response.data).substring(0, 500));
}
process.exit(1);
}
}
testWebClient();