Spaces:
Running

microfunnel-api / backend /scripts /test_auth_flow.py
axeronis
feat: Implement Auth and Email Verification (Frontend & Backend)
dd35b54
Raw History Blame Contribute Delete
2.66 kB
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())