import asyncio import httpx import random import string from urllib.parse import urlparse BASE_URL = "http://localhost:8000/api/v1" def generate_random_email(): return f"test_{''.join(random.choices(string.ascii_lowercase, k=8))}@example.com" async def test_auth_flow(): email = generate_random_email() password = "Start@123_Secure" company_name = "Test Company" print(f"šŸ“§ Testing with email: {email}") async with httpx.AsyncClient(timeout=30.0) as client: # 1. Register print("\n1ļøāƒ£ Testing Registration...") response = await client.post(f"{BASE_URL}/auth/register", json={ "email": email, "password": password, "company_name": company_name }) if response.status_code != 200: print(f"āŒ Registration failed: {response.text}") return data = response.json() if data.get("access_token") == "verification_pending": print("āœ… Registration successful (Verification Pending)") else: print(f"āš ļø Unexpected response: {data}") # At this point, we need the verification code. # Since we can't easily read the email in this test script without mocking, # we might need to peek into the DB or just rely on the user to check email. # But for automated testing, we could query the DB if we had direct access. # For now, I will verify that the login fails without verification print("\n2ļøāƒ£ Testing Login (Unverified)...") response = await client.post(f"{BASE_URL}/auth/login", json={ "email": email, "password": password }) if response.status_code == 403 and "verified" in response.text.lower(): print("āœ… Login failed as expected (Unverified)") else: print(f"āŒ Login verification check failed: {response.status_code} {response.text}") # Test Forgot Password Request print("\n3ļøāƒ£ Testing Forgot Password Request...") response = await client.post(f"{BASE_URL}/auth/forgot-password", json={ "email": email }) if response.status_code == 200: print("āœ… Forgot Password request successful") else: print(f"āŒ Forgot Password request failed: {response.text}") print("\nāš ļø Manual verification required for obtaining the OTP and completing the flow.") print("Please check the backend logs or email inbox for the code.") if __name__ == "__main__": asyncio.run(test_auth_flow())