test_docker.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. """
  2. Docker integration tests.
  3. These tests run against a containerized instance of BamBuddy.
  4. They verify the application works correctly in the Docker environment.
  5. Run with: pytest -m docker
  6. Or via: ./test_docker.sh --integration-only
  7. """
  8. import os
  9. import httpx
  10. import pytest
  11. # Get the test URL from environment (set by docker-compose.test.yml)
  12. BAMBUDDY_URL = os.environ.get("BAMBUDDY_TEST_URL", "http://localhost:8000")
  13. @pytest.fixture
  14. def client():
  15. """HTTP client for testing."""
  16. return httpx.Client(base_url=BAMBUDDY_URL, timeout=10.0)
  17. @pytest.mark.docker
  18. class TestDockerHealth:
  19. """Test health and basic functionality in Docker."""
  20. def test_health_endpoint(self, client):
  21. """Health endpoint returns healthy status."""
  22. response = client.get("/health")
  23. assert response.status_code == 200
  24. data = response.json()
  25. assert data["status"] == "healthy"
  26. def test_api_docs_available(self, client):
  27. """OpenAPI docs are accessible."""
  28. response = client.get("/docs")
  29. assert response.status_code == 200
  30. assert "swagger" in response.text.lower() or "openapi" in response.text.lower()
  31. def test_static_files_served(self, client):
  32. """Static files (frontend) are served."""
  33. response = client.get("/")
  34. assert response.status_code == 200
  35. # Should serve index.html with React app
  36. assert "text/html" in response.headers.get("content-type", "")
  37. @pytest.mark.docker
  38. class TestDockerAPI:
  39. """Test API endpoints work correctly in Docker."""
  40. def test_printers_endpoint(self, client):
  41. """Printers API endpoint is accessible."""
  42. response = client.get("/api/v1/printers/")
  43. # Should return empty list or list of printers
  44. assert response.status_code == 200
  45. assert isinstance(response.json(), list)
  46. def test_archives_endpoint(self, client):
  47. """Archives API endpoint is accessible."""
  48. response = client.get("/api/v1/archives/")
  49. assert response.status_code == 200
  50. data = response.json()
  51. assert "items" in data or isinstance(data, list)
  52. def test_settings_endpoint(self, client):
  53. """Settings API endpoint is accessible."""
  54. response = client.get("/api/v1/settings")
  55. assert response.status_code == 200
  56. def test_projects_endpoint(self, client):
  57. """Projects API endpoint is accessible."""
  58. response = client.get("/api/v1/projects/")
  59. assert response.status_code == 200
  60. assert isinstance(response.json(), list)
  61. @pytest.mark.docker
  62. class TestDockerPersistence:
  63. """Test that data persistence works in Docker."""
  64. def test_database_writable(self, client):
  65. """Can create and retrieve data (database is writable)."""
  66. # Create a project
  67. response = client.post(
  68. "/api/v1/projects/",
  69. json={"name": "Docker Test Project", "description": "Test project for Docker"},
  70. )
  71. # May return 200, 201, or 409 (if already exists)
  72. assert response.status_code in [200, 201, 409]
  73. # Verify we can list projects
  74. response = client.get("/api/v1/projects/")
  75. assert response.status_code == 200
  76. projects = response.json()
  77. assert isinstance(projects, list)
  78. @pytest.mark.docker
  79. class TestDockerWebSocket:
  80. """Test WebSocket functionality in Docker."""
  81. def test_websocket_endpoint_exists(self, client):
  82. """WebSocket endpoint is configured (not a full WS test)."""
  83. # We can't easily test WebSocket with httpx, but we can verify
  84. # the endpoint is routed and accessible
  85. response = client.get("/api/v1/ws")
  86. # May return various codes depending on framework handling:
  87. # 200 (endpoint exists), 400, 403, or 426 (Upgrade Required)
  88. assert response.status_code in [200, 400, 403, 426]