diff --git a/src/server.py b/src/server.py index d14fb1b..9524d3e 100644 --- a/src/server.py +++ b/src/server.py @@ -18,65 +18,84 @@ from src.settings import ServerSettings -# Load environment variables from .env file -env = ServerSettings() -app = Flask(__name__) -CORS(app) - - -@app.route("/help") -def serve_help(): - return send_from_directory(f"{str(Path(__file__).parent)}/", "help.txt") - - -@app.route("/home") -def serve_index(): - return render_template("index.html", env_vars=env.model_dump()) - - -@app.route("/script.js") -def serve_script(): - return send_file("static/script.js") - - -@app.route("/") -def default_route(): - query_parameters = urllib.parse.parse_qsl( - request.query_string.decode(), keep_blank_values=True - ) - parsed_parameters = [] - - for key, value in query_parameters: - if value: - parsed_parameters.append(f"{key}={value}") - else: - parsed_parameters.append(key) - - # Join the parsed parameters list into a single string - args = ",".join(parsed_parameters) - - async def run_subprocess(): - try: - result = subprocess.run( - ["python3", "src/cli.py", args], - capture_output=True, - text=True, - check=True, - ) - return result.stdout - except subprocess.CalledProcessError as e: - # Print the error message from the subprocess - print("Error message from subprocess:", e.stderr) - # Raise the error again to propagate it - raise e - - # Run subprocess asynchronously - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) - result = loop.run_until_complete(run_subprocess()) - return result +def create_app(env): + """ + Application factory function + """ + + # Load environment variables from .env file + + app = Flask(__name__) + CORS(app) + + @app.route("/help") + def serve_help(): + """ + Servers the help.txt file + """ + return send_from_directory( + Path(__file__).resolve().parents[1], "help.txt" + ) + + @app.route("/home") + def serve_index(): + """ + Servers index.html + """ + return render_template("index.html", env_vars=env.model_dump()) + + @app.route("/script.js") + def serve_script(): + """ + Servers javascript + """ + return send_file("static/script.js") + + @app.route("/") + def default_route(): + """ + Default route, serves surf report + """ + query_parameters = urllib.parse.parse_qsl( + request.query_string.decode(), keep_blank_values=True + ) + parsed_parameters = [] + + for key, value in query_parameters: + if value: + parsed_parameters.append(f"{key}={value}") + else: + parsed_parameters.append(key) + + # Join the parsed parameters list into a single string + args = ",".join(parsed_parameters) + + async def run_subprocess(): + try: + result = subprocess.run( + ["python3", "src/cli.py", args], + capture_output=True, + text=True, + check=True, + ) + return result.stdout + except subprocess.CalledProcessError as e: + # Print the error message from the subprocess + print("Error message from subprocess:", e.stderr) + # Raise the error again to propagate it + raise e + + # Run subprocess asynchronously + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + result = loop.run_until_complete(run_subprocess()) + return result + + return app if __name__ == "__main__": + env = ServerSettings() + app = create_app(env) app.run(host="0.0.0.0", port=env.PORT, debug=env.DEBUG) diff --git a/tests/test_server.py b/tests/test_server.py index b61c3aa..dd8eace 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -3,3 +3,29 @@ Make sure pytest is installed: pip install pytest Run pytest: pytest """ + +from src import settings +from src.server import create_app + + +def test_routes(): + """ + Test that the routes are able to be retrieved + /home, /help, / + When a page is requested (GET) + THEN check if the response is valid (200) + """ + env = settings.ServerSettings() + flask_app = create_app(env) + OK = 200 + + # Create a test client using the Flask application configured for testing + with flask_app.test_client() as test_client: + response_help = test_client.get("/help") + assert response_help.status_code == OK + + response_home = test_client.get("/home") + assert response_home.status_code == OK + + response_root = test_client.get("/") + assert response_root.status_code == OK