Add authorization
This commit is contained in:
30
test/conftest.py
Normal file
30
test/conftest.py
Normal 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
12
test/test_auth.py
Normal 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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user