From f7e9e680a75cc473a9eab0797bcf4b24518eec83 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Tue, 10 Oct 2023 11:29:43 -0400 Subject: [PATCH] Remove session protecting during testing --- htmx_contact/__init__.py | 2 +- tests/conftest.py | 36 +++++++++++++++++++++++++++++++++++- tests/test_routes.py | 12 ++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/htmx_contact/__init__.py b/htmx_contact/__init__.py index 9b138a1..7913c16 100644 --- a/htmx_contact/__init__.py +++ b/htmx_contact/__init__.py @@ -12,7 +12,7 @@ Session = sessionmaker(engine) # Configure Authentication login_manager = LoginManager() -login_manager.session_protection = "strong" +login_manager.session_protection = "basic" login_manager.login_view = "user.user_login" diff --git a/tests/conftest.py b/tests/conftest.py index 1b4cf69..637204e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,6 +5,7 @@ from flask_login.test_client import FlaskLoginClient from htmx_contact import create_app from htmx_contact.config import ContactSettings +from htmx_contact.models import Contact from htmx_contact.models import User @@ -18,6 +19,36 @@ def test_user(): return User(id=1, primary_email="test", password="test", username="test") +@pytest.fixture() +def test_contacts(test_user): + return [ + Contact( + id=1, + user_id=test_user.id, + first_name="Mark", + last_name="Zuckerberg", + email="zuck@facebook.com", + phone="555-777-1337", + ), + Contact( + id=2, + user_id=test_user.id, + first_name="Shaan", + last_name="Puri", + email="shaan@twitter.com", + phone="666-777-1337", + ), + Contact( + id=3, + user_id=test_user.id, + first_name="Sam", + last_name="Par", + email="sam@thesampar.com", + phone="587-343-1337", + ), + ] + + @pytest.fixture() def app(test_config): app = create_app(config=test_config) @@ -25,6 +56,8 @@ def app(test_config): app.config.update( { "TESTING": True, + # disbale session protection or session "freshness" will cause failures. + "SESSION_PROTECTION": None, } ) @@ -42,7 +75,8 @@ def anynomous_client(app): @pytest.fixture() def client(app, test_user): - return app.test_client(user=test_user) + with app.test_client(user=test_user) as client: + yield client @pytest.fixture() diff --git a/tests/test_routes.py b/tests/test_routes.py index 30ab975..57a2a93 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -13,6 +13,18 @@ def test_login_get_contacts(anynomous_client): assert response.status_code == 302 +def test_contacts(client, test_contacts): + mock_session_instance = Mock() + mock_session_instance.configure_mock(**{"scalars.return_value.all.return_value": test_contacts}) + with patch("htmx_contact.main.Session") as sqla_session: + sqla_session.configure_mock(**{"return_value.__enter__.return_value": mock_session_instance}) + response = client.get("/contacts") + assert response.status_code == 200 + # check they all are in there + for contact in test_contacts: + assert contact.first_name in response.text + + def test_get_contacts(client): with client.session_transaction() as session: response = client.get("/contacts", follow_redirects=True)