105 lines
4.6 KiB
Python
105 lines
4.6 KiB
Python
import requests
|
|
import json
|
|
|
|
BASE_URL = "http://localhost:8000"
|
|
|
|
def get_token(username, password):
|
|
r = requests.post(f"{BASE_URL}/auth/login", json={"username": username, "password": password})
|
|
if r.status_code != 200:
|
|
print(f"Login failed for {username}: {r.status_code} - {r.text}")
|
|
return None
|
|
return r.json().get("access_token")
|
|
|
|
def test_comprehensive():
|
|
print("=== Starting Comprehensive E2E Verification ===\n")
|
|
|
|
password = "password123"
|
|
admin_user = "admin"
|
|
author_user = "author"
|
|
listener_user = "listener"
|
|
|
|
# 1. ADMIN: Login & User Management
|
|
print("--- Testing Admin Role ---")
|
|
token_admin = get_token(admin_user, password)
|
|
if not token_admin: return
|
|
headers_admin = {"Authorization": f"Bearer {token_admin}"}
|
|
|
|
# List users and toggle status
|
|
r = requests.get(f"{BASE_URL}/admin/users", headers=headers_admin)
|
|
if r.status_code == 200:
|
|
users = r.json()
|
|
listener = next((u for u in users if u['username'] == listener_user), None)
|
|
if listener:
|
|
# Toggle status twice to return it to original state but verify it works
|
|
r = requests.post(f"{BASE_URL}/admin/users/{listener['id']}/toggle-status", headers=headers_admin)
|
|
print(f"Admin toggle user status (1): {r.status_code}")
|
|
r = requests.post(f"{BASE_URL}/admin/users/{listener['id']}/toggle-status", headers=headers_admin)
|
|
print(f"Admin toggle user status (2): {r.status_code}")
|
|
r = requests.post(f"{BASE_URL}/admin/users/{listener['id']}/toggle-status", headers=headers_admin)
|
|
|
|
# Verify Author
|
|
token_author = get_token(author_user, password)
|
|
if token_author:
|
|
author_data = requests.get(f"{BASE_URL}/auth/me", headers={"Authorization": f"Bearer {token_author}"}).json()
|
|
author_id = author_data.get("id")
|
|
r = requests.post(f"{BASE_URL}/admin/users/{author_id}/verify", headers=headers_admin)
|
|
print(f"Admin verify author: {r.status_code}")
|
|
|
|
# 2. AUTHOR: Upload & Auto-moderation
|
|
print("\n--- Testing Author Role ---")
|
|
if not token_author: return
|
|
headers_author = {"Authorization": f"Bearer {token_author}"}
|
|
|
|
files = {'file': ('test_song.mp3', b'fake audio content', 'audio/mpeg')}
|
|
data = {'title': 'Verified Song', 'genre': 'Test'}
|
|
r = requests.post(f"{BASE_URL}/songs/upload", headers=headers_author, files=files, data=data)
|
|
if r.status_code in [200, 201]:
|
|
song_id = r.json().get("song_id")
|
|
print(f"Author upload success: {song_id}")
|
|
# Check if auto-approved (since we verified the author)
|
|
song_info = requests.get(f"{BASE_URL}/songs/{song_id}", headers=headers_author).json()
|
|
print(f"Song approved status: {song_info.get('is_approved')}")
|
|
else:
|
|
print(f"Author upload failed: {r.status_code} - {r.text}")
|
|
|
|
# 3. LISTENER: Discovery, Like, and Playlists
|
|
print("\n--- Testing Listener Role ---")
|
|
token_listener = get_token(listener_user, password)
|
|
if not token_listener: return
|
|
headers_listener = {"Authorization": f"Bearer {token_listener}"}
|
|
|
|
# Discover songs
|
|
r = requests.get(f"{BASE_URL}/songs/", headers=headers_listener)
|
|
print(f"Discover songs: {r.status_code}")
|
|
|
|
if 'song_id' in locals() or 'uploaded_song_id' in locals():
|
|
# Like the song
|
|
s_id = song_id if 'song_id' in locals() else None
|
|
if s_id:
|
|
r = requests.post(f"{BASE_URL}/songs/{s_id}/like", headers=headers_listener)
|
|
print(f"Like song {s_id}: {r.status_code}")
|
|
|
|
# Check Liked Songs playlist
|
|
r = requests.get(f"{BASE_URL}/playlists/liked", headers=headers_listener)
|
|
print(f"Get liked songs: {r.status_code} - Found {len(r.json()) if r.status_code == 200 else 0} songs")
|
|
|
|
# Playlist management
|
|
playlist_data = {"name": "My Test Playlist"}
|
|
r = requests.post(f"{BASE_URL}/playlists", headers=headers_listener, json=playlist_data)
|
|
if r.status_code in [200, 201]:
|
|
p_id = r.json().get("playlist_id") or r.json().get("id")
|
|
print(f"Create playlist: {p_id}")
|
|
# Add song to playlist
|
|
if 'song_id' in locals():
|
|
r = requests.post(f"{BASE_URL}/playlists/{p_id}/songs/{song_id}", headers=headers_listener)
|
|
print(f"Add song to playlist: {r.status_code}")
|
|
|
|
# 4. PROFILE: Password Change
|
|
print("\n--- Testing Profile Settings ---")
|
|
new_password = "NewPassword123!"
|
|
r = requests.put(f"{BASE_URL}/auth/change-password", headers=headers_listener, json={"old_password": password, "new_password": new_password})
|
|
print(f"Change password: {r.status_code}")
|
|
|
|
if __name__ == "__main__":
|
|
test_comprehensive()
|