diff --git a/server/api_server/api_server_start.py b/server/api_server/api_server_start.py index de68c90d..daf9c274 100755 --- a/server/api_server/api_server_start.py +++ b/server/api_server/api_server_start.py @@ -4,7 +4,7 @@ import os # flake8: noqa: E402 -from flask import Flask, request, jsonify, Response +from flask import Flask, redirect, request, jsonify, url_for, Response from models.device_instance import DeviceInstance # noqa: E402 from flask_cors import CORS from werkzeug.exceptions import HTTPException @@ -1165,6 +1165,11 @@ def api_docs(): return send_from_directory(openapi_dir, 'swagger.html') +@app.route('/') +def index_redirect(): + """Redirect root to API documentation.""" + return redirect(url_for('api_docs')) + # -------------------------- # DB query # -------------------------- diff --git a/test/api_endpoints/test_docs.py b/test/api_endpoints/test_docs.py new file mode 100644 index 00000000..bc12cecf --- /dev/null +++ b/test/api_endpoints/test_docs.py @@ -0,0 +1,23 @@ +# test/api_endpoints/test_docs.py - Tests for root redirect and API docs endpoints + + +from api_server import api_server_start + + +def test_index_redirect_logic(): + """Test the redirect function logic directly.""" + with api_server_start.app.test_client() as client: + response = client.get("/", follow_redirects=False) + assert response.status_code == 302 + assert response.location == '/docs' + + +def test_api_docs_logic(): + """Test that api_docs attempts to serve the correct file.""" + with api_server_start.app.test_client() as client: + response = client.get("/docs") + assert response.status_code == 200 + assert response.mimetype == "text/html" + response.direct_passthrough = False + data = response.get_data() + assert b"" in data or b"" in data