2 Commits

Author SHA1 Message Date
96c26d0ead I can register, login, and logout 2025-10-27 22:17:21 +01:00
3f3e3a6ae5 I can login test is now working 2025-10-27 21:47:07 +01:00
3 changed files with 81 additions and 17 deletions

View File

@@ -15,8 +15,13 @@ clean-build: clean-package
find . -name "*.pyo" -exec rm -f {} +
clean-tests:
rm -rf .sesskey
rm -rf tests/.sesskey
rm -rf tests/Users.db
rm -rf tests/*.db
# Alias to clean everything
clean: clean-build clean-tests
clean: clean-build clean-tests
clean-all : clean
rm -rf src/.sesskey
rm -rf src/Users.db

View File

@@ -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,17 +210,17 @@ 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
)
if response.status_code == 200:
if response.status_code in (200, 201):
return response.json()
return None
except httpx.HTTPError:
except httpx.HTTPError as ex:
return None
@@ -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

View File

@@ -1,18 +1,38 @@
import os
from dataclasses import dataclass
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
@dataclass
class DummyUser:
email: str
username: str
password: str
@pytest.fixture()
def registered_user():
user = DummyUser("user@email.com", "user", "#Passw0rd")
register_user(user.email, user.username, user.password)
return user
@pytest.fixture()
def app():
beforeware = create_auth_beforeware()
test_app, test_rt = fast_app(before=beforeware)
setup_auth_routes(test_app, test_rt, mount_auth_app=True, sqlite_db_path="TestUsers.db")
@test_rt('/')
def index(): return "You are now logged in !"
return test_app
@@ -24,7 +44,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 +73,43 @@ 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):
# create user
register_user("user@email.com", "user", "#Passw0rd")
def test_i_can_login_with_correct_credentials(user, registered_user):
user.open("/login")
form = user.find_form(fields=["Email", "Password"])
form.fill(Email="user@email.com", Password="#Passw0rd")
form.fill(Email=registered_user.email, Password=registered_user.password)
form.submit()
user.should_see("You are now logged in")
user.should_see("You are now logged in !")
def test_i_can_can_navigate_once_logged_in(user, registered_user):
user.open("/welcome") # not logged in, redirects to login
user.should_see("Sign In")
form = user.find_form(fields=["Email", "Password"])
form.fill(Email=registered_user.email, Password=registered_user.password)
form.submit()
user.open("/welcome") # once logged in, welcome page is accessible
user.should_see("Welcome back, user@email.com!") # welcome page is predefined
def test_i_can_register(user):
user.open("/register")
form = user.find_form(fields=["Email", "Username", "Password"])
form.fill(Email="user@email.com", Username="username", Password="#Passw0rd", confirm_password="#Passw0rd")
form.submit()
user.should_see("You are now logged in !")
def test_i_can_logout(user, registered_user):
user.open("/login")
form = user.find_form(fields=["Email", "Password"])
form.fill(Email=registered_user.email, Password=registered_user.password)
form.submit()
user.open("/logout")
user.should_see("Sign In")
user.open("/welcome")
user.should_see("Sign In")