api_diff / tests /test_frontend.py
kumudraj01's picture
Add application file
2d9b352
Raw
History Blame Contribute Delete
3.27 kB
"""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()