diff --git a/src/myfasthtml/auth/utils.py b/src/myfasthtml/auth/utils.py index 5567995..b59686d 100644 --- a/src/myfasthtml/auth/utils.py +++ b/src/myfasthtml/auth/utils.py @@ -33,6 +33,8 @@ DEFAULT_SKIP_PATTERNS = [ '/logout', ] +http_client = httpx + def create_auth_beforeware(additional_patterns: Optional[List[str]] = None) -> Beforeware: """ @@ -174,7 +176,7 @@ def login_user(email: str, password: str) -> Optional[Dict[str, Any]]: None if authentication fails """ try: - response = httpx.post( + response = http_client.post( f"{API_BASE_URL}/auth/login", data={"username": email, "password": password}, headers={"Content-Type": "application/x-www-form-urlencoded"}, @@ -200,6 +202,7 @@ def register_user(email: str, username: str, password: str) -> Optional[Dict[str Args: email: User email address + username: User name password: User password Returns: @@ -207,9 +210,9 @@ def register_user(email: str, username: str, password: str) -> Optional[Dict[str None if registration fails """ try: - response = httpx.post( + response = http_client.post( f"{API_BASE_URL}/auth/register", - json={"email": email, "username": username, "password": password}, + json={"email": email, "username": username, "password": password}, timeout=10.0 ) @@ -233,7 +236,7 @@ def refresh_access_token(refresh_token: str) -> Optional[Dict[str, Any]]: None if refresh fails """ try: - response = httpx.post( + response = http_client.post( f"{API_BASE_URL}/auth/refresh", json={"refresh_token": refresh_token}, timeout=10.0 @@ -263,7 +266,7 @@ def get_user_info(access_token: str) -> Optional[Dict[str, Any]]: None if request fails """ try: - response = httpx.get( + response = http_client.get( f"{API_BASE_URL}/auth/me", headers={"Authorization": f"Bearer {access_token}"}, timeout=10.0 @@ -288,7 +291,7 @@ def logout_user(refresh_token: str) -> bool: True if logout successful, False otherwise """ try: - response = httpx.post( + response = http_client.post( f"{API_BASE_URL}/auth/logout", json={"refresh_token": refresh_token}, timeout=10.0 diff --git a/tests/auth/test_login.py b/tests/auth/test_login.py index 0017f54..80e8c7f 100644 --- a/tests/auth/test_login.py +++ b/tests/auth/test_login.py @@ -3,6 +3,7 @@ import os import pytest from fasthtml.fastapp import fast_app +import myfasthtml.auth.utils from myfasthtml.auth.routes import setup_auth_routes from myfasthtml.auth.utils import create_auth_beforeware, register_user from myfasthtml.core.testclient import MyTestClient @@ -24,7 +25,12 @@ def rt(app): @pytest.fixture() def user(app): user = MyTestClient(app) - return user + previous = myfasthtml.auth.utils.http_client + myfasthtml.auth.utils.http_client = user.client + + yield user + + myfasthtml.auth.utils.http_client = previous @pytest.fixture(autouse=True) @@ -48,12 +54,15 @@ def test_i_cannot_login_with_wrong_credentials(user): user.should_see("Invalid email or password. Please try again.") -def test_i_can_login_with_correct_credentials(user): +def test_i_can_login_with_correct_credentials(user, rt): # create user register_user("user@email.com", "user", "#Passw0rd") + @rt('/') + def index(): return "You are now logged in !" + user.open("/login") form = user.find_form(fields=["Email", "Password"]) form.fill(Email="user@email.com", Password="#Passw0rd") form.submit() - user.should_see("You are now logged in") + user.should_see("You are now logged in !")