#!/usr/bin/env python3 """ Integration test for the organization onboard fix. Uses TestClient to call the endpoint and checks if a branch is created. """ import asyncio import sys import os sys.path.insert(0, '/app/backend') from fastapi.testclient import TestClient from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine from sqlalchemy.orm import sessionmaker from app.main import app from app.db.session import get_db from app.models.marketplace.organization import Organization, Branch from app.models.identity import User from app.core.security import create_access_token import uuid # Override get_db to use test database TEST_DATABASE_URL = os.getenv("DATABASE_URL", "postgresql+asyncpg://service_finder_app:AppSafePass_2026@postgres-db:5432/service_finder") engine = create_async_engine(TEST_DATABASE_URL) AsyncTestingSessionLocal = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) async def override_get_db(): async with AsyncTestingSessionLocal() as session: yield session app.dependency_overrides[get_db] = override_get_db async def test_onboard_creates_branch(): """Test that onboard endpoint creates a default main branch.""" client = TestClient(app) # First, we need an authenticated user. Let's get an existing user from the database. async with AsyncTestingSessionLocal() as db: # Find any active user from sqlalchemy import select stmt = select(User).where(User.is_active == True).limit(1) result = await db.execute(stmt) user = result.scalar_one_or_none() if not user: print("No active user found in database. Skipping test.") return False # Create a JWT token for this user token = create_access_token(data={"sub": user.email}) # Generate unique tax number import datetime timestamp = datetime.datetime.now().strftime("%H%M%S") tax_number = f"98765432-1-{timestamp[-2:]}" # Call onboard endpoint org_data = { "full_name": "Auto-Fixer Pro Kft.", "name": "AutoFixerPro", "display_name": "Auto-Fixer Pro", "tax_number": tax_number, "reg_number": "01-09-123456", "country_code": "HU", "address_zip": "1234", "address_city": "Budapest", "address_street_name": "Teszt utca", "address_street_type": "utca", "address_house_number": "42", "address_hrsz": "" } response = client.post( "/api/v1/organizations/onboard", json=org_data, headers={"Authorization": f"Bearer {token}"} ) print(f"Response status: {response.status_code}") if response.status_code != 201: print(f"Error: {response.text}") return False org_response = response.json() org_id = org_response.get("organization_id") print(f"Organization created with ID: {org_id}") # Now check if a branch was created async with AsyncTestingSessionLocal() as db: stmt = select(Branch).where( Branch.organization_id == org_id, Branch.is_main == True ) result = await db.execute(stmt) branch = result.scalar_one_or_none() if branch: print(f"āœ… SUCCESS: Default branch created: {branch.name} (ID: {branch.id})") # Cleanup: delete the test organization and branch # (optional, but good practice) await db.delete(branch) org_stmt = select(Organization).where(Organization.id == org_id) org_result = await db.execute(org_stmt) org = org_result.scalar_one_or_none() if org: await db.delete(org) await db.commit() return True else: print("āŒ FAIL: No default branch found.") return False async def main(): print("=== Running Organization Onboard Integration Test ===") success = await test_onboard_creates_branch() if success: print("\nāœ… Test PASSED: Branch creation fix works!") sys.exit(0) else: print("\nāŒ Test FAILED: Branch not created.") sys.exit(1) if __name__ == "__main__": asyncio.run(main())