feat: wire auth into main app, protect landing page, add create_admin script
This commit is contained in:
+56
-13
@@ -1,31 +1,74 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
from app.core.auth import create_access_token
|
||||
from app.core.database import Base, get_db
|
||||
from app.main import app
|
||||
|
||||
client = TestClient(app)
|
||||
from app.modules.auth.models import User
|
||||
from app.modules.auth.service import hash_password
|
||||
|
||||
|
||||
def test_landing_returns_html():
|
||||
@pytest.fixture(autouse=True)
|
||||
def override_db():
|
||||
engine = create_engine(
|
||||
"sqlite:///:memory:",
|
||||
connect_args={"check_same_thread": False},
|
||||
poolclass=StaticPool,
|
||||
)
|
||||
Base.metadata.create_all(bind=engine)
|
||||
Session = sessionmaker(bind=engine)
|
||||
session = Session()
|
||||
app.dependency_overrides[get_db] = lambda: session
|
||||
yield session
|
||||
app.dependency_overrides.clear()
|
||||
session.close()
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
return TestClient(app, follow_redirects=False)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def auth_cookies(override_db):
|
||||
user = User(username="testuser", full_name="Test User", pw_hash=hash_password("pw"))
|
||||
override_db.add(user)
|
||||
override_db.commit()
|
||||
token = create_access_token(username="testuser", is_admin=False)
|
||||
return {"access_token": token}
|
||||
|
||||
|
||||
def test_landing_without_auth_redirects(client):
|
||||
response = client.get("/")
|
||||
assert response.status_code == 307
|
||||
assert "/auth/login" in response.headers["location"]
|
||||
|
||||
|
||||
def test_landing_returns_html(client, auth_cookies):
|
||||
response = client.get("/", cookies=auth_cookies)
|
||||
assert response.status_code == 200
|
||||
assert "text/html" in response.headers["content-type"]
|
||||
|
||||
|
||||
def test_landing_contains_title():
|
||||
response = client.get("/")
|
||||
def test_landing_contains_title(client, auth_cookies):
|
||||
response = client.get("/", cookies=auth_cookies)
|
||||
assert "University Process Hub" in response.text
|
||||
|
||||
|
||||
def test_landing_contains_rss_module():
|
||||
response = client.get("/")
|
||||
def test_landing_contains_rss_module(client, auth_cookies):
|
||||
response = client.get("/", cookies=auth_cookies)
|
||||
assert "RSS-Feed Server" in response.text
|
||||
|
||||
|
||||
def test_landing_navbar_links_present():
|
||||
response = client.get("/")
|
||||
def test_landing_navbar_links_present(client, auth_cookies):
|
||||
response = client.get("/", cookies=auth_cookies)
|
||||
assert "Übersicht" in response.text
|
||||
assert "RSS-Feeds" in response.text
|
||||
|
||||
|
||||
def test_landing_info_strip_shows_db_mode():
|
||||
response = client.get("/")
|
||||
assert "SQLite" in response.text or "MariaDB" in response.text
|
||||
def test_landing_info_strip_shows_db_mode(client, auth_cookies):
|
||||
response = client.get("/", cookies=auth_cookies)
|
||||
assert "SQLite" in response.text or "MariaDB" in response.text
|
||||
|
||||
Reference in New Issue
Block a user