File size: 3,198 Bytes
0bda635
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

import requests
import time
from algosdk import account, mnemonic, util
import sys
import os
import random
import string

# URL
BASE_URL = "http://localhost:8000"

def generate_username():
    return "user_" + ''.join(random.choices(string.ascii_lowercase + string.digits, k=6))

def sign_message(private_key, message):
    # Determine how backend expects signature.
    # algorand_service.verify_signature uses util.verify_bytes
    # which expects the signature of the byte encoded message.
    # We need to sign the bytes of the message.
    
    # In Pera/standard dApps, usually we sign "MX..." if it's a specific format
    # But here backend does: util.verify_bytes(message.encode('utf-8'), signature, wallet_address)
    # So we just sign the raw bytes.
    
    signature = util.sign_bytes(message.encode('utf-8'), private_key)
    return signature

def test_signup_login():
    print("--- Testing Auth Flow ---")
    
    # 1. Generate Wallet
    private_key, address = account.generate_account()
    print(f"Generated Wallet: {address}")
    
    username = generate_username()
    print(f"Username: {username}")
    
    message = "Login to Rift" # In real app, this should be a nonce
    signature = sign_message(private_key, message)
    
    # 2. Test Login (Unregistered) -> Should Fail
    print("\n[Test 1] Login Unregistered User...")
    login_payload = {
        "wallet_address": address,
        "signature": signature,
        "message": message
    }
    
    res = requests.post(f"{BASE_URL}/auth/login", json=login_payload)
    if res.status_code == 404:
        print("SUCCESS: Login failed as expected (404 User not found).")
    else:
        print(f"FAILURE: Login unexpected response: {res.status_code} {res.text}")
        return

    # 3. Test Signup -> Should Success
    print("\n[Test 2] Signup New User...")
    signup_payload = {
        "wallet_address": address,
        "signature": signature,
        "message": message,
        "username": username
    }
    
    res = requests.post(f"{BASE_URL}/auth/signup", json=signup_payload)
    if res.status_code == 200:
        print("SUCCESS: Signup successful.")
        token = res.json().get("access_token")
        print(f"Token received: {token[:10]}...")
    else:
        print(f"FAILURE: Signup failed: {res.status_code} {res.text}")
        return

    # 4. Test Login (Registered) -> Should Success
    print("\n[Test 3] Login Registered User...")
    res = requests.post(f"{BASE_URL}/auth/login", json=login_payload)
    if res.status_code == 200:
        print("SUCCESS: Login successful.")
    else:
        print(f"FAILURE: Login failed: {res.status_code} {res.text}")
        
    # 5. Test Signup (Duplicate) -> Should Fail
    print("\n[Test 4] Signup Duplicate User...")
    res = requests.post(f"{BASE_URL}/auth/signup", json=signup_payload)
    if res.status_code == 400:
        print("SUCCESS: Duplicate signup failed as expected (400).")
    else:
        print(f"FAILURE: Duplicate signup unexpected response: {res.status_code} {res.text}")

if __name__ == "__main__":
    test_signup_login()