Có 2 script để test API:
- test_api.py - Script Python chi tiết, chạy từ máy local
- test_api_vps.sh - Bash script nhanh, chạy trực tiếp trên VPS
pip install requestspython test_api.py- ✅ Test health check
- ✅ Test user registration
- ✅ Test user login
- ✅ Test get current user
- ✅ Test admin login
- ✅ Test Nginx proxy
- 🎨 Colored output
- 📊 Detailed summary report
Sửa BASE_URL trong file test_api.py:
BASE_URL = "http://194.59.xxx.xxx:81" # Your VPS IP and portssh root@194.59.xxx.xxx
cd ~/LocalAIChatBox # Hoặc đường dẫn project của bạngit pull origin mainchmod +x test_api_vps.sh./test_api_vps.sh========================================
LocalAIChatBox API Quick Test
========================================
[1/6] Testing Backend Health (Direct)...
✓ Backend Health Check: PASS
{
"status": "healthy",
"service": "Company RAG Chat",
"version": "2.0",
"services": {
"database": "ok",
"ollama": "ok"
}
}
[2/6] Testing API through Nginx...
✓ Nginx Proxy: PASS
[3/6] Testing Database Connection...
✓ Database Connection: PASS
[4/6] Testing Admin Login...
✓ Admin Login: PASS
Token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
[5/6] Testing Get Current User...
✓ Get Current User: PASS
{
"id": 1,
"username": "admin",
"email": "admin@company.local",
"full_name": "Administrator",
"is_admin": true
}
[6/6] Testing User Registration...
✓ User Registration: PASS
========================================
Test Complete
========================================
Nếu không muốn dùng script, test thủ công bằng curl:
curl http://localhost:8001/api/health | python3 -m json.toolcurl -X POST http://localhost:8001/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"admin123"}'curl -X POST http://localhost:8001/api/auth/register \
-H "Content-Type: application/json" \
-d '{
"username": "newuser",
"email": "new@example.com",
"full_name": "New User",
"password": "password123"
}'TOKEN="your_jwt_token_here"
curl http://localhost:8001/api/auth/me \
-H "Authorization: Bearer $TOKEN"Problem: Không kết nối được API
Solutions:
# Kiểm tra containers đang chạy
docker-compose ps
# Xem logs backend
docker-compose logs backend --tail=50
# Restart backend
docker-compose restart backendProblem: Backend lỗi
Solutions:
# Xem logs chi tiết
docker-compose logs backend --tail=100
# Kiểm tra database
docker exec -it ragchat-postgres psql -U raguser -d ragdb -c "SELECT 1"
# Restart cả stack
docker-compose restartProblem: Backend không kết nối được database
Solutions:
# Check postgres running
docker-compose ps postgres
# Check postgres logs
docker-compose logs postgres --tail=50
# Restart postgres và backend
docker-compose restart postgres
sleep 5
docker-compose restart backendProblem: Nginx không proxy đúng
Solutions:
# Check nginx config
docker exec ragchat-nginx cat /etc/nginx/conf.d/default.conf
# Check nginx logs
docker-compose logs nginx --tail=50
# Restart nginx
docker-compose restart nginxNếu tất cả tests pass:
- API đang hoạt động bình thường
- Database kết nối OK
- Auth system hoạt động
- Có thể truy cập frontend và đăng nhập
→ Backend không chạy hoặc crash
→ Check: docker-compose logs backend
→ PostgreSQL không chạy hoặc connection issue
→ Check: docker-compose ps postgres
→ Backend chạy nhưng auth có vấn đề
→ Check: Backend logs, database có user admin không
→ Nginx config sai hoặc backend không accessible
→ Check: nginx/nginx.conf, upstream config
Sau khi tất cả tests pass:
- Test từ browser: Truy cập http://194.59.xxx.xxx:81
- Đăng nhập: Username
admin, Passwordadmin123 - Upload document: Test RAG functionality
- Test chat: Hỏi câu hỏi về documents
- Test admin panel: Quản lý users, rebuild KG
Nếu tests vẫn fail sau khi thử troubleshooting:
- Chụp màn hình output của test script
- Chạy:
docker-compose logs backend --tail=100 > backend.log - Chạy:
docker-compose ps > containers.log - Gửi các file logs để được hỗ trợ
Để monitor API liên tục:
# Run test every 30 seconds
watch -n 30 ./test_api_vps.sh
# Or create a cron job (run every 5 minutes)
*/5 * * * * /path/to/LocalAIChatBox/test_api_vps.sh > /var/log/api_test.log 2>&1