From 88c7fb53ad6808a83476f73394b972511af18b09 Mon Sep 17 00:00:00 2001 From: Ryan Frederich Date: Thu, 25 Jul 2024 09:49:52 -0700 Subject: [PATCH 1/5] tests for flask server --- src/server.py | 134 +++++++++++++++++++++++++------------------ tests/test_server.py | 24 ++++++++ 2 files changed, 101 insertions(+), 57 deletions(-) diff --git a/src/server.py b/src/server.py index d14fb1b..2ddad94 100644 --- a/src/server.py +++ b/src/server.py @@ -18,65 +18,85 @@ 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(): + """ + Application factory function + """ + + # Load environment variables from .env file + env = ServerSettings() + + 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__": + app = create_app() + env = ServerSettings() 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..6089a52 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -3,3 +3,27 @@ Make sure pytest is installed: pip install pytest Run pytest: pytest """ + +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) + """ + flask_app = create_app() + 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 From 09a0d2147a18cd21a76d0fcb8a6357bd106d43fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9D=95=82?= <51281148+K-dash@users.noreply.github.com> Date: Tue, 30 Jul 2024 07:19:37 +0900 Subject: [PATCH 2/5] Update src/server.py --- src/server.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/server.py b/src/server.py index 2ddad94..33db727 100644 --- a/src/server.py +++ b/src/server.py @@ -99,4 +99,5 @@ async def run_subprocess(): if __name__ == "__main__": app = create_app() env = ServerSettings() + app = create_app(env) app.run(host="0.0.0.0", port=env.PORT, debug=env.DEBUG) From edb0b3cf1b35c11209ab73ecea95ddbb4a127287 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9D=95=82?= <51281148+K-dash@users.noreply.github.com> Date: Tue, 30 Jul 2024 07:20:32 +0900 Subject: [PATCH 3/5] Update src/server.py --- src/server.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/server.py b/src/server.py index 33db727..76c72c2 100644 --- a/src/server.py +++ b/src/server.py @@ -25,7 +25,6 @@ def create_app(): """ # Load environment variables from .env file - env = ServerSettings() app = Flask(__name__) CORS(app) From 7edc957eb9d3e1812d8057f02c22511d749eb5e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9D=95=82?= <51281148+K-dash@users.noreply.github.com> Date: Tue, 30 Jul 2024 07:20:39 +0900 Subject: [PATCH 4/5] Update tests/test_server.py --- tests/test_server.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_server.py b/tests/test_server.py index 6089a52..bbddf14 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -14,7 +14,8 @@ def test_routes(): When a page is requested (GET) THEN check if the response is valid (200) """ - flask_app = create_app() + env = ServerSettings() + flask_app = create_app(env) OK = 200 # Create a test client using the Flask application configured for testing From ad42440d77d43ae3bdac3b23286769f09078f3b9 Mon Sep 17 00:00:00 2001 From: Ryan Frederich Date: Tue, 30 Jul 2024 10:11:53 -0700 Subject: [PATCH 5/5] lint/format errors --- src/server.py | 3 +-- tests/test_server.py | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/server.py b/src/server.py index 76c72c2..9524d3e 100644 --- a/src/server.py +++ b/src/server.py @@ -19,7 +19,7 @@ from src.settings import ServerSettings -def create_app(): +def create_app(env): """ Application factory function """ @@ -96,7 +96,6 @@ async def run_subprocess(): if __name__ == "__main__": - app = create_app() 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 bbddf14..dd8eace 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -4,6 +4,7 @@ Run pytest: pytest """ +from src import settings from src.server import create_app @@ -14,7 +15,7 @@ def test_routes(): When a page is requested (GET) THEN check if the response is valid (200) """ - env = ServerSettings() + env = settings.ServerSettings() flask_app = create_app(env) OK = 200