Files
MyFastHtml/tests/auth/test_login.py

69 lines
1.8 KiB
Python

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
@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")
return test_app
@pytest.fixture()
def rt(app):
return app.route
@pytest.fixture()
def user(app):
user = MyTestClient(app)
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)
def cleanup():
if os.path.exists("TestUsers.db"):
os.remove("TestUsers.db")
def test_i_can_see_login_page(user):
user.open("/login")
user.should_see("Sign In")
user.should_see("Register here")
user.find_form(fields=["Email", "Password"])
def test_i_cannot_login_with_wrong_credentials(user):
user.open("/login")
form = user.find_form(fields=["Email", "Password"])
form.fill(Email="user@email.com", Password="#Passw0rd")
form.submit()
user.should_see("Invalid email or password. Please try again.")
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 !")