|
| 1 | +import json |
| 2 | +from unittest.mock import patch, mock_open, MagicMock |
| 3 | +from fastapi.testclient import TestClient |
| 4 | +from server import app, apply_proxy_to_openapi |
| 5 | +from urllib.parse import unquote |
| 6 | + |
| 7 | +client = TestClient(app) |
| 8 | + |
| 9 | +def test_proxy_invalid_url(): |
| 10 | + # Tests error when passing an invalid URL |
| 11 | + response = client.get("/proxy", params={"url": "http://invalid-url"}) |
| 12 | + assert response.status_code == 500 |
| 13 | + assert "Failed to fetch OpenAPI document" in response.text |
| 14 | + |
| 15 | +def test_docs_returns_html(): |
| 16 | + # Tests if the main route returns HTML |
| 17 | + response = client.get("/") |
| 18 | + assert response.status_code == 200 |
| 19 | + assert "text/html" in response.headers["content-type"] |
| 20 | + |
| 21 | +def test_proxy_with_headers(): |
| 22 | + url = "http://example.com/openapi.json" |
| 23 | + headers_dict = {"Authorization": "Bearer token123"} |
| 24 | + headers_encoded = json.dumps(headers_dict) # The endpoint expects headers already in JSON |
| 25 | + |
| 26 | + mock_response = MagicMock() |
| 27 | + mock_response.content = b'{"openapi": "3.0.0"}' |
| 28 | + mock_response.text = '{"openapi": "3.0.0"}' |
| 29 | + mock_response.headers = {"content-type": "application/json"} |
| 30 | + |
| 31 | + with patch("server.requests.get", return_value=mock_response) as mock_get: |
| 32 | + response = client.get("/proxy", params={"url": url, "headers": headers_encoded}) |
| 33 | + assert response.status_code == 200 |
| 34 | + assert response.json() == {"openapi": "3.0.0"} |
| 35 | + mock_get.assert_called_once() |
| 36 | + # Checks if the headers were passed correctly |
| 37 | + called_headers = mock_get.call_args[1]["headers"] |
| 38 | + assert called_headers == headers_dict |
| 39 | + |
| 40 | +def test_proxy_yaml_response(): |
| 41 | + url = "http://example.com/openapi.yaml" |
| 42 | + headers_dict = {"Authorization": "Bearer token123"} |
| 43 | + headers_json = json.dumps(headers_dict) |
| 44 | + |
| 45 | + yaml_content = """ |
| 46 | +openapi: 3.0.0 |
| 47 | +info: |
| 48 | + title: API Teste |
| 49 | + version: "1.0" |
| 50 | +paths: {} |
| 51 | +""" |
| 52 | + mock_response = MagicMock() |
| 53 | + mock_response.content = yaml_content.encode("utf-8") |
| 54 | + mock_response.text = yaml_content |
| 55 | + mock_response.headers = {"content-type": "application/yaml"} |
| 56 | + |
| 57 | + with patch("server.requests.get", return_value=mock_response) as mock_get: |
| 58 | + response = client.get("/proxy", params={"url": url, "headers": headers_json}) |
| 59 | + assert response.status_code == 200 |
| 60 | + assert response.headers["content-type"].startswith("text/yaml") |
| 61 | + assert "openapi: 3.0.0" in response.text |
| 62 | + mock_get.assert_called_once() |
| 63 | + called_headers = mock_get.call_args[1]["headers"] |
| 64 | + assert called_headers == headers_dict |
| 65 | + |
| 66 | +def test_docs_file_not_found(monkeypatch): |
| 67 | + # Simulates FileNotFoundError when opening the file |
| 68 | + with patch("builtins.open", side_effect=FileNotFoundError): |
| 69 | + response = client.get("/") |
| 70 | + assert response.status_code == 200 |
| 71 | + # Should contain the default aggregator name |
| 72 | + assert "Swagger Aggregator" in response.text |
| 73 | + |
| 74 | +def test_docs_json_decode_error(monkeypatch): |
| 75 | + # Simulates invalid JSON error when opening the file |
| 76 | + m = mock_open(read_data="not a json") |
| 77 | + with patch("builtins.open", m): |
| 78 | + with patch("json.load", side_effect=json.JSONDecodeError("msg", "doc", 0)): |
| 79 | + response = client.get("/") |
| 80 | + assert response.status_code == 200 |
| 81 | + assert "Swagger Aggregator" in response.text |
| 82 | + |
| 83 | +def test_config_json_decode_error(): |
| 84 | + # Simulates invalid JSON error when opening the file |
| 85 | + m = mock_open(read_data="not a json") |
| 86 | + with patch("builtins.open", m): |
| 87 | + with patch("json.load", side_effect=Exception("invalid json")): |
| 88 | + response = client.get("/config") |
| 89 | + assert response.status_code == 200 |
| 90 | + |
| 91 | +def test_apply_proxy_to_openapi_with_http(): |
| 92 | + url = "http://example.com/openapi.json" |
| 93 | + header = {"Authorization": "Bearer token"} |
| 94 | + result = apply_proxy_to_openapi(url, header) |
| 95 | + assert result.startswith("/proxy?url=http://example.com/openapi.json") |
| 96 | + assert "headers=" in result |
| 97 | + headers_param = result.split("headers=")[1] |
| 98 | + decoded = json.loads(unquote(headers_param)) # Fixed here! |
| 99 | + assert decoded == header |
| 100 | + |
| 101 | +def test_apply_proxy_to_openapi_with_http_and_headers(): |
| 102 | + url = "http://example.com/openapi.json" |
| 103 | + header = {"Authorization": "Bearer token"} |
| 104 | + result = apply_proxy_to_openapi(url, header) |
| 105 | + assert result.startswith("/proxy?url=http://example.com/openapi.json") |
| 106 | + assert "headers=" in result |
| 107 | + # Decodes and checks the header |
| 108 | + headers_param = result.split("headers=")[1] |
| 109 | + decoded = json.loads(unquote(headers_param)) |
| 110 | + assert decoded == header |
| 111 | + |
| 112 | +def test_apply_proxy_to_openapi_with_http_no_headers(): |
| 113 | + url = "http://example.com/openapi.json" |
| 114 | + result = apply_proxy_to_openapi(url) |
| 115 | + assert result == f"/proxy?url={url}" |
| 116 | + |
| 117 | +def test_apply_proxy_to_openapi_with_non_http_url(): |
| 118 | + url = "/local/openapi.json" |
| 119 | + result = apply_proxy_to_openapi(url) |
| 120 | + assert result == url |
| 121 | + |
| 122 | +def test_apply_proxy_to_openapi_with_empty_header(): |
| 123 | + url = "http://example.com/openapi.json" |
| 124 | + result = apply_proxy_to_openapi(url, {}) |
| 125 | + # Should not add headers if the dict is empty |
| 126 | + assert result == f"/proxy?url={url}" |
| 127 | + |
| 128 | +def test_apply_proxy_to_openapi_with_special_characters_in_header(): |
| 129 | + url = "http://example.com/openapi.json" |
| 130 | + header = {"X-Test": "çãõ@#%&"} |
| 131 | + result = apply_proxy_to_openapi(url, header) |
| 132 | + headers_param = result.split("headers=")[1] |
| 133 | + decoded = json.loads(unquote(headers_param)) |
| 134 | + assert decoded == header |
0 commit comments