lecoffre-back-mini/test-rattachements-endpoint.js
Sadrinho27 18936f18e7
Some checks failed
Build and Push to Registry / build-and-push (push) Failing after 40s
Remove hardcoded values & updated env file
2025-09-12 11:55:54 +02:00

224 lines
6.5 KiB
JavaScript
Executable File

#!/usr/bin/env node
const fetch = require('node-fetch');
// Test configuration
const BASE_URL = process.env.API_BASE_URL;
const ENDPOINT = '/api/v1/idnot/user'; // Base endpoint, idnot will be added as path parameter
// Test cases for the rattachements endpoint
const testCases = [
{
name: 'Valid IDNot parameter',
idNot: '12345',
expectedStatus: 200,
description: 'Should return rattachements data for valid IDNot'
},
{
name: 'Missing IDNot parameter',
idNot: undefined,
expectedStatus: 404, // Without idnot in path, this should return 404
description: 'Should handle missing IDNot parameter in path'
},
{
name: 'Empty IDNot parameter',
idNot: '',
expectedStatus: 404, // Empty idnot in path should return 404
description: 'Should handle empty IDNot parameter in path'
},
{
name: 'Special characters in IDNot',
idNot: 'test-id_with+special=chars',
expectedStatus: 200,
description: 'Should handle special characters in IDNot'
},
{
name: 'Very long IDNot',
idNot: 'a'.repeat(100),
expectedStatus: 200,
description: 'Should handle very long IDNot values'
}
];
async function testRattachementsEndpoint(testCase) {
console.log(`\n🧪 Testing: ${testCase.name}`);
console.log(`📝 Description: ${testCase.description}`);
console.log(`🆔 IDNot: ${testCase.idNot || 'undefined'}`);
try {
// Build URL with path parameter
let url = `${BASE_URL}${ENDPOINT}`;
if (testCase.idNot !== undefined) {
url += `/${encodeURIComponent(testCase.idNot)}/rattachements`;
} else {
url += '/rattachements'; // Test without idnot parameter
}
console.log(`📍 URL: ${url}`);
const response = await fetch(url, {
method: 'GET',
headers: {
'Accept': 'application/json'
}
});
const responseText = await response.text();
let responseData;
try {
responseData = JSON.parse(responseText);
} catch (e) {
responseData = { rawResponse: responseText };
}
console.log(`📊 Status: ${response.status} ${response.statusText}`);
if (response.status === testCase.expectedStatus) {
console.log(`✅ PASS: Expected status ${testCase.expectedStatus}`);
} else {
console.log(`❌ FAIL: Expected status ${testCase.expectedStatus}, got ${response.status}`);
}
// Analyze the response
if (responseData.error) {
console.log(`🚨 Error: ${responseData.error}`);
if (responseData.message) {
console.log(`📄 Message: ${responseData.message}`);
}
} else if (Array.isArray(responseData)) {
console.log(`📋 Response: Array with ${responseData.length} items`);
if (responseData.length > 0) {
console.log(`🔍 First item keys: ${Object.keys(responseData[0]).join(', ')}`);
}
} else if (typeof responseData === 'object') {
console.log(`📋 Response: Object with keys: ${Object.keys(responseData).join(', ')}`);
if (responseData.result) {
console.log(`🔍 Result type: ${Array.isArray(responseData.result) ? 'Array' : typeof responseData.result}`);
}
} else {
console.log(`📋 Response type: ${typeof responseData}`);
console.log(`📄 Content: ${responseText.substring(0, 200)}${responseText.length > 200 ? '...' : ''}`);
}
return {
testCase,
status: response.status,
expectedStatus: testCase.expectedStatus,
passed: response.status === testCase.expectedStatus,
response: responseData,
url: url
};
} catch (error) {
console.log(`💥 Network Error: ${error.message}`);
return {
testCase,
status: 'NETWORK_ERROR',
expectedStatus: testCase.expectedStatus,
passed: false,
error: error.message,
url: url
};
}
}
async function runTests() {
console.log('🚀 Starting Rattachements Endpoint Tests...\n');
console.log(`📍 Testing against: ${BASE_URL}${ENDPOINT}`);
console.log('=' .repeat(70));
const results = [];
for (const testCase of testCases) {
const result = await testRattachementsEndpoint(testCase);
results.push(result);
// Add a small delay between tests
await new Promise(resolve => setTimeout(resolve, 100));
}
// Summary
console.log('\n' + '=' .repeat(70));
console.log('📊 TEST SUMMARY');
console.log('=' .repeat(70));
const passed = results.filter(r => r.passed).length;
const total = results.length;
console.log(`✅ Passed: ${passed}/${total}`);
console.log(`❌ Failed: ${total - passed}/${total}`);
if (passed === total) {
console.log('🎉 All tests passed!');
} else {
console.log('⚠️ Some tests failed. Check the output above for details.');
}
// Failed tests details
const failedTests = results.filter(r => !r.passed);
if (failedTests.length > 0) {
console.log('\n🔍 FAILED TESTS:');
failedTests.forEach(result => {
console.log(` - ${result.testCase.name}: Expected ${result.testCase.expectedStatus}, got ${result.status}`);
});
}
}
// Check if server is running
async function checkServerHealth() {
try {
const response = await fetch(`${BASE_URL}/api/v1/health`);
if (response.ok) {
console.log('✅ Server is running and responding');
return true;
} else {
console.log(`⚠️ Server responded with status: ${response.status}`);
return false;
}
} catch (error) {
console.log('❌ Server is not responding');
console.log('💡 Make sure to start your server first with: npm run dev');
return false;
}
}
// Main execution
async function main() {
console.log('🔍 Checking server health...');
const serverRunning = await checkServerHealth();
if (!serverRunning) {
console.log('❌ Server health check failed. Exiting.');
process.exit(1);
}
console.log('🚀 Starting tests...');
await runTests();
console.log('🏁 Tests completed.');
}
// Handle command line arguments
if (process.argv.includes('--help') || process.argv.includes('-h')) {
console.log(`
Usage: node test-rattachements-endpoint.js [options]
Options:
--help, -h Show this help message
--url <url> Set custom base URL (default: http://localhost:8080)
Examples:
node test-rattachements-endpoint.js
node test-rattachements-endpoint.js --url http://localhost:3000
`);
process.exit(0);
}
// Parse custom URL if provided
const urlIndex = process.argv.indexOf('--url');
if (urlIndex !== -1 && process.argv[urlIndex + 1]) {
BASE_URL = process.argv[urlIndex + 1];
console.log(`🔧 Using custom URL: ${BASE_URL}`);
}
main().catch(console.error);