LinkDesk/backend/test_admin_api_project_id.py

204 lines
7.4 KiB
Python

"""
Test script to verify project_id is included in admin API responses.
This script tests the admin API endpoints to ensure project_id is properly
included in the responses for deleted shots and recovery operations.
"""
import sys
import os
sys.path.append('.')
import requests
import json
from database import get_db
from models.shot import Shot
from models.user import User
def get_auth_token():
"""Get authentication token for admin user."""
login_data = {
"email": "admin@vfx.com",
"password": "admin123"
}
response = requests.post("http://localhost:8000/auth/login", json=login_data)
if response.status_code == 200:
return response.json()["access_token"]
else:
print(f"Failed to login: {response.status_code} - {response.text}")
return None
def test_deleted_shots_api():
"""Test the deleted shots API endpoint includes project_id."""
print("Testing /admin/deleted-shots/ API endpoint...")
token = get_auth_token()
if not token:
print("✗ Failed to get auth token")
return False
headers = {"Authorization": f"Bearer {token}"}
try:
response = requests.get("http://localhost:8000/admin/deleted-shots/", headers=headers)
if response.status_code == 200:
deleted_shots = response.json()
print(f"✓ API call successful, found {len(deleted_shots)} deleted shots")
if deleted_shots:
# Check first shot for project_id
first_shot = deleted_shots[0]
if 'project_id' in first_shot:
print(f"✓ project_id field present: {first_shot['project_id']}")
print(f"✓ project_name field present: {first_shot['project_name']}")
return True
else:
print("✗ project_id field missing from API response")
print(f"Available fields: {list(first_shot.keys())}")
return False
else:
print("✓ No deleted shots found, but API structure is correct")
return True
else:
print(f"✗ API call failed: {response.status_code} - {response.text}")
return False
except Exception as e:
print(f"✗ Error testing API: {str(e)}")
return False
def test_recovery_preview_api():
"""Test the recovery preview API endpoint includes project_id."""
print("\nTesting recovery preview API endpoint...")
token = get_auth_token()
if not token:
print("✗ Failed to get auth token")
return False
headers = {"Authorization": f"Bearer {token}"}
try:
# First get list of deleted shots
response = requests.get("http://localhost:8000/admin/deleted-shots/", headers=headers)
if response.status_code == 200:
deleted_shots = response.json()
if deleted_shots:
# Test recovery preview for first deleted shot
shot_id = deleted_shots[0]['id']
print(f"Testing recovery preview for shot ID: {shot_id}")
preview_response = requests.get(
f"http://localhost:8000/admin/shots/{shot_id}/recovery-preview",
headers=headers
)
if preview_response.status_code == 200:
preview_data = preview_response.json()
print("✓ Recovery preview API call successful")
if 'project_id' in preview_data:
print(f"✓ project_id field present: {preview_data['project_id']}")
print(f"✓ project_name field present: {preview_data['project_name']}")
return True
else:
print("✗ project_id field missing from recovery preview response")
print(f"Available fields: {list(preview_data.keys())}")
return False
else:
print(f"✗ Recovery preview API call failed: {preview_response.status_code}")
return False
else:
print("✓ No deleted shots found to test recovery preview")
return True
else:
print(f"✗ Failed to get deleted shots list: {response.status_code}")
return False
except Exception as e:
print(f"✗ Error testing recovery preview API: {str(e)}")
return False
def test_project_filtering():
"""Test project filtering in deleted shots API."""
print("\nTesting project filtering in deleted shots API...")
token = get_auth_token()
if not token:
print("✗ Failed to get auth token")
return False
headers = {"Authorization": f"Bearer {token}"}
try:
# Get all deleted shots
response = requests.get("http://localhost:8000/admin/deleted-shots/", headers=headers)
if response.status_code == 200:
all_deleted_shots = response.json()
print(f"✓ Found {len(all_deleted_shots)} total deleted shots")
if all_deleted_shots:
# Test filtering by project_id
project_id = all_deleted_shots[0]['project_id']
print(f"Testing filter by project_id: {project_id}")
filtered_response = requests.get(
f"http://localhost:8000/admin/deleted-shots/?project_id={project_id}",
headers=headers
)
if filtered_response.status_code == 200:
filtered_shots = filtered_response.json()
print(f"✓ Filtered API call successful, found {len(filtered_shots)} shots")
# Verify all returned shots have the correct project_id
if all(shot['project_id'] == project_id for shot in filtered_shots):
print("✓ All filtered shots have correct project_id")
return True
else:
print("✗ Some filtered shots have incorrect project_id")
return False
else:
print(f"✗ Filtered API call failed: {filtered_response.status_code}")
return False
else:
print("✓ No deleted shots found to test filtering")
return True
else:
print(f"✗ Failed to get deleted shots: {response.status_code}")
return False
except Exception as e:
print(f"✗ Error testing project filtering: {str(e)}")
return False
if __name__ == "__main__":
print("Starting admin API project_id tests...")
print("Note: This test requires the backend server to be running on localhost:8000")
results = []
results.append(test_deleted_shots_api())
results.append(test_recovery_preview_api())
results.append(test_project_filtering())
print(f"\n{'='*60}")
print("Test Results:")
print(f"✓ Passed: {sum(results)}")
print(f"✗ Failed: {len(results) - sum(results)}")
if all(results):
print("\n🎉 All admin API project_id tests passed!")
else:
print("\n❌ Some API tests failed!")
print(f"{'='*60}")