Spaces:
Paused

File size: 4,920 Bytes
aa13248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Integration tests for the workspace management API.

Coverage:
  - GET /workspace/ returns the caller's current workspace
  - GET /workspace/list/ returns only workspaces the caller belongs to
  - PATCH /workspace/ (admin-only) updates workspace name
  - Customer cannot update workspace (403)
  - GET /workspace/branding/ returns branding config (dict)
"""

import pytest
from httpx import AsyncClient


_INTERNAL = {"X-Nexus-Portal": "internal"}


# ---------------------------------------------------------------------------
# Get current workspace
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_get_my_workspace_customer(client: AsyncClient, token_a: str):
    """GET /workspace/ returns the workspace the customer belongs to."""
    res = await client.get(
        "/api/v1/workspace/",
        headers={"Authorization": f"Bearer {token_a}"},
    )
    assert res.status_code == 200
    data = res.json()
    assert "id" in data
    assert "name" in data
    assert data["name"] == "Test Workspace A"


@pytest.mark.asyncio
async def test_get_my_workspace_admin(client: AsyncClient, admin_token_a: str):
    """SC_ADMIN can also retrieve their current workspace."""
    res = await client.get(
        "/api/v1/workspace/",
        headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
    )
    assert res.status_code == 200
    assert res.json()["name"] == "Test Workspace A"


# ---------------------------------------------------------------------------
# List available workspaces
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_list_workspaces_customer_only_sees_own(
    client: AsyncClient, token_a: str
):
    """A customer only sees the workspaces they are a member of."""
    res = await client.get(
        "/api/v1/workspace/list/",
        headers={"Authorization": f"Bearer {token_a}"},
    )
    assert res.status_code == 200
    names = [w["name"] for w in res.json()]
    assert "Test Workspace A" in names
    assert "Test Workspace B" not in names


@pytest.mark.asyncio
async def test_list_workspaces_admin_sees_all(client: AsyncClient, admin_token_a: str):
    """SC_ADMIN (platform admin) sees all workspaces in the system."""
    res = await client.get(
        "/api/v1/workspace/list/",
        headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
    )
    assert res.status_code == 200
    names = [w["name"] for w in res.json()]
    # Both seeded workspaces must be visible to the platform admin
    assert "Test Workspace A" in names
    assert "Test Workspace B" in names


# ---------------------------------------------------------------------------
# Update workspace
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_admin_can_update_workspace_name(client: AsyncClient, admin_token_a: str):
    """SC_ADMIN can rename their workspace."""
    res = await client.patch(
        "/api/v1/workspace/",
        json={"name": "Renamed Workspace A"},
        headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
    )
    assert res.status_code == 200
    assert res.json()["name"] == "Renamed Workspace A"


@pytest.mark.asyncio
async def test_customer_cannot_update_workspace(client: AsyncClient, token_a: str):
    """A CUSTOMER_USER must receive 403 when trying to rename the workspace."""
    res = await client.patch(
        "/api/v1/workspace/",
        json={"name": "Unauthorized Rename"},
        headers={"Authorization": f"Bearer {token_a}"},
    )
    assert res.status_code == 403


# ---------------------------------------------------------------------------
# Branding
# ---------------------------------------------------------------------------

@pytest.mark.asyncio
async def test_get_branding_returns_dict(client: AsyncClient, token_a: str):
    """GET /workspace/branding/ returns a JSON object (empty dict if not set)."""
    res = await client.get(
        "/api/v1/workspace/branding/",
        headers={"Authorization": f"Bearer {token_a}"},
    )
    assert res.status_code == 200
    assert isinstance(res.json(), dict)


@pytest.mark.asyncio
async def test_admin_can_set_branding(client: AsyncClient, admin_token_a: str):
    """SC_ADMIN can write branding config; it is then returned by the branding endpoint."""
    await client.patch(
        "/api/v1/workspace/",
        json={"branding": {"primary_color": "#FF5733", "logo_url": "https://example.com/logo.png"}},
        headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
    )
    res = await client.get(
        "/api/v1/workspace/branding/",
        headers={"Authorization": f"Bearer {admin_token_a}", **_INTERNAL},
    )
    assert res.status_code == 200
    branding = res.json()
    assert branding.get("primary_color") == "#FF5733"