Add authorization

This commit is contained in:
2025-10-23 11:22:27 +02:00
parent a1563b53ac
commit e1c8b4ebeb
7 changed files with 82 additions and 9 deletions

30
test/conftest.py Normal file
View File

@@ -0,0 +1,30 @@
import pytest
from fastapi import Request
from fastapi.testclient import TestClient
from allmende_payment_system.app import create_app
@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)
return TestClient(app)
@pytest.fixture(scope="session")
def unauthorized_client():
app = create_app()
return TestClient(app)

12
test/test_auth.py Normal file
View File

@@ -0,0 +1,12 @@
from allmende_payment_system.models import Account
def test_unauthorized_access(unauthorized_client):
response = unauthorized_client.get("/")
assert response.status_code == 401
def test_unauthorized_access(unauthorized_client):
response = unauthorized_client.get("/")
print(response.text)
assert response.status_code == 401

View File

@@ -1,8 +1,9 @@
# tests/conftest.py
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from allmende_payment_system.models import Base, User
from sqlalchemy.orm import sessionmaker
from allmende_payment_system.models import Account, Base, User
# Create an in-memory SQLite database
@pytest.fixture
@@ -19,9 +20,16 @@ def in_memory_db():
db.close()
def test_create_user(in_memory_db):
def test_user_model(in_memory_db):
user = User(username="test", display_name="Test User")
in_memory_db.add(user)
in_memory_db.commit()
assert user.id is not None
assert user.id is not None
account = Account(name="Test Account")
account.users.append(user)
in_memory_db.add(account)
in_memory_db.commit()
assert len(user.accounts) == 1