Add and fix tests
This commit is contained in:
@@ -269,11 +269,10 @@ async def new_product_post(
|
|||||||
session: SessionDep,
|
session: SessionDep,
|
||||||
user: UserDep,
|
user: UserDep,
|
||||||
product_data: Annotated[types.Product, Form()],
|
product_data: Annotated[types.Product, Form()],
|
||||||
# product_image: Annotated[bytes, File()]
|
|
||||||
):
|
):
|
||||||
if not user.has_permission("product", "edit"):
|
if not user.has_permission("product", "edit"):
|
||||||
raise HTTPException(status_code=403, detail="Insufficient permissions")
|
raise HTTPException(status_code=403, detail="Insufficient permissions")
|
||||||
# print(len(product_image))
|
|
||||||
product = Product()
|
product = Product()
|
||||||
|
|
||||||
for field_name, data in product_data.model_dump().items():
|
for field_name, data in product_data.model_dump().items():
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
from sqlalchemy import create_engine, select
|
from sqlalchemy import create_engine, select
|
||||||
from sqlalchemy.orm import Session, sessionmaker
|
from sqlalchemy.orm import Session, sessionmaker
|
||||||
@@ -40,10 +41,12 @@ def ensure_user(user_info: dict, session: Session) -> User:
|
|||||||
session.add(user)
|
session.add(user)
|
||||||
session.flush()
|
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
|
# when in demo mode each user gets an account with some starting balance
|
||||||
account = Account(name=f"Demokonto für {user.display_name}")
|
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)
|
user.accounts.append(account)
|
||||||
session.flush()
|
session.flush()
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,8 @@ class Account(Base):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def balance(self):
|
def balance(self):
|
||||||
|
for t in self.transactions:
|
||||||
|
print(t)
|
||||||
return sum(t.total_amount for t in self.transactions)
|
return sum(t.total_amount for t in self.transactions)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -78,3 +78,11 @@ def test_db():
|
|||||||
test_session = None
|
test_session = None
|
||||||
db.rollback()
|
db.rollback()
|
||||||
db.close()
|
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)
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
@@ -5,7 +7,14 @@ from sqlalchemy.orm import Session
|
|||||||
|
|
||||||
from allmende_payment_system.app import app
|
from allmende_payment_system.app import app
|
||||||
from allmende_payment_system.database import ensure_user
|
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
|
@pytest.fixture
|
||||||
@@ -16,6 +25,7 @@ def admin_user(test_db):
|
|||||||
group = UserGroup(id=1, name="Admins")
|
group = UserGroup(id=1, name="Admins")
|
||||||
group.permissions.append(Permission(scope="user", action="edit"))
|
group.permissions.append(Permission(scope="user", action="edit"))
|
||||||
group.permissions.append(Permission(scope="account", action="edit"))
|
group.permissions.append(Permission(scope="account", action="edit"))
|
||||||
|
group.permissions.append(Permission(scope="product", action="edit"))
|
||||||
user.user_groups.append(group)
|
user.user_groups.append(group)
|
||||||
test_db.add(group)
|
test_db.add(group)
|
||||||
test_db.flush()
|
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()
|
account = test_db.execute(select(Account).where(Account.id == account.id)).scalar()
|
||||||
assert account.balance == 100.00
|
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")
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
|
import os
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
from allmende_payment_system.models import Account
|
from allmende_payment_system.models import Account
|
||||||
|
|
||||||
|
|
||||||
def test_unauthorized_access(unauthorized_client, test_db):
|
def test_unauthorized_access(unauthorized_client, test_db):
|
||||||
response = unauthorized_client.get("/")
|
# 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
|
assert response.status_code == 401
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user