"""Test module for API comparison functionality""" from unittest.mock import AsyncMock, patch # Standard library import aiohttp import pytest # Third-party from fastapi import status def test_compare_page_protected(client): """Verify that compare page requires authentication""" response = client.get("/compare", follow_redirects=False) assert response.status_code == status.HTTP_302_FOUND assert response.headers["location"] == "/login" def test_compare_page_authenticated(client, test_user): """Verify authenticated user can access compare page""" client.post("/login", data=test_user) response = client.get("/compare") assert response.status_code == status.HTTP_200_OK def test_compare_apis_validation(client, test_user): """Test API comparison form validation""" client.post("/login", data=test_user) response = client.post("/compare", data={ "api1_url": "not-a-url", "api1_method": "GET", "api1_payload": "{}", "api1_headers": "{}", "api2_url": "http://example.com", "api2_method": "GET", "api2_payload": "{}", "api2_headers": "{}", "view_mode": "line" }) assert "valid URL" in response.text @pytest.mark.asyncio async def test_compare_apis_success(client, test_user): """Test successful API comparison with mocked responses, including SSL setting.""" mock_data1 = {"test": "data1"} mock_data2 = {"test": "data2"} # Mock responses mock_response1 = AsyncMock() mock_response1.json.return_value = mock_data1 mock_response1.status = 200 mock_response1.__aenter__.return_value = mock_response1 mock_response2 = AsyncMock() mock_response2.json.return_value = mock_data2 mock_response2.status = 200 mock_response2.__aenter__.return_value = mock_response2 # Mock session mock_session = AsyncMock(spec=aiohttp.ClientSession) mock_session.__aenter__.return_value = mock_session mock_session.request.side_effect = [mock_response1, mock_response2] # Login the test user client.post("/login", data=test_user) # Patch aiohttp ClientSession with patch('aiohttp.ClientSession', return_value=mock_session): response = client.post("/compare", data={ "api1_url": "http://api1.example.com", "api1_method": "GET", "api1_payload": "{}", "api1_headers": "{}", "api2_url": "http://api2.example.com", "api2_method": "GET", "api2_payload": "{}", "api2_headers": "{}", "view_mode": "line" }, follow_redirects=True) assert response.status_code == status.HTTP_200_OK assert "data1" in response.text assert "data2" in response.text assert mock_session.request.call_count == 2 # Check that requests include the ssl argument (which can be None or a context) mock_session.request.assert_any_call( method='GET', url="http://api1.example.com", json=None, params={}, headers={}, timeout=30, ssl=None # 🛡️ Include SSL param check ) mock_session.request.assert_any_call( method='GET', url="http://api2.example.com", json=None, params={}, headers={}, timeout=30, ssl=None ) def test_download_result_protected(client): """Test that download requires authentication""" response = client.post("/download/", data={ "content": "{}", "filename": "test.json", "username": "admin" }, follow_redirects=False) assert response.status_code == status.HTTP_302_FOUND assert response.headers["location"] == "/login" def test_download_result_authenticated(client, test_user): """Test authenticated download with valid JSON""" client.post("/login", data=test_user) response = client.post("/download/", data={ "content": '{"test": "data"}', "filename": "test.json", "username": test_user["username"] }) assert response.status_code == status.HTTP_200_OK assert response.headers["content-type"] == "application/json"