Add and fix tests

This commit is contained in:
2026-03-14 15:34:33 +01:00
parent cb098cb139
commit 171f4a0b9c
6 changed files with 61 additions and 6 deletions

View File

@@ -269,11 +269,10 @@ async def new_product_post(
session: SessionDep,
user: UserDep,
product_data: Annotated[types.Product, Form()],
# product_image: Annotated[bytes, File()]
):
if not user.has_permission("product", "edit"):
raise HTTPException(status_code=403, detail="Insufficient permissions")
# print(len(product_image))
product = Product()
for field_name, data in product_data.model_dump().items():

View File

@@ -1,4 +1,5 @@
import os
from decimal import Decimal
from sqlalchemy import create_engine, select
from sqlalchemy.orm import Session, sessionmaker
@@ -40,10 +41,12 @@ def ensure_user(user_info: dict, session: Session) -> User:
session.add(user)
session.flush()
if not "APS_PRODUCTION_MODE" in os.environ:
if not "APS_PRODUCTION_MODE" in os.environ and not "APS_TESTRUN" in os.environ:
# when in demo mode each user gets an account with some starting balance
account = Account(name=f"Demokonto für {user.display_name}")
account.transactions.append(Transaction(total_amount=100.0, type="deposit"))
account.transactions.append(
Transaction(total_amount=Decimal(100), type="deposit")
)
user.accounts.append(account)
session.flush()

View File

@@ -35,6 +35,8 @@ class Account(Base):
@property
def balance(self):
for t in self.transactions:
print(t)
return sum(t.total_amount for t in self.transactions)

View File

@@ -78,3 +78,11 @@ def test_db():
test_session = None
db.rollback()
db.close()
@pytest.fixture(scope="session", autouse=True)
def test_env():
"""Set up environment variables for testing"""
os.environ["APS_TESTRUN"] = "true"
yield
os.environ.pop("APS_TESTRUN", None)

View File

@@ -1,3 +1,5 @@
from decimal import Decimal
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import select
@@ -5,7 +7,14 @@ from sqlalchemy.orm import Session
from allmende_payment_system.app import app
from allmende_payment_system.database import ensure_user
from allmende_payment_system.models import Account, Permission, User, UserGroup
from allmende_payment_system.models import (
Account,
Area,
Permission,
Product,
User,
UserGroup,
)
@pytest.fixture
@@ -16,6 +25,7 @@ def admin_user(test_db):
group = UserGroup(id=1, name="Admins")
group.permissions.append(Permission(scope="user", action="edit"))
group.permissions.append(Permission(scope="account", action="edit"))
group.permissions.append(Permission(scope="product", action="edit"))
user.user_groups.append(group)
test_db.add(group)
test_db.flush()
@@ -187,3 +197,31 @@ def test_add_balance_to_account(test_db, client, admin_user):
account = test_db.execute(select(Account).where(Account.id == account.id)).scalar()
assert account.balance == 100.00
### area and product management
def test_add_product(test_db, client, admin_user):
area = Area(name="Test Area", description="An area for testing")
test_db.add(area)
test_db.flush()
response = client.post(
"/admin/products/new",
data={
"name": "Test Product",
"vat_rate": "19.00",
"unit_of_measure": "piece",
"price": 9.99,
"area_id": area.id,
},
user=admin_user,
follow_redirects=False,
)
assert response.status_code == 303
product = test_db.execute(
select(Product).where(Product.name == "Test Product")
).scalar()
assert product is not None
assert product.name == "Test Product"
assert product.price == Decimal("9.99")

View File

@@ -1,7 +1,12 @@
import os
from unittest import mock
from allmende_payment_system.models import Account
def test_unauthorized_access(unauthorized_client, test_db):
# enable production mode to test the authentication mechanism
with mock.patch.dict(os.environ, {"APS_PRODUCTION_MODE": "true"}, clear=False):
response = unauthorized_client.get("/")
assert response.status_code == 401