-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test.js
More file actions
122 lines (102 loc) · 3.44 KB
/
api_test.js
File metadata and controls
122 lines (102 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Test script para verificar la API y autenticación
const API_URL = 'http://127.0.0.1:8001/api/v1';
// Test 1: Login con credenciales admin
async function testLogin() {
try {
console.log('🔐 Testing login...');
// El backend espera form data, no JSON
const formData = new URLSearchParams();
formData.append('username', 'dlaurenap@gmail.com'); // username, no correo
formData.append('password', 'Sendai2011');
const response = await fetch(`${API_URL}/auth/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: formData
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Login failed: ${response.status} ${response.statusText} - ${errorText}`);
}
const data = await response.json();
console.log('✅ Login successful');
console.log('Token:', data.access_token ? 'Present' : 'Missing');
return data.access_token;
} catch (error) {
console.error('❌ Login failed:', error.message);
return null;
}
}
// Test 2: Get tutorials con autenticación
async function testTutorials(token) {
try {
console.log('\n📚 Testing tutorials endpoint...');
const response = await fetch(`${API_URL}/tutoriales/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
});
if (!response.ok) {
throw new Error(`Tutorials request failed: ${response.status} ${response.statusText}`);
}
const tutorials = await response.json();
console.log('✅ Tutorials loaded successfully');
console.log(`Found ${tutorials.length} tutorials`);
if (tutorials.length > 0) {
console.log('First tutorial:', {
id: tutorials[0].id,
nombre: tutorials[0].nombre,
descripcion: tutorials[0].descripcion?.substring(0, 50) + '...'
});
}
return tutorials;
} catch (error) {
console.error('❌ Tutorials request failed:', error.message);
return null;
}
}
// Test 3: Health check
async function testHealthCheck() {
try {
console.log('\n🏥 Testing health check...');
const response = await fetch(`${API_URL}/health-check`);
if (!response.ok) {
throw new Error(`Health check failed: ${response.status}`);
}
console.log('✅ Backend is healthy');
return true;
} catch (error) {
console.error('❌ Health check failed:', error.message);
return false;
}
}
// Ejecutar todos los tests
async function runTests() {
console.log('🚀 Starting API tests...\n');
// Test health check first
const isHealthy = await testHealthCheck();
if (!isHealthy) {
console.log('❌ Backend is not responding. Make sure it\'s running on port 8001');
return;
}
// Test login
const token = await testLogin();
if (!token) {
console.log('❌ Cannot proceed without authentication token');
return;
}
// Test tutorials
const tutorials = await testTutorials(token);
console.log('\n📊 Test Summary:');
console.log(`- Backend Health: ${isHealthy ? '✅' : '❌'}`);
console.log(`- Authentication: ${token ? '✅' : '❌'}`);
console.log(`- Tutorials Loading: ${tutorials ? '✅' : '❌'}`);
if (tutorials && tutorials.length === 0) {
console.log('\n⚠️ No tutorials found in database. You may need to create some tutorials first.');
}
}
// Run the tests
runTests().catch(console.error);