File size: 1,596 Bytes
37c6d1c
 
 
 
 
13ab7fe
37c6d1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)