import asyncio import logging import sys from datetime import datetime, date import uuid # Hozzáadjuk az app modul eléréséhez sys.path.append("/app/backend") from app.db.session import AsyncSessionLocal from app.models.identity import User, Person, VerificationToken from app.models.marketplace import Organization, OrganizationMember from app.services.auth_service import AuthService from app.schemas.auth import UserLiteRegister, UserKYCComplete from sqlalchemy import select, delete, text logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) async def run_tests(): async with AsyncSessionLocal() as db: logger.info("--- TESZTKÖRNYEZET ELŐKÉSZÍTÉSE ---") suffix = uuid.uuid4().hex[:6] email_owner = f"owner_{suffix}@test.com" email_existing = f"existing_{suffix}@test.com" email_newbie = f"newbie_{suffix}@test.com" email_shadow = f"shadow_{suffix}@test.com" shadow_name = f"ShadowBéla_{suffix}" # 1. Alap adatok: Tulajdonos regisztrál és KYC owner_in = UserLiteRegister( email=email_owner, password="Password123!", first_name="Tulajdonos", last_name="Teszt", region_code="HU", lang="hu" ) owner_user = await AuthService.register_lite(db, owner_in) owner_user.is_active = True person = (await db.execute(select(Person).where(Person.id == owner_user.person_id))).scalar_one() person.is_active = True await db.commit() kyc_owner = UserKYCComplete( phone_number="+36301234567", birth_place="Budapest", birth_date=date(1980, 1, 1), mothers_last_name="Anyja", mothers_first_name="Neve", address_zip="1111", address_city="Budapest", address_street_name="Teszt", address_street_type="utca", address_house_number="1", identity_docs={}, ice_contact={"name": "Teszt", "phone": "123", "relationship": "barat"} ) owner_user = await AuthService.complete_kyc(db, owner_user.id, kyc_owner) org_id = int(owner_user.scope_id) logger.info(f"Tulajdonos regisztrálva. Org ID: {org_id}") existing_in = UserLiteRegister( email=email_existing, password="Password123!", first_name="Létező", last_name="Teszt", region_code="HU", lang="hu" ) existing_user = await AuthService.register_lite(db, existing_in) await db.commit() from app.api.v1.endpoints.organizations import invite_to_organization, accept_invitation, OrgInvitationIn logger.info("--- TESZT 1: LÉTEZŐ FELHASZNÁLÓ MEGHÍVÁSA ---") try: res1 = await invite_to_organization( org_id=org_id, invite_in=OrgInvitationIn(email=email_existing, role="DRIVER"), db=db, current_user=owner_user ) logger.info(f"Teszt 1 Eredmény: {res1}") stmt_mem = select(OrganizationMember).where(OrganizationMember.user_id == existing_user.id) mem = (await db.execute(stmt_mem)).scalar_one() assert mem.status == "pending" assert mem.role == "DRIVER" logger.info("Teszt 1 SIKERES") except Exception as e: logger.error(f"Teszt 1 HIBA: {e}") logger.info("--- TESZT 2: ÚJ FELHASZNÁLÓ MEGHÍVÁSA ---") try: res2 = await invite_to_organization( org_id=org_id, invite_in=OrgInvitationIn(email=email_newbie, role="MECHANIC"), db=db, current_user=owner_user ) logger.info(f"Teszt 2 Eredmény: {res2}") stmt_token = select(VerificationToken).where(VerificationToken.token_type == "org_invite") tokens = (await db.execute(stmt_token)).scalars().all() token_rec = next(t for t in tokens if t.extra_data and t.extra_data.get("email") == email_newbie) assert token_rec.extra_data["role"] == "MECHANIC" assert token_rec.extra_data["org_id"] == org_id newbie_in = UserLiteRegister( email=email_newbie, password="Password123!", first_name="Újonc", last_name="Teszt", region_code="HU", lang="hu" ) newbie_user = await AuthService.register_lite(db, newbie_in) await db.commit() res3 = await accept_invitation(token=token_rec.token, db=db, current_user=newbie_user) logger.info(f"Teszt 2 Elfogadás: {res3}") stmt_mem2 = select(OrganizationMember).where(OrganizationMember.user_id == newbie_user.id) mem2 = (await db.execute(stmt_mem2)).scalar_one() assert mem2.status == "active" logger.info("Teszt 2 SIKERES") except Exception as e: logger.error(f"Teszt 2 HIBA: {e}") logger.info("--- TESZT 3: SHADOW IDENTITY CHECK ---") try: shadow_person = Person( first_name="Elek", last_name=shadow_name, birth_date=date(1990, 5, 5), is_active=False, is_ghost=True, identity_docs={"ocr": "done"}, ice_contact={"name": "Teszt", "phone": "123", "relationship": "barat"} ) db.add(shadow_person) await db.commit() shadow_person_id = shadow_person.id logger.info(f"Létrehozott Shadow Person ID: {shadow_person_id}") shadow_user_in = UserLiteRegister( email=email_shadow, password="Password123!", first_name="Elek", last_name=shadow_name, region_code="HU", lang="hu" ) shadow_user = await AuthService.register_lite(db, shadow_user_in) shadow_user.is_active = True await db.commit() eredeti_person_id = shadow_user.person_id kyc_shadow = UserKYCComplete( phone_number="+36309999999", birth_place="Debrecen", birth_date=date(1990, 5, 5), mothers_last_name="Teszt", mothers_first_name="Anya", address_zip="4000", address_city="Debrecen", address_street_name="Fő", address_street_type="utca", address_house_number="2", identity_docs={"manual": "done"}, ice_contact={"name": "Teszt", "phone": "123", "relationship": "barat"} ) updated_user = await AuthService.complete_kyc(db, shadow_user.id, kyc_shadow) logger.info(f"Eredeti Person ID: {eredeti_person_id}, Új Person ID: {updated_user.person_id}") if updated_user.person_id == shadow_person_id: logger.info("Teszt 3 SIKERES") else: logger.error("Teszt 3 SIKERTELEN (Nem az árnyék ID-t kapta)") except Exception as e: logger.error(f"Teszt 3 HIBA: {e}") if __name__ == "__main__": asyncio.run(run_tests())