You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.3 KiB
Python

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_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)
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