File size: 2,578 Bytes
65dc45c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi.testclient import TestClient
import pytest

def test_create_user(client: TestClient):
    response = client.post(
        "/api/users/",
        json={
            "username": "testuser",
            "email": "testuser@example.com",
            "password": "testpassword123"
        }
    )
    assert response.status_code == 200
    data = response.json()
    assert data["email"] == "testuser@example.com"
    assert data["username"] == "testuser"
    assert "id" in data

def test_create_existing_user(client: TestClient):
    # First create
    client.post(
        "/api/users/",
        json={
            "username": "testuser2",
            "email": "testuser2@example.com",
            "password": "testpassword123"
        }
    )
    # Then try to create again
    response = client.post(
        "/api/users/",
        json={
            "username": "testuser2",
            "email": "testuser2@example.com",
            "password": "testpassword123"
        }
    )
    assert response.status_code == 400
    assert response.json()["detail"] == "Email already exists"

def test_login_user(client: TestClient):
    # Create user first
    client.post(
        "/api/users/",
        json={
            "username": "loginuser",
            "email": "loginuser@example.com",
            "password": "loginpassword123"
        }
    )
    
    # Login
    response = client.post(
        "/api/users/login",
        json={
            "email": "loginuser@example.com",
            "password": "loginpassword123"
        }
    )
    assert response.status_code == 200
    data = response.json()
    assert "access_token" in data
    assert data["token_type"] == "bearer"
    assert data["user"]["email"] == "loginuser@example.com"

def test_read_users(client: TestClient):
    # Create user first
    client.post(
        "/api/users/",
        json={
            "username": "readuser",
            "email": "readuser@example.com",
            "password": "readpassword123"
        }
    )
    
    # Login to get token
    login_response = client.post(
        "/api/users/login",
        json={
            "email": "readuser@example.com",
            "password": "readpassword123"
        }
    )
    token = login_response.json()["access_token"]
    
    # Get users with token
    response = client.get(
        "/api/users/",
        headers={"Authorization": f"Bearer {token}"}
    )
    assert response.status_code == 200
    data = response.json()
    assert len(data) >= 1
    assert any(user["email"] == "readuser@example.com" for user in data)