File size: 5,252 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import pytest
from httpx import AsyncClient
from sqlalchemy.ext.asyncio import AsyncSession
from ..utils import create_user_and_login, create_branch

pytestmark = pytest.mark.asyncio

@pytest.fixture
async def admin_token(client: AsyncClient) -> str:
    """Create an admin user and return their token"""
    user_data = await create_user_and_login(
        client,
        email="admin@example.com",
        password="adminpass123",
        full_name="Admin User",
        is_superuser=True
    )
    return user_data["token"]

@pytest.fixture
async def regular_token(client: AsyncClient) -> str:
    """Create a regular user and return their token"""
    user_data = await create_user_and_login(
        client,
        email="user@example.com",
        password="userpass123",
        full_name="Regular User",
        is_superuser=False
    )
    return user_data["token"]

async def test_create_branch(client: AsyncClient, admin_token: str):
    # Test creating a branch as admin
    response = await client.post(
        "/api/v1/branches/",
        json={
            "name": "Test Branch",
            "location": "Test Location",
            "contact_email": "branch@example.com",
            "phone": "1234567890"
        },
        headers={"Authorization": f"Bearer {admin_token}"}
    )
    assert response.status_code == 200
    data = response.json()
    assert data["name"] == "Test Branch"
    assert data["location"] == "Test Location"
    return data["id"]

async def test_create_branch_unauthorized(client: AsyncClient, regular_token: str):
    # Test creating a branch as regular user (should fail)
    response = await client.post(
        "/api/v1/branches/",
        json={
            "name": "Test Branch",
            "location": "Test Location"
        },
        headers={"Authorization": f"Bearer {regular_token}"}
    )
    assert response.status_code == 403

async def test_get_branch(client: AsyncClient, admin_token: str):
    # First create a branch
    branch_id = await test_create_branch(client, admin_token)
    
    # Test getting the branch
    response = await client.get(
        f"/api/v1/branches/{branch_id}",
        headers={"Authorization": f"Bearer {admin_token}"}
    )
    assert response.status_code == 200
    data = response.json()
    assert data["id"] == branch_id
    assert data["name"] == "Test Branch"

async def test_list_branches(client: AsyncClient, admin_token: str):
    # Create multiple branches
    await test_create_branch(client, admin_token)
    await client.post(
        "/api/v1/branches/",
        json={
            "name": "Another Branch",
            "location": "Another Location",
            "contact_email": "another@example.com",
            "phone": "0987654321"
        },
        headers={"Authorization": f"Bearer {admin_token}"}
    )
    
    # Test listing branches
    response = await client.get(
        "/api/v1/branches/",
        headers={"Authorization": f"Bearer {admin_token}"}
    )
    assert response.status_code == 200
    data = response.json()
    assert len(data) >= 2
    assert any(b["name"] == "Test Branch" for b in data)
    assert any(b["name"] == "Another Branch" for b in data)

async def test_update_branch(client: AsyncClient, admin_token: str):
    # First create a branch
    branch_id = await test_create_branch(client, admin_token)
    
    # Test updating the branch
    response = await client.put(
        f"/api/v1/branches/{branch_id}",
        json={
            "name": "Updated Branch",
            "location": "Updated Location",
            "contact_email": "updated@example.com",
            "phone": "1112223333"
        },
        headers={"Authorization": f"Bearer {admin_token}"}
    )
    assert response.status_code == 200
    data = response.json()
    assert data["name"] == "Updated Branch"
    assert data["location"] == "Updated Location"
    assert data["contact_email"] == "updated@example.com"

async def test_delete_branch(client: AsyncClient, admin_token: str):
    # First create a branch
    branch_id = await test_create_branch(client, admin_token)
    
    # Test deleting the branch
    response = await client.delete(
        f"/api/v1/branches/{branch_id}",
        headers={"Authorization": f"Bearer {admin_token}"}
    )
    assert response.status_code == 200
    
    # Verify branch is deleted
    response = await client.get(
        f"/api/v1/branches/{branch_id}",
        headers={"Authorization": f"Bearer {admin_token}"}
    )
    assert response.status_code == 404

async def test_branch_statistics(client: AsyncClient, admin_token: str):
    # First create a branch
    branch_id = await test_create_branch(client, admin_token)
    
    # Test getting branch statistics
    response = await client.get(
        f"/api/v1/branches/{branch_id}/statistics",
        headers={"Authorization": f"Bearer {admin_token}"}
    )
    assert response.status_code == 200
    data = response.json()
    assert "total_products" in data
    assert "total_orders" in data
    assert "total_revenue" in data
    assert "average_order_value" in data