From 1ca82abcdb1545aebd0c97bae3ff0fe7d75cf623 Mon Sep 17 00:00:00 2001 From: Pierre Bastianelli Date: Wed, 7 Oct 2026 18:21:53 -0700 Subject: [PATCH] chore: testing new routes --- src/makeitso/config.py | 2 ++ tests/conftest.py | 10 +++++++++ tests/stacks/test_routes.py | 41 +++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 tests/stacks/test_routes.py diff --git a/src/makeitso/config.py b/src/makeitso/config.py index b9f0ead..0ea2322 100644 --- a/src/makeitso/config.py +++ b/src/makeitso/config.py @@ -62,6 +62,8 @@ class PytestConfig(LocalConfig): SECRET_KEY = "pytest" # Fixed name, so tests never wipe the real database SQLALCHEMY_DATABASE_URI = _database_uri("mis_test") + # Disable CSRF so we can run post() requests + WTF_CSRF_ENABLED = False configs = { diff --git a/tests/conftest.py b/tests/conftest.py index 9c9be00..cd2827c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -20,6 +20,16 @@ def client(app): return app.test_client() +@pytest.fixture +def logged_in_client(app): + client = app.test_client() + + with client.session_transaction() as session: + session["user"] = {"login": "testuser", "avatar_url": ""} + + return client + + # Empty every table after each test, so tests don't see each other's rows @pytest.fixture(autouse=True) def clean_db(app): diff --git a/tests/stacks/test_routes.py b/tests/stacks/test_routes.py new file mode 100644 index 0000000..5e430df --- /dev/null +++ b/tests/stacks/test_routes.py @@ -0,0 +1,41 @@ +from unittest.mock import MagicMock, patch + +from makeitso.extensions import db +from makeitso.models.stack import Stack + + +def test_new(logged_in_client): + response = logged_in_client.get("/stacks/new") + assert response.status_code == 200 + + assert b"New stack" in response.data + assert b"Create stack" in response.data + + +def test_new_create( + logged_in_client, +): + with ( + patch("makeitso.stacks.views.new.enqueue_sync") as mock_enqueue_sync, + patch("flask_wtf.form.FlaskForm.validate_on_submit") as mock_validate, + ): + mock_validate.return_value = True + + data = { + "organization": "test-org", + "repository": "test-repo", + "branch": "test-branch", + "environment": "testing-test", + "continuous_deploy": False, + } + + response = logged_in_client.post("/stacks/new", data=data) + + new_stack: Stack = db.session.execute( + db.select(Stack).filter_by(repository="test-repo") + ).scalar_one() + + Stack.repository + + assert response.status_code == 302 + assert response.headers["Location"] == f"/stacks/{new_stack.id}"