Files
orion/tests/fixtures/store_fixtures.py
Samir Boulahtit 1dcb0e6c33
Some checks failed
CI / ruff (push) Successful in 11s
CI / validate (push) Has been cancelled
CI / dependency-scanning (push) Has been cancelled
CI / docs (push) Has been cancelled
CI / deploy (push) Has been cancelled
CI / pytest (push) Has been cancelled
feat: RBAC Phase 1 — consolidate user roles into 4-value enum
Consolidate User.role (2-value: admin/store) + User.is_super_admin (boolean)
into a single 4-value UserRole enum: super_admin, platform_admin,
merchant_owner, store_member. Drop stale StoreUser.user_type column.
Fix role="user" bug in merchant creation.

Key changes:
- Expand UserRole enum from 2 to 4 values with computed properties
  (is_admin, is_super_admin, is_platform_admin, is_merchant_owner, is_store_user)
- Add Alembic migration (tenancy_003) for data migration + column drops
- Remove is_super_admin from JWT token payload
- Update all auth dependencies, services, routes, templates, JS, and tests
- Update all RBAC documentation

66 files changed, 1219 unit tests passing.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 22:44:29 +01:00

255 lines
6.7 KiB
Python

# tests/fixtures/store_fixtures.py
"""
Store-related test fixtures.
Note: Fixtures should NOT use db.expunge() as it breaks lazy loading.
See tests/conftest.py for details on fixture best practices.
"""
import uuid
import pytest
from app.modules.catalog.models import Product
from app.modules.inventory.models import Inventory
from app.modules.tenancy.models import Merchant, Store
@pytest.fixture
def test_merchant(db, test_user):
"""Create a test merchant owned by test_user."""
unique_id = str(uuid.uuid4())[:8]
merchant = Merchant(
name=f"Test Merchant {unique_id}",
owner_user_id=test_user.id,
contact_email=f"test{unique_id}@merchant.com",
is_active=True,
is_verified=True,
)
db.add(merchant)
db.commit()
db.refresh(merchant)
return merchant
@pytest.fixture
def other_merchant(db, other_user):
"""Create a test merchant owned by other_user."""
unique_id = str(uuid.uuid4())[:8]
merchant = Merchant(
name=f"Other Merchant {unique_id}",
owner_user_id=other_user.id,
contact_email=f"other{unique_id}@merchant.com",
is_active=True,
is_verified=True,
)
db.add(merchant)
db.commit()
db.refresh(merchant)
return merchant
@pytest.fixture
def test_store(db, test_merchant):
"""Create a test store with unique store code."""
unique_id = str(uuid.uuid4())[:8].upper()
store = Store(
merchant_id=test_merchant.id,
store_code=f"TESTSTORE_{unique_id}",
subdomain=f"teststore{unique_id.lower()}",
name=f"Test Store {unique_id.lower()}",
is_active=True,
is_verified=True,
)
db.add(store)
db.commit()
db.refresh(store)
return store
@pytest.fixture
def test_store_with_store_user(db, test_store_user):
"""Create a store owned by a store user (for testing store API endpoints)."""
from app.modules.tenancy.models import StoreUser
unique_id = str(uuid.uuid4())[:8].upper()
# Create merchant first
merchant = Merchant(
name=f"Store API Merchant {unique_id}",
owner_user_id=test_store_user.id,
contact_email=f"storeapi{unique_id}@merchant.com",
is_active=True,
is_verified=True,
)
db.add(merchant)
db.commit()
db.refresh(merchant)
store = Store(
merchant_id=merchant.id,
store_code=f"STOREAPI_{unique_id}",
subdomain=f"storeapi{unique_id.lower()}",
name=f"Store API Test {unique_id}",
is_active=True,
is_verified=True,
)
db.add(store)
db.commit()
db.refresh(store)
# Create StoreUser association (ownership determined via Merchant.owner_user_id)
store_user = StoreUser(
store_id=store.id,
user_id=test_store_user.id,
is_active=True,
)
db.add(store_user)
db.commit()
db.refresh(store)
return store
@pytest.fixture
def unique_store(db, test_merchant):
"""Create a unique store for tests that need isolated store data."""
unique_id = str(uuid.uuid4())[:8]
store = Store(
merchant_id=test_merchant.id,
store_code=f"UNIQUESTORE_{unique_id.upper()}",
subdomain=f"uniquestore{unique_id.lower()}",
name=f"Unique Test Store {unique_id}",
description=f"A unique test store {unique_id}",
is_active=True,
is_verified=True,
)
db.add(store)
db.commit()
db.refresh(store)
return store
@pytest.fixture
def inactive_store(db, other_merchant):
"""Create an inactive store owned by other_user."""
unique_id = str(uuid.uuid4())[:8]
store = Store(
merchant_id=other_merchant.id,
store_code=f"INACTIVE_{unique_id.upper()}",
subdomain=f"inactive{unique_id.lower()}",
name=f"Inactive Store {unique_id}",
is_active=False,
is_verified=False,
)
db.add(store)
db.commit()
db.refresh(store)
return store
@pytest.fixture
def verified_store(db, other_merchant):
"""Create a verified store owned by other_user."""
unique_id = str(uuid.uuid4())[:8]
store = Store(
merchant_id=other_merchant.id,
store_code=f"VERIFIED_{unique_id.upper()}",
subdomain=f"verified{unique_id.lower()}",
name=f"Verified Store {unique_id}",
is_active=True,
is_verified=True,
)
db.add(store)
db.commit()
db.refresh(store)
return store
@pytest.fixture
def test_product(db, test_store, unique_product):
"""Create a store product relationship."""
product = Product(
store_id=test_store.id,
marketplace_product_id=unique_product.id,
is_active=True,
price=24.99,
is_featured=False,
min_quantity=1,
)
db.add(product)
db.commit()
db.refresh(product)
return product
@pytest.fixture
def test_inventory(db, test_product):
"""Create test inventory entry linked to product."""
unique_id = str(uuid.uuid4())[:8].upper()
inventory = Inventory(
product_id=test_product.id,
store_id=test_product.store_id,
warehouse="strassen",
bin_location=f"SA-10-{unique_id[:2]}",
quantity=100,
reserved_quantity=10,
gtin=test_product.marketplace_product.gtin,
)
db.add(inventory)
db.commit()
db.refresh(inventory)
return inventory
@pytest.fixture
def multiple_inventory_entries(db, multiple_products, test_store):
"""Create multiple inventory entries for testing."""
inventory_entries = []
for i, product in enumerate(multiple_products):
inventory = Inventory(
product_id=product.id,
gtin=product.gtin,
warehouse="strassen",
bin_location=f"SA-{i:02d}-01",
quantity=10 + (i * 5),
reserved_quantity=i,
store_id=test_store.id,
)
inventory_entries.append(inventory)
db.add_all(inventory_entries)
db.commit()
for inventory in inventory_entries:
db.refresh(inventory)
return inventory_entries
def create_unique_store_factory():
"""Factory function to create unique stores in tests."""
def _create_store(db, merchant_id, **kwargs):
unique_id = str(uuid.uuid4())[:8]
defaults = {
"merchant_id": merchant_id,
"store_code": f"FACTORY_{unique_id.upper()}",
"subdomain": f"factory{unique_id.lower()}",
"name": f"Factory Store {unique_id}",
"is_active": True,
"is_verified": False,
}
defaults.update(kwargs)
store = Store(**defaults)
db.add(store)
db.commit()
db.refresh(store)
return store
return _create_store
@pytest.fixture
def store_factory():
"""Fixture that provides a store factory function."""
return create_unique_store_factory()