test_button_apis_v2.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import pytest
  2. import random
  3. import time
  4. from fastapi.testclient import TestClient
  5. def register_and_login(client):
  6. """Helper to create a user and get an access token."""
  7. username = "testuser_" + str(random.randint(10000, 99999))
  8. password = "password123"
  9. # 1. Register
  10. reg_res = client.post(
  11. "/api/v1/auth/register",
  12. json={"username": username, "password": password, "role": "user"}
  13. )
  14. assert reg_res.status_code == 200, f"Registration failed: {reg_res.text}"
  15. # 2. Login (This tests the Login Button API)
  16. login_res = client.post(
  17. "/api/v1/auth/login",
  18. data={"username": username, "password": password}
  19. )
  20. assert login_res.status_code == 200, f"Login failed: {login_res.text}"
  21. data = login_res.json()
  22. assert "access_token" in data
  23. return data["access_token"], username
  24. def test_button_api_workflow(client):
  25. """
  26. Test the entire workflow corresponding to main button interactions:
  27. 1. Login (Implicit in setup)
  28. 2. Create Persona (Prerequisite for forum)
  29. 3. Create Forum (Create Button)
  30. 4. Start Forum (Start Button)
  31. 5. Delete Forum (Delete Button)
  32. """
  33. # 1. Login
  34. token, _ = register_and_login(client)
  35. headers = {"Authorization": f"Bearer {token}"}
  36. # 2. Create a Persona (Needed for forum creation)
  37. persona_res = client.post(
  38. "/api/v1/personas/",
  39. headers=headers,
  40. json={
  41. "name": "Test Persona",
  42. "title": "Tester",
  43. "bio": "A test persona",
  44. "theories": ["Test Theory"],
  45. "stance": "Neutral",
  46. "system_prompt": "You are a test.",
  47. "is_public": False
  48. }
  49. )
  50. assert persona_res.status_code == 200
  51. persona_id = persona_res.json()["id"]
  52. # 3. Create Forum (Simulates 'Create Forum' button click)
  53. forum_res = client.post(
  54. "/api/v1/forums/",
  55. headers=headers,
  56. json={
  57. "topic": "Button Test Forum",
  58. "participant_ids": [persona_id],
  59. "duration_minutes": 30
  60. }
  61. )
  62. assert forum_res.status_code == 200
  63. forum_data = forum_res.json()
  64. forum_id = forum_data["id"]
  65. assert forum_data["topic"] == "Button Test Forum"
  66. assert forum_data["status"] == "pending"
  67. # 4. Start Forum (Simulates 'Start Forum' button click)
  68. # Note: Start endpoint might be async or trigger background tasks
  69. start_res = client.post(
  70. f"/api/v1/forums/{forum_id}/start",
  71. headers=headers
  72. )
  73. # It might return 200 or 202
  74. assert start_res.status_code in [200, 202]
  75. # Wait for background task to start
  76. time.sleep(1)
  77. # Verify status changed to running
  78. get_res = client.get(f"/api/v1/forums/{forum_id}", headers=headers)
  79. assert get_res.status_code == 200
  80. assert get_res.json()["status"] == "running"
  81. # 5. Delete Forum (Simulates 'Delete' button click)
  82. delete_res = client.delete(f"/api/v1/forums/{forum_id}", headers=headers)
  83. assert delete_res.status_code == 200
  84. # Verify deletion
  85. get_res_after = client.get(f"/api/v1/forums/{forum_id}", headers=headers)
  86. assert get_res_after.status_code == 404