File size: 2,568 Bytes
9d4bd7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
import httpx
pytest_plugins = ["pytest_asyncio"]

@pytest.mark.asyncio
async def test_register_user(client, initialize_tests):
    async with httpx.AsyncClient() as async_client:
        response = await async_client.post("http://localhost:8001/users/register", json={
            "username": "testwuser",
            "email": "[email protected]",
            "password": "testpassword123"
        })
        
        assert response.status_code == 200
        data = response.json()
        assert data["success"] == True
        assert data["data"]["username"] == "testuser"
        assert data["data"]["email"] == "[email protected]"

@pytest.mark.asyncio
async def test_register_duplicate_email(client, initialize_tests):
    # First registration
    async with httpx.AsyncClient() as async_client:
        await async_client.post("http://localhost:8001/users/register", json={
            "username": "existing",
            "email": "[email protected]",
            "password": "password123"
        })
        
        # Attempt duplicate registration
        response = await async_client.post("http://localhost:8001/users/register", json={
            "username": "testwuser",
            "email": "[email protected]",
            "password": "testpassword123"
        })
        
        assert response.status_code == 400
        data = response.json()
        print(data)
        assert data["success"] == False
        assert "Email already registered" in data["message"]


@pytest.mark.asyncio
async def test_get_portfolio(client, initialize_tests):
    async with httpx.AsyncClient() as async_client:
        # First create a user
        register_response = await async_client.post("http://localhost:8001/users/register", json={
            "username": "portfoliouser",
            "email": "[email protected]",
            "password": "password123"
        })
        user_id = register_response.json()["data"]["id"]
        
        # Create a portfolio for the user
        portfolio_response = await async_client.post(f"http://localhost:8001/users/{user_id}/portfolio", json={
            "name": "Test Portfolio"
        })
        assert portfolio_response.status_code == 200
        assert portfolio_response.json()["success"] == True
        
        # Get the portfolio
        get_response = await async_client.get(f"http://localhost:8001/users/{user_id}/portfolio")
        assert get_response.status_code == 200
        data = get_response.json()
        assert data["success"] == True
        assert "data" in data