ifixkart-backend/test_delivery_cod_rules.py
adithiyan 10bfba7d58 feat: add warranties, refunds, notifications, CRM calendar, system config & delivery modules
- New models: WarrantyModel, WalletModel, SystemConfigModel
- New schemas: WarrantySchema, RefundSchema, SystemConfig, CrmTaskCalendar, NotificationTemplate, NotificationGateway, RepairErp, ReviewModeration, InventoryPos, MultiDashboard, BillingFinance
- New services: WarrantyService, RefundService, CrmService, CrmTaskCalendarService, DeliveryService, EmailTemplateService, InventoryPosService, MultiDashboardService, NotificationGatewayService, RepairErpService, ReviewModerationService, SystemConfigService, TemplateService, CreditService
- Updated routers: AdminOrder, BillingFinance, Cart, Catalog, Checkout, Crm, InventoryPos, NotificationGateway, RolePermission, ServiceJob, Settings, Storefront, UserCreation
- Updated models: Inventory, Notification, Order, Product, Service, Setting, SystemConfig, User
- Updated core: database init, invoice generator, main app
- Added WarrantyRouter with full CRUD and claim flow
2026-10-13 22:05:16 +05:30

149 lines
5.8 KiB
Python

"""
Automated test suite verifying complete COD removal and dynamic delivery rules.
"""
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__)))
import app.models.db_base
from fastapi import HTTPException
from app.core.database.db_session import SessionLocal
from app.services.DeliveryService import DeliveryService
from app.models.EcomCustomerModel import EcomCustomer, CustomerAddress
from app.models.CartModel import Cart
from app.models.ProductModel import ProductVariant
from app.services.OrderService import OrderService
import json
import ulid
def test_delivery_and_cod():
db = SessionLocal()
try:
print("\n--- 1. Testing Default Delivery Settings ---")
settings = DeliveryService.get_delivery_settings(db)
print("Initial settings:", settings)
assert settings["free_delivery_enabled"] is False, "Free delivery must be disabled by default!"
assert settings["delivery_charge"] == 99.0, "Default delivery charge should be 99.0!"
# Ensure a test customer and cart exist
test_email = "test_storefront_wallet@ifixkart.com"
customer = db.query(EcomCustomer).filter(EcomCustomer.email == test_email).first()
if not customer:
customer = EcomCustomer(
customer_id=str(ulid.ULID()),
first_name="Delivery",
last_name="Tester",
email=test_email,
phone="9876543299",
is_active=True
)
db.add(customer)
db.commit()
# Ensure address
address = db.query(CustomerAddress).filter(CustomerAddress.customer_id == customer.customer_id).first()
if not address:
address = CustomerAddress(
address_id=str(ulid.ULID()),
customer_id=customer.customer_id,
full_name="Delivery Tester",
phone="9876543299",
street_address="123 Tech Park",
city="Chennai",
state="Tamil Nadu",
pincode="600001",
address_type="SHIPPING"
)
db.add(address)
db.commit()
# Find a product variant
variant = db.query(ProductVariant).filter(ProductVariant.price > 100).first()
assert variant is not None, "At least one product variant must exist in DB"
# Populate cart with 2 units so subtotal is substantial
cart = db.query(Cart).filter(Cart.customer_id == customer.customer_id).first()
if not cart:
cart = Cart(
cart_id=str(ulid.ULID()),
customer_id=customer.customer_id
)
db.add(cart)
cart.items_json = json.dumps([{"variant_id": variant.variant_id, "qty": 2}])
db.commit()
print("\n--- 2. Verifying COD Rejection ---")
cod_rejected = False
try:
OrderService.create_ecommerce_order(
db=db,
customer=customer,
address_id=address.address_id,
payment_method="COD",
)
except HTTPException as e:
if e.status_code == 400 and "COD" in e.detail:
cod_rejected = True
print("PASSED: COD was rejected with:", e.detail)
assert cod_rejected, "COD must be rejected with HTTP 400!"
print("\n--- 3. Verifying Order with Disabled Free Delivery (Flat Fee Applied) ---")
# Ensure free delivery is false
DeliveryService.update_delivery_settings(db, delivery_charge=99.0, free_delivery_enabled=False)
order_res = OrderService.create_ecommerce_order(
db=db,
customer=customer,
address_id=address.address_id,
payment_method="RAZORPAY",
)
print("Created order:", order_res["order_no"], "Shipping Cost:", order_res["shipping_cost"])
assert order_res["shipping_cost"] == 99.0, "Shipping cost must be 99.0 when free delivery is disabled!"
print("\n--- 4. Testing Dynamic Free Delivery Enablement via Admin Settings ---")
DeliveryService.update_delivery_settings(
db=db,
delivery_charge=99.0,
free_delivery_enabled=True,
free_delivery_min_order_amount=200.0
)
updated_settings = DeliveryService.get_delivery_settings(db)
print("Updated settings:", updated_settings)
assert updated_settings["free_delivery_enabled"] is True
assert updated_settings["free_delivery_min_order_amount"] == 200.0
# Repopulate cart
cart.items_json = json.dumps([{"variant_id": variant.variant_id, "qty": 2}])
db.commit()
order_res2 = OrderService.create_ecommerce_order(
db=db,
customer=customer,
address_id=address.address_id,
payment_method="RAZORPAY",
)
print("Created order 2 with free shipping enabled:", order_res2["order_no"], "Shipping Cost:", order_res2["shipping_cost"])
assert order_res2["shipping_cost"] == 0.0, "Shipping cost should be 0.0 when free delivery is enabled and threshold met!"
print("\n--- 5. Resetting Free Delivery to False (Default State) ---")
DeliveryService.update_delivery_settings(
db=db,
delivery_charge=99.0,
free_delivery_enabled=False,
free_delivery_min_order_amount=999.0
)
final_settings = DeliveryService.get_delivery_settings(db)
assert final_settings["free_delivery_enabled"] is False
print("Final settings restored:", final_settings)
print("\n=======================================================")
print(">>> ALL DELIVERY AND COD INTEGRATION TESTS PASSED 100% <<<")
print("=======================================================\n")
finally:
db.close()
if __name__ == "__main__":
test_delivery_and_cod()