Skip to content

[Bug] Fix Unit Tests to Handle UI-Disabled Mode #378

@crivetimihai

Description

@crivetimihai

Fix Unit Tests to Handle UI-Disabled Mode

Priority: Medium (Testing/CI)

Description:
Two unit tests are failing because they assume the Admin UI is enabled, but the default test configuration has the UI disabled (MCPGATEWAY_UI_ENABLED=false). The tests need to be updated to either:

  1. Check if UI is enabled before testing UI-specific behavior
  2. Mock/override the UI settings for these specific tests
  3. Skip UI tests when UI is disabled

Failing Tests:

  1. tests/unit/mcpgateway/test_main.py::TestHealthAndInfrastructure::test_root_redirect

    • Expects: 303 redirect to /admin
    • Actual: 200 (likely returning JSON info when UI is disabled)
  2. tests/unit/mcpgateway/test_ui_version.py::test_admin_ui_contains_version_tab

    • Expects: 200 with HTML content
    • Actual: 404 (admin UI not available when disabled)

Root Cause:
The tests were written assuming the UI is always enabled, but the default .env.example and test environment have:

MCPGATEWAY_UI_ENABLED=false
MCPGATEWAY_ADMIN_API_ENABLED=false

Suggested Implementation:

  1. Update test_root_redirect to handle both UI modes:
# test_main.py
def test_root_redirect(self, test_client):
    """Test that root path behavior depends on UI configuration."""
    response = test_client.get("/", follow_redirects=False)
    
    # Check if UI is enabled
    if settings.mcpgateway_ui_enabled:
        # When UI is enabled, should redirect to admin
        assert response.status_code == 303
        assert response.headers["location"] == "/admin"
    else:
        # When UI is disabled, should return API info
        assert response.status_code == 200
        data = response.json()
        assert data["name"] == "MCP_Gateway"
        assert data["ui_enabled"] is False
  1. Update test_admin_ui_contains_version_tab to skip when UI disabled:
# test_ui_version.py
import pytest
from mcpgateway.config import settings

@pytest.mark.skipif(
    not settings.mcpgateway_ui_enabled,
    reason="Admin UI tests require MCPGATEWAY_UI_ENABLED=true"
)
def test_admin_ui_contains_version_tab(test_client: TestClient, auth_headers: Dict[str, str]):
    """The Admin dashboard must contain the "Version & Environment Info" tab."""
    resp = test_client.get("/admin", headers=auth_headers)
    assert resp.status_code == 200
    assert 'id="tab-version-info"' in resp.text
    assert "Version and Environment Info" in resp.text
  1. Alternative: Create a UI-enabled test fixture:
# conftest.py or test file
@pytest.fixture
def ui_enabled_client(app):
    """Test client with UI forcibly enabled."""
    original_ui = settings.mcpgateway_ui_enabled
    original_admin = settings.mcpgateway_admin_api_enabled
    
    # Force UI to be enabled
    settings.mcpgateway_ui_enabled = True
    settings.mcpgateway_admin_api_enabled = True
    
    client = TestClient(app)
    yield client
    
    # Restore original settings
    settings.mcpgateway_ui_enabled = original_ui
    settings.mcpgateway_admin_api_enabled = original_admin

# Then use in tests:
def test_admin_ui_contains_version_tab(ui_enabled_client, auth_headers):
    resp = ui_enabled_client.get("/admin", headers=auth_headers)
    assert resp.status_code == 200
    # ... rest of test
  1. Add explicit UI-disabled tests:
@pytest.mark.parametrize("ui_enabled", [True, False])
def test_root_behavior_with_ui_toggle(test_client, ui_enabled):
    """Test root endpoint behavior with different UI settings."""
    with patch.object(settings, 'mcpgateway_ui_enabled', ui_enabled):
        response = test_client.get("/", follow_redirects=False)
        
        if ui_enabled:
            assert response.status_code == 303
            assert response.headers["location"] == "/admin"
        else:
            assert response.status_code == 200
            data = response.json()
            assert "ui_enabled" in data
            assert data["ui_enabled"] == ui_enabled

Testing Strategy:

  1. Create separate test classes/modules for UI-specific tests
  2. Use pytest markers to categorize tests: @pytest.mark.ui
  3. Allow running UI tests separately: pytest -m ui
  4. Document in test files what configuration they expect

Benefits:

  • Tests work correctly regardless of default configuration
  • CI/CD pipelines won't fail due to configuration differences
  • Clear separation between API and UI tests
  • Better test coverage for both UI-enabled and UI-disabled modes

Implementation Notes:

  • Consider adding a test configuration file specifically for UI tests
  • May need to update CI/CD to run tests with different configurations
  • Document the expected test environment setup

Acceptance Criteria:

  • test_root_redirect passes with UI disabled
  • test_admin_ui_contains_version_tab is skipped or passes appropriately
  • Tests clearly indicate their UI requirements
  • No false failures due to configuration mismatches
  • Test suite runs successfully in default configuration

Related Issues:

  • Consider creating integration tests that specifically test UI functionality
  • May need to update other tests that assume UI availability
  • Document testing best practices for UI vs API tests

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingtestingTesting (unit, e2e, manual, automated, etc)triageIssues / Features awaiting triage

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions