File size: 4,255 Bytes
2d9b352
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""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"