""" Test script for NutriScan Backend API """ import requests import json import time BASE_URL = "http://localhost:5000/api" TEST_USER = { "username": "testuser", "email": "test@example.com", "password": "testpassword123" } def test_health_check(): """Test health check endpoint""" print("Testing health check...") try: response = requests.get(f"{BASE_URL}/health") print(f"Health check: {response.status_code} - {response.json()}") return response.status_code == 200 except requests.exceptions.ConnectionError: print(" Cannot connect to the server. Make sure the backend is running.") return False except Exception as e: print(f" Health check failed: {e}") return False def test_user_registration(): """Test user registration""" print("Testing user registration...") try: response = requests.post(f"{BASE_URL}/auth/register", json=TEST_USER) print(f"Registration: {response.status_code} - {response.json()}") return response.status_code in [200, 201] except Exception as e: print(f" Registration failed: {e}") return False def test_user_login(): """Test user login and return token""" print("Testing user login...") try: login_data = { "username": TEST_USER["username"], "password": TEST_USER["password"] } response = requests.post(f"{BASE_URL}/auth/login", json=login_data) result = response.json() print(f"Login: {response.status_code} - {result}") if response.status_code == 200 and "access_token" in result: return result["access_token"] return None except Exception as e: print(f" Login failed: {e}") return None def test_model_status(token): """Test model status endpoint""" print("Testing model status...") try: headers = {"Authorization": f"Bearer {token}"} response = requests.get(f"{BASE_URL}/inference/model-status", headers=headers) result = response.json() print(f"Model status: {response.status_code} - {result}") return response.status_code == 200 except Exception as e: print(f" Model status check failed: {e}") return False def test_ingredient_analysis(token): """Test ingredient analysis""" print("Testing ingredient analysis...") try: headers = {"Authorization": f"Bearer {token}"} data = { "ingredients": "sugar, palm oil, peanuts, E202 (Potassium sorbate)", "patient_history": "Diabetes" } response = requests.post(f"{BASE_URL}/inference/analyze-ingredients", headers=headers, json=data) result = response.json() print(f"Ingredient analysis: {response.status_code}") if response.status_code == 200: print(f"Analysis result: {result.get('result', {}).get('analysis', 'No analysis')[:100]}...") else: print(f"Error: {result}") return response.status_code == 200 except Exception as e: print(f" Ingredient analysis failed: {e}") return False def test_inference_history(token): """Test inference history""" print("Testing inference history...") try: headers = {"Authorization": f"Bearer {token}"} response = requests.get(f"{BASE_URL}/inference/history", headers=headers) result = response.json() print(f"Inference history: {response.status_code} - Found {len(result.get('history', []))} records") return response.status_code == 200 except Exception as e: print(f" Inference history failed: {e}") return False def main(): """Run all tests""" print(" Starting NutriScan Backend API Tests\n") if not test_health_check(): print(" Server is not running. Please start the backend first.") return print(" Server is running\n") test_user_registration() print() token = test_user_login() if not token: print(" Cannot proceed without authentication token") return print(" Authentication successful\n") if test_model_status(token): print(" Model status check passed\n") else: print(" Model status check failed - model might not be loaded\n") if test_ingredient_analysis(token): print(" Ingredient analysis passed\n") else: print(" Ingredient analysis failed - check model loading\n") if test_inference_history(token): print(" Inference history passed\n") print(" Tests completed!") print("\nTo start the backend:") print(" ./start.sh") print("\nOr manually:") print(" source myenv/bin/activate") print(" python app.py") if __name__ == "__main__": main()