|
|
|
@ -1,3 +1,7 @@
|
|
|
|
|
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
|
|
|
|
@ -10,5 +14,35 @@ def test_login_get_contacts(anynomous_client):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_contacts(client):
|
|
|
|
|
response = client.get("/contacts", follow_redirects=True)
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
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
|
|
|
|
|