test_integration.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """Integration tests against the live Chatto API.
  2. Run with: pytest tests/test_integration.py -v
  3. Requires: CHATTO_LOGIN and CHATTO_PASSWORD environment variables.
  4. """
  5. import os
  6. import pytest
  7. from chattolib.client import ChattoClient
  8. from chattolib.exceptions import ChattoAuthError
  9. pytestmark = pytest.mark.skipif(
  10. not os.environ.get("CHATTO_LOGIN") or not os.environ.get("CHATTO_PASSWORD"),
  11. reason="CHATTO_LOGIN and CHATTO_PASSWORD env vars required",
  12. )
  13. @pytest.fixture
  14. async def client():
  15. c = await ChattoClient.login(
  16. os.environ["CHATTO_LOGIN"],
  17. os.environ["CHATTO_PASSWORD"],
  18. )
  19. async with c:
  20. yield c
  21. async def test_login_and_me(client):
  22. user = await client.me()
  23. assert user.id
  24. assert user.login == os.environ["CHATTO_LOGIN"]
  25. print(f"Logged in as {user.login} (id={user.id})")
  26. async def test_invalid_login():
  27. with pytest.raises(ChattoAuthError, match="Invalid credentials"):
  28. await ChattoClient.login("nonexistent_user_xyz", "wrongpassword")
  29. async def test_list_spaces(client):
  30. spaces = await client.spaces()
  31. assert isinstance(spaces, list)
  32. print(f"Found {len(spaces)} spaces")
  33. for s in spaces:
  34. print(f" - {s.name} (id={s.id}, members={s.member_count})")
  35. async def test_create_user(client):
  36. """Test creating a new user via the createUser mutation (requires admin)."""
  37. import random
  38. import string
  39. suffix = "".join(random.choices(string.ascii_lowercase + string.digits, k=6))
  40. login = f"testbot_{suffix}"
  41. display_name = f"Test Bot {suffix}"
  42. password = "TestPass123!"
  43. user = await client.create_user(login, display_name, password)
  44. assert user.id
  45. assert user.login == login
  46. assert user.display_name == display_name
  47. print(f"Created user: {user.login} (id={user.id})")