108 lines
4.5 KiB
Python
108 lines
4.5 KiB
Python
import requests
|
|
import json
|
|
|
|
BASE_URL = "http://localhost:8000"
|
|
|
|
def test_flow():
|
|
print("--- Starting Full System Test Flow ---")
|
|
uploaded_song_id = None
|
|
listener_username = "listener"
|
|
author_username = "author"
|
|
admin_username = "admin"
|
|
password = "password123"
|
|
|
|
# 1. Author Flow: Upload a song
|
|
print("\n--- Testing Author Flow ---")
|
|
login_author = {"username": author_username, "password": password}
|
|
r = requests.post(f"{BASE_URL}/auth/login", json=login_author)
|
|
if r.status_code != 200:
|
|
print(f"Author Login failed: {r.status_code} - {r.text}")
|
|
return
|
|
|
|
token_author = r.json().get("access_token")
|
|
headers_author = {"Authorization": f"Bearer {token_author}"}
|
|
|
|
# Upload a song
|
|
files = {'file': ('test_song.mp3', b'fake audio content', 'audio/mpeg')}
|
|
data = {'title': 'E2E Test Song', 'genre': 'Pop'}
|
|
r = requests.post(f"{BASE_URL}/songs/upload", headers=headers_author, files=files, data=data)
|
|
print(f"Upload: {r.status_code} - {r.text}")
|
|
if r.status_code == 200 or r.status_code == 201:
|
|
try:
|
|
uploaded_song_id = r.json().get("song_id")
|
|
print(f"Uploaded Song ID: {uploaded_song_id}")
|
|
except Exception as e:
|
|
print(f"Failed to get song id from response: {e}")
|
|
|
|
# 2. Admin Flow: Approve the uploaded song AND ensure listener is active
|
|
print("\n--- Testing Admin Moderation & User Mgmt ---")
|
|
login_admin = {"username": admin_username, "password": password}
|
|
r = requests.post(f"{BASE_URL}/auth/login", json=login_admin)
|
|
if r.status_code != 200:
|
|
print(f"Admin Login failed: {r.status_code} - {r.text}")
|
|
return
|
|
|
|
token_admin = r.json().get("access_token")
|
|
headers_admin = {"Authorization": f"Bearer {token_admin}"}
|
|
|
|
# Activate listener if deactivated
|
|
r = requests.get(f"{BASE_URL}/admin/users", headers=headers_admin)
|
|
if r.status_code == 200:
|
|
users = r.json()
|
|
target_user = next((u for u in users if u['username'] == listener_username), None)
|
|
if target_user and not target_user['is_active']:
|
|
r = requests.post(f"{BASE_URL}/admin/users/{target_user['id']}/toggle-status", headers=headers_admin)
|
|
print(f"Admin Reactivated Listener: {r.status_code}")
|
|
|
|
if uploaded_song_id:
|
|
r = requests.post(f"{BASE_URL}/admin/moderation/{uploaded_song_id}/approve", headers=headers_admin)
|
|
print(f"Approve Song {uploaded_song_id}: {r.status_code} - {r.text}")
|
|
else:
|
|
print("No uploaded song ID available to approve.")
|
|
|
|
# 3. Listener Flow: Discover, Like, and Stream the approved song
|
|
print("\n--- Testing Listener Flow ---")
|
|
login_listener = {"username": listener_username, "password": password}
|
|
r = requests.post(f"{BASE_URL}/auth/login", json=login_listener)
|
|
if r.status_code != 200:
|
|
print(f"Listener Login failed: {r.status_code} - {r.text}")
|
|
return
|
|
|
|
token_listener = r.json().get("access_token")
|
|
headers_listener = {"Authorization": f"Bearer {token_listener}"}
|
|
|
|
# Discover songs
|
|
r = requests.get(f"{BASE_URL}/songs/", headers=headers_listener)
|
|
print(f"Discover: {r.status_code} - {r.text}")
|
|
|
|
if uploaded_song_id:
|
|
# Like the specific song we uploaded (which adds it to Liked Songs)
|
|
r = requests.post(f"{BASE_URL}/songs/{uploaded_song_id}/like", headers=headers_listener)
|
|
print(f"Like Song {uploaded_song_id}: {r.status_code} - {r.text}")
|
|
|
|
# Stream the specific song we uploaded
|
|
r = requests.get(f"{BASE_URL}/songs/{uploaded_song_id}/stream")
|
|
print(f"Stream Song {uploaded_song_id}: {r.status_code}")
|
|
else:
|
|
print("No uploaded song ID available to test saving/streaming.")
|
|
|
|
# 4. Admin Flow: List users and toggle status of listener (instead of deleting, for repeatable tests)
|
|
print("\n--- Testing Admin User Management ---")
|
|
# List users
|
|
r = requests.get(f"{BASE_URL}/admin/users", headers=headers_admin)
|
|
print(f"List Users: {r.status_code} - {r.text}")
|
|
if r.status_code == 200:
|
|
users = r.json()
|
|
target_user = next((u for u in users if u['username'] == listener_username), None)
|
|
if target_user:
|
|
user_id = target_user['id']
|
|
r = requests.post(f"{BASE_URL}/admin/users/{user_id}/toggle-status", headers=headers_admin)
|
|
print(f"Admin Toggle Status User {user_id}: {r.status_code} - {r.text}")
|
|
else:
|
|
print("Target listener user not found.")
|
|
else:
|
|
print("Could not list users.")
|
|
|
|
if __name__ == "__main__":
|
|
test_flow()
|