File size: 3,081 Bytes
680fa2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import pytest

def test_signup_customer(client):
    response = client.post(
        "/api/auth/signup",
        json={
            "name": "Jane Doe",
            "phone": "9876543211",
            "role": "customer",
            "city": "Jaipur"
        }
    )
    assert response.status_code == 201
    data = response.json()
    assert data["name"] == "Jane Doe"
    assert data["phone"] == "9876543211"
    assert data["role"] == "customer"
    assert "customer_profile" in data
    assert data["customer_profile"]["wallet_balance"] == 8200

def test_signup_worker_fails_without_skill_or_rate(client):
    response = client.post(
        "/api/auth/signup",
        json={
            "name": "Ramu",
            "phone": "9876543212",
            "role": "worker",
            "city": "Jaipur"
        }
    )
    assert response.status_code == 400
    assert "detail" in response.json()

def test_signup_worker_success(client):
    response = client.post(
        "/api/auth/signup",
        json={
            "name": "Ramu Mason",
            "phone": "9876543212",
            "role": "worker",
            "city": "Jaipur",
            "skill": "Mason",
            "rate": 500
        }
    )
    assert response.status_code == 201
    data = response.json()
    assert data["role"] == "worker"
    assert "worker_profile" in data
    assert data["worker_profile"]["skill"] == "Mason"
    assert data["worker_profile"]["rate"] == 500

def test_login_demo_bypass_otp(client):
    # Log in as a customer with phone and demo OTP
    response = client.post(
        "/api/auth/login",
        json={
            "phone": "9876543213",
            "role": "customer",
            "otp": "123456"
        }
    )
    assert response.status_code == 200
    data = response.json()
    assert "access_token" in data
    assert data["role"] == "customer"

def test_login_demo_bypass_otp_worker(client):
    response = client.post(
        "/api/auth/login",
        json={
            "phone": "9876543214",
            "role": "worker",
            "otp": "123456"
        }
    )
    assert response.status_code == 200
    data = response.json()
    assert "access_token" in data
    assert data["role"] == "worker"

def test_login_invalid_otp_fails(client):
    response = client.post(
        "/api/auth/login",
        json={
            "phone": "9876543213",
            "role": "customer",
            "otp": "999999"
        }
    )
    assert response.status_code == 401

def test_fetch_me_profile_success(client):
    # 1. Login to get token
    login_response = client.post(
        "/api/auth/login",
        json={
            "phone": "9876543215",
            "role": "customer",
            "otp": "123456"
        }
    )
    token = login_response.json()["access_token"]
    
    # 2. Access me endpoint
    response = client.get(
        "/api/auth/me",
        headers={"Authorization": f"Bearer {token}"}
    )
    assert response.status_code == 200
    data = response.json()
    assert data["phone"] == "9876543215"
    assert data["role"] == "customer"