voice_detection / test_api.py
ranar110
feat: add README for Voice Detection System API and update API key in test script.
13ab7fe
import requests
import os
# Configuration
BASE_URL = "http://127.0.0.1:8000"
API_KEY = "ap2_b71da4b1-5155-47a2-b522-431fe5cb728d"
TEST_FILE = "test_audio.mp3"
def create_dummy_mp3():
with open(TEST_FILE, "wb") as f:
f.write(b"\x00" * 1024) # 1KB of dummy data
def test_auth_failure():
print("Testing Authentication Failure...")
files = {'file': open(TEST_FILE, 'rb')}
response = requests.post(f"{BASE_URL}/detect", files=files, headers={"x-api-key": "wrong_key"})
if response.status_code == 401:
print("PASS: Auth failure handled correctly.")
else:
print(f"FAIL: Expected 401, got {response.status_code}")
def test_success_flow():
print("Testing Success Flow...")
files = {'file': open(TEST_FILE, 'rb')}
response = requests.post(f"{BASE_URL}/detect", files=files, headers={"x-api-key": API_KEY})
if response.status_code == 200:
data = response.json()
print("PASS: Request successful.")
print("Response:", data)
# Verify structure
if "analysis" in data and "is_human" in data["analysis"]:
print("PASS: JSON structure validated.")
else:
print("FAIL: JSON structure incorrect.")
else:
print(f"FAIL: Expected 200, got {response.status_code}. Text: {response.text}")
if __name__ == "__main__":
create_dummy_mp3()
try:
test_auth_failure()
test_success_flow()
except Exception as e:
print(f"An error occurred: {e}")
finally:
if os.path.exists(TEST_FILE):
os.remove(TEST_FILE)