File size: 2,277 Bytes
4c5298a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
from httpx import AsyncClient
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.security import create_access_token

pytestmark = pytest.mark.asyncio

async def test_login(client: AsyncClient, db_session: AsyncSession):
    # First register a test user
    register_response = await client.post(
        "/api/v1/auth/register",
        data={
            "username": "testuser@example.com",
            "password": "testpass123"
        }
    )
    assert register_response.status_code == 200
    
    # Test login with correct credentials
    response = await client.post(
        "/api/v1/auth/login",
        data={
            "username": "testuser@example.com",
            "password": "testpass123"
        }
    )
    assert response.status_code == 200
    assert "access_token" in response.json()
    assert response.json()["token_type"] == "bearer"

    # Test login with incorrect password
    response = await client.post(
        "/api/v1/auth/login",
        data={
            "username": "testuser@example.com",
            "password": "wrongpass"
        }
    )
    assert response.status_code == 401

    # Test login with non-existent user
    response = await client.post(
        "/api/v1/auth/login",
        data={
            "username": "nonexistent@example.com",
            "password": "testpass123"
        }
    )
    assert response.status_code == 401

async def test_register(client: AsyncClient, db_session: AsyncSession):
    # Test successful registration
    response = await client.post(
        "/api/v1/auth/register",
        data={
            "username": "newuser@example.com",
            "password": "newpass123"
        }
    )
    assert response.status_code == 200
    user_data = response.json()
    assert user_data["email"] == "newuser@example.com"
    assert "hashed_password" not in user_data

    # Test registration with existing email
    response = await client.post(
        "/api/v1/auth/register",
        data={
            "username": "newuser@example.com",
            "password": "anotherpass"
        }
    )
    assert response.status_code == 400
    assert "Email already registered" in response.json()["detail"]