File size: 3,268 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
"""Test module for frontend functionality"""
from bs4 import BeautifulSoup
from fastapi import status


def test_compare_form_validation(client, test_user):
    """Test form validation for API comparison"""
    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


def test_download_error_handling(client, test_user):
    """Test error handling for file downloads"""
    client.post("/login", data=test_user)

    response = client.post("/download/", data={
        "content": "{invalid-json}",
        "filename": "test.json",
        "username": test_user["username"]
    })
    assert response.status_code == status.HTTP_400_BAD_REQUEST
    assert "Invalid JSON content" in response.text


def test_session_timeout_redirect(client, test_user):
    """Test session timeout redirects to login page"""
    client.post("/login", data=test_user)

    response = client.get("/compare")
    assert response.status_code == status.HTTP_200_OK

    client.cookies.clear()

    response = client.get("/compare", follow_redirects=False)
    assert response.status_code == status.HTTP_302_FOUND
    assert response.headers["location"] == "/login"


def test_session_timeout_page_content(client):
    """Test session timeout page content"""
    response = client.get("/login")
    assert response.status_code == status.HTTP_200_OK

    soup = BeautifulSoup(response.text, 'html.parser')

    assert soup.find('form', {'action': '/login'}) is not None
    assert soup.find('input', {'name': 'username'}) is not None
    assert soup.find('input', {'name': 'password'}) is not None


def test_error_page_content(client, test_user):
    """Test error page content"""
    client.post("/login", data=test_user)

    response = client.post("/compare", data={
        "api1_url": "http://example.com",
        "api1_method": "GET",
        "api1_payload": "{}",
        "api1_headers": "{}",
        "api2_url": "http://example.com",
        "api2_method": "GET",
        "api2_payload": "{}",
        "api2_headers": "{}"
    })
    assert response.status_code == status.HTTP_200_OK
    assert "API URLs cannot be identical" in response.text


def test_error_handler_internal_server_error(client, test_user):
    """Test internal server error handling"""
    client.post("/login", data=test_user)

    response = client.post("/compare", data={
        "api1_url": "http://api1.example.com",
        "api1_method": "GET",
        "api1_payload": "{invalid-json}",
        "api1_headers": "{}",
        "api2_url": "http://api2.example.com",
        "api2_method": "GET",
        "api2_payload": "{}",
        "api2_headers": "{}"
    })
    assert response.status_code == status.HTTP_200_OK
    assert "error" in response.text.lower()
    soup = BeautifulSoup(response.text, 'html.parser')
    error_div = soup.find('div', {'class': 'error'})
    assert error_div is not None
    assert "invalid" in error_div.text.lower()