from unittest.mock import Mock from unittest.mock import patch def test_index_redirect_to_contacts(client): response = client.get("/") assert response.status_code == 302 assert response.headers.get("Location") == "/contacts" def test_login_get_contacts(anynomous_client): response = anynomous_client.get("/contacts", follow_redirects=False) assert response.status_code == 302 def test_get_contacts(client): with client.session_transaction() as session: response = client.get("/contacts", follow_redirects=True) assert response.status_code == 200 assert session["_user_id"] == 1 def test_user_logout(client): # need an active request context to check session. with client.session_transaction() as session: assert session["_user_id"] == 1 response = client.get("/user/logout", follow_redirects=False) assert response.status_code == 302 with client.session_transaction() as session: assert session.get("_user_id") is None def test_user_login(anynomous_client, test_user): with patch("htmx_contact.user.Session") as sqla_session: mock_session_instance = Mock(name="slqasession") mock_session_instance.configure_mock(**{"scalar.return_value": test_user}) sqla_session.configure_mock(**{"return_value.__enter__.return_value": mock_session_instance}) with anynomous_client.session_transaction() as session: assert session.get("_user_id") is None response = anynomous_client.post("/user/login", data={"email": "test", "password": "test"}) assert response.status_code == 302 with anynomous_client.session_transaction() as session: assert session.get("_user_id") == test_user.id