Add first info to landing page

This commit is contained in:
2025-10-23 12:35:52 +02:00
parent f6e69b1521
commit 81929cca21
11 changed files with 186 additions and 57 deletions

View File

@@ -1,30 +1,35 @@
import pytest
from fastapi import Request
from fastapi.testclient import TestClient
import os
from allmende_payment_system.app import create_app
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from allmende_payment_system.app import app
from allmende_payment_system.models import Base
@pytest.fixture(scope="session")
def client():
app = create_app()
async def add_ynh_headers(request: Request, call_next):
username = request.headers.get("APS-TEST-username", "test")
# This seems to work although headers are immutable
# If this ever turns out to be a problem, we can use request.state instead,
# but will have to modify app.get_user
request.headers._list.append((b"ynh_user", username.encode("utf-8")))
response = await call_next(request)
return response
app.middleware("http")(add_ynh_headers)
os.environ["APS_username"] = "test"
return TestClient(app)
@pytest.fixture(scope="session")
def unauthorized_client():
app = create_app()
os.environ.pop("APS_username", None)
return TestClient(app)
@pytest.fixture
def test_db():
engine = create_engine("sqlite:///:memory:")
Base.metadata.create_all(bind=engine) # Create tables
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Provide a session and the engine
db = TestingSessionLocal()
try:
yield db
finally:
db.close()

View File

@@ -6,7 +6,6 @@ def test_unauthorized_access(unauthorized_client):
assert response.status_code == 401
def test_unauthorized_access(unauthorized_client):
response = unauthorized_client.get("/")
print(response.text)
assert response.status_code == 401
def test_authorized_access(client):
response = client.get("/")
assert response.status_code == 200

19
test/test_database.py Normal file
View File

@@ -0,0 +1,19 @@
from sqlalchemy import func, select
from allmende_payment_system.database import ensure_user
from allmende_payment_system.models import User
def test_ensure_user(test_db):
user_info = {"username": "test", "display_name": "Test User"}
user = ensure_user(user_info, test_db)
assert user.username == "test"
test_db.commit()
assert test_db.scalar(select(func.count()).select_from(User)) == 1
user = ensure_user(user_info, test_db)
assert test_db.scalar(select(func.count()).select_from(User)) == 1

View File

@@ -1,35 +1,16 @@
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from allmende_payment_system.models import Account, Base, User
from allmende_payment_system.models import Account, User
# Create an in-memory SQLite database
@pytest.fixture
def in_memory_db():
engine = create_engine("sqlite:///:memory:")
Base.metadata.create_all(bind=engine) # Create tables
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Provide a session and the engine
db = TestingSessionLocal()
try:
yield db
finally:
db.close()
def test_user_model(in_memory_db):
def test_user_model(test_db):
user = User(username="test", display_name="Test User")
in_memory_db.add(user)
in_memory_db.commit()
test_db.add(user)
test_db.commit()
assert user.id is not None
account = Account(name="Test Account")
account.users.append(user)
in_memory_db.add(account)
in_memory_db.commit()
test_db.add(account)
test_db.commit()
assert len(user.accounts) == 1