import pytest from l4d2web.app import create_app from l4d2web.auth import hash_password from l4d2web.db import init_db, session_scope from l4d2web.models import User @pytest.fixture def app_and_user(tmp_path, monkeypatch): db_url = f"sqlite:///{tmp_path/'profile.db'}" monkeypatch.setenv("DATABASE_URL", db_url) app = create_app({"TESTING": True, "DATABASE_URL": db_url, "SECRET_KEY": "test"}) init_db() with session_scope() as db: u = User(username="alice", password_digest=hash_password("currentpass")) db.add(u) db.flush() uid = u.id marker = u.password_changed_at.isoformat() return app, uid, marker def _logged_in_client(app, uid, marker): client = app.test_client() with client.session_transaction() as sess: sess["user_id"] = uid sess["pw_changed_at"] = marker sess["csrf_token"] = "test-token" return client def test_profile_requires_login(app_and_user): app, _, _ = app_and_user response = app.test_client().get("/profile", follow_redirects=False) assert response.status_code == 302 assert "/login" in response.headers["Location"] def test_profile_page_renders(app_and_user): app, uid, marker = app_and_user client = _logged_in_client(app, uid, marker) response = client.get("/profile") assert response.status_code == 200 body = response.get_data(as_text=True) assert "Change password" in body assert 'name="current_password"' in body assert 'name="new_password"' in body assert 'name="confirm_new_password"' in body def test_base_template_links_username_to_profile(app_and_user): app, uid, marker = app_and_user client = _logged_in_client(app, uid, marker) response = client.get("/dashboard") body = response.get_data(as_text=True) assert 'href="/profile"' in body assert ">alice<" in body