202 lines
7.7 KiB
Python
202 lines
7.7 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
Тестирование удаления нового сообщества через API
|
||
"""
|
||
|
||
import json
|
||
import time
|
||
import requests
|
||
|
||
|
||
def wait_for_server_ready(url: str, timeout: int = 60) -> bool:
|
||
"""Ждем готовности сервера"""
|
||
start_time = time.time()
|
||
while time.time() - start_time < timeout:
|
||
try:
|
||
response = requests.get(url, timeout=10)
|
||
if response.status_code == 200:
|
||
return True
|
||
except:
|
||
pass
|
||
time.sleep(2)
|
||
return False
|
||
|
||
|
||
def test_delete_new_community():
|
||
"""Тестируем удаление нового сообщества через API"""
|
||
|
||
# Проверяем готовность бэкенда
|
||
print("🌐 Проверяем готовность бэкенда...")
|
||
if not wait_for_server_ready("http://localhost:8000"):
|
||
print("❌ Бэкенд не готов в течение 60 секунд")
|
||
return False
|
||
|
||
print("✅ Бэкенд готов")
|
||
|
||
# 1. Авторизуемся как test_admin@discours.io
|
||
print("🔐 Авторизуемся как test_admin@discours.io...")
|
||
try:
|
||
login_response = requests.post(
|
||
"http://localhost:8000/graphql",
|
||
headers={"Content-Type": "application/json"},
|
||
json={
|
||
"query": """
|
||
mutation Login($email: String!, $password: String!) {
|
||
login(email: $email, password: $password) {
|
||
success
|
||
token
|
||
author {
|
||
id
|
||
name
|
||
email
|
||
}
|
||
error
|
||
}
|
||
}
|
||
""",
|
||
"variables": {"email": "test_admin@discours.io", "password": "password123"},
|
||
},
|
||
timeout=30 # Увеличиваем таймаут
|
||
)
|
||
except requests.exceptions.Timeout:
|
||
print("❌ Таймаут при авторизации")
|
||
return False
|
||
except requests.exceptions.ConnectionError:
|
||
print("❌ Ошибка подключения к бэкенду")
|
||
return False
|
||
|
||
login_data = login_response.json()
|
||
if not login_data.get("data", {}).get("login", {}).get("success"):
|
||
print("❌ Ошибка авторизации test_admin@discours.io")
|
||
print(f"Ответ: {json.dumps(login_data, indent=2, ensure_ascii=False)}")
|
||
return False
|
||
|
||
token = login_data["data"]["login"]["token"]
|
||
user_id = login_data["data"]["login"]["author"]["id"]
|
||
print(f"✅ Авторизация успешна, пользователь ID: {user_id}")
|
||
|
||
# 2. Проверяем, что сообщество существует
|
||
print("🔍 Проверяем существование сообщества...")
|
||
try:
|
||
communities_response = requests.post(
|
||
"http://localhost:8000/graphql",
|
||
headers={"Authorization": f"Bearer {token}", "Content-Type": "application/json"},
|
||
json={
|
||
"query": """
|
||
query GetCommunities {
|
||
get_communities_all {
|
||
id
|
||
name
|
||
slug
|
||
created_by {
|
||
id
|
||
name
|
||
email
|
||
}
|
||
}
|
||
}
|
||
""",
|
||
},
|
||
timeout=30 # Увеличиваем таймаут
|
||
)
|
||
except requests.exceptions.Timeout:
|
||
print("❌ Таймаут при получении списка сообществ")
|
||
return False
|
||
|
||
communities_data = communities_response.json()
|
||
target_community = None
|
||
for community in communities_data.get("data", {}).get("get_communities_all", []):
|
||
if community["slug"] == "test-admin-community-e2e-1754005730":
|
||
target_community = community
|
||
break
|
||
|
||
if not target_community:
|
||
print("❌ Сообщество test-admin-community-e2e-1754005730 не найдено")
|
||
print("Доступные сообщества:")
|
||
for community in communities_data.get("data", {}).get("get_communities_all", []):
|
||
print(f" - {community['name']} (slug: {community['slug']})")
|
||
return False
|
||
|
||
print(f"✅ Найдено сообщество: {target_community['name']} (ID: {target_community['id']})")
|
||
print(f" Создатель: {target_community['created_by']['name']} (ID: {target_community['created_by']['id']})")
|
||
|
||
# 3. Пытаемся удалить сообщество
|
||
print("🗑️ Пытаемся удалить сообщество...")
|
||
try:
|
||
delete_response = requests.post(
|
||
"http://localhost:8000/graphql",
|
||
headers={"Authorization": f"Bearer {token}", "Content-Type": "application/json"},
|
||
json={
|
||
"query": """
|
||
mutation DeleteCommunity($slug: String!) {
|
||
delete_community(slug: $slug) {
|
||
success
|
||
message
|
||
error
|
||
}
|
||
}
|
||
""",
|
||
"variables": {"slug": "test-admin-community-e2e-1754005730"},
|
||
},
|
||
timeout=30 # Увеличиваем таймаут
|
||
)
|
||
except requests.exceptions.Timeout:
|
||
print("❌ Таймаут при удалении сообщества")
|
||
return False
|
||
|
||
delete_data = delete_response.json()
|
||
print(f"📡 Ответ удаления: {json.dumps(delete_data, indent=2, ensure_ascii=False)}")
|
||
|
||
if delete_data.get("data", {}).get("delete_community", {}).get("success"):
|
||
print("✅ Удаление прошло успешно")
|
||
|
||
# 4. Проверяем, что сообщество действительно удалено
|
||
print("🔍 Проверяем, что сообщество удалено...")
|
||
try:
|
||
check_response = requests.post(
|
||
"http://localhost:8000/graphql",
|
||
headers={"Authorization": f"Bearer {token}", "Content-Type": "application/json"},
|
||
json={
|
||
"query": """
|
||
query GetCommunities {
|
||
get_communities_all {
|
||
id
|
||
name
|
||
slug
|
||
}
|
||
}
|
||
""",
|
||
},
|
||
timeout=30 # Увеличиваем таймаут
|
||
)
|
||
except requests.exceptions.Timeout:
|
||
print("❌ Таймаут при проверке удаления")
|
||
return False
|
||
|
||
check_data = check_response.json()
|
||
still_exists = False
|
||
for community in check_data.get("data", {}).get("get_communities_all", []):
|
||
if community["slug"] == "test-admin-community-e2e-1754005730":
|
||
still_exists = True
|
||
break
|
||
|
||
if still_exists:
|
||
print("❌ Сообщество все еще существует после удаления")
|
||
return False
|
||
else:
|
||
print("✅ Сообщество успешно удалено из базы данных")
|
||
return True
|
||
else:
|
||
print("❌ Ошибка удаления")
|
||
error = delete_data.get("data", {}).get("delete_community", {}).get("error")
|
||
print(f"Ошибка: {error}")
|
||
return False
|
||
|
||
|
||
if __name__ == "__main__":
|
||
if test_delete_new_community():
|
||
print("✅ Тест завершен успешно")
|
||
else:
|
||
print("❌ Тест завершен с ошибками")
|
||
exit(1)
|