File size: 2,068 Bytes
151ec26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
from httpx import AsyncClient
from backend.app.models.user import User


@pytest.mark.asyncio
async def test_user_registration(client: AsyncClient):
    payload = {
        "email": "new.patient@sanjeevani.ai",
        "password": "SecurePassword123!",
        "full_name": "Rohan Sharma",
        "role": "PATIENT",
    }
    response = await client.post("/api/v1/auth/register", json=payload)
    assert response.status_code == 200
    res_data = response.json()
    assert res_data["success"] is True
    assert "access_token" in res_data["data"]
    assert "refresh_token" in res_data["data"]
    assert res_data["data"]["user"]["email"] == "new.patient@sanjeevani.ai"


@pytest.mark.asyncio
async def test_user_login(client: AsyncClient):
    # Register first
    payload = {
        "email": "login.user@sanjeevani.ai",
        "password": "LoginPassword123!",
        "full_name": "Anita Roy",
        "role": "PATIENT",
    }
    await client.post("/api/v1/auth/register", json=payload)

    # Login
    login_payload = {
        "email": "login.user@sanjeevani.ai",
        "password": "LoginPassword123!",
    }
    response = await client.post("/api/v1/auth/login", json=login_payload)
    assert response.status_code == 200
    data = response.json()
    assert data["success"] is True
    assert "access_token" in data["data"]


@pytest.mark.asyncio
async def test_user_login_invalid_password(client: AsyncClient):
    login_payload = {
        "email": "nonexistent@sanjeevani.ai",
        "password": "WrongPassword",
    }
    response = await client.post("/api/v1/auth/login", json=login_payload)
    assert response.status_code == 401
    assert response.json()["error"]["code"] == "AUTHENTICATION_FAILED"


@pytest.mark.asyncio
async def test_get_me(client: AsyncClient, test_user: User, auth_headers: dict):
    response = await client.get("/api/v1/auth/me", headers=auth_headers)
    assert response.status_code == 200
    data = response.json()
    assert data["success"] is True
    assert data["data"]["email"] == test_user.email