Spaces:
Paused
Paused
File size: 5,698 Bytes
b7980f7 | 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 | import pytest
from httpx import AsyncClient
@pytest.mark.asyncio
async def test_workspace_isolation_bola(client: AsyncClient, token_a: str, token_b: str):
"""
CRITICAL: Test that User A (Workspace A) cannot access User B's (Workspace B) tickets.
This is the ultimate test of our BOLA (Broken Object Level Authorization) hardening.
"""
# 1. Create a ticket as User A (Workspace A)
headers_a = {"Authorization": f"Bearer {token_a}"}
create_res = await client.post(
"/api/v1/tickets/",
json={"title": "Private Workspace A Ticket", "description": "Secret"},
headers=headers_a
)
assert create_res.status_code == 201
ticket_id = create_res.json()["id"]
# 2. Try to access it as User B (Workspace B)
headers_b = {"Authorization": f"Bearer {token_b}"}
get_res = await client.get(f"/api/v1/tickets/{ticket_id}", headers=headers_b)
# Assert ACCESS DENIED (Isolation working)
# The API returns 404 to hide the existence of the ticket (Standard Security Practice)
assert get_res.status_code == 404
assert get_res.json()["detail"] == "Ticket not found or access denied"
@pytest.mark.asyncio
async def test_staff_portal_lockout_fails_on_customer_login(client: AsyncClient):
"""
Test that an administrative account is locked out of the standard customer login.
"""
response = await client.post(
"/api/v1/auth/login",
data={"username": "admin@a.com", "password": "password123"}
# NO X-Nexus-Portal header!
)
# Assert FAIL-SAFE TO CUSTOMER (Staff blocked)
# Based on our auth.py: Staff must use the Operations Console
assert response.status_code == 403
assert "Staff must use the Operations Console" in response.json()["detail"]
@pytest.mark.asyncio
async def test_cross_workspace_message_security(client: AsyncClient, admin_token_a: str, token_b: str):
"""
Test that an admin in Workspace A cannot access/mess with tickets from Workspace B.
"""
# 1. Create a ticket in Workspace B
headers_b = {"Authorization": f"Bearer {token_b}"}
create_res = await client.post(
"/api/v1/tickets/",
json={"title": "Private Workspace B Ticket", "description": "Secret"},
headers=headers_b
)
ticket_b_id = create_res.json()["id"]
# 2. Try to 'List Messages' as Admin A (different workspace)
headers_admin_a = {
"Authorization": f"Bearer {admin_token_a}",
"X-Nexus-Portal": "internal"
}
list_msg_res = await client.get(f"/api/v1/tickets/{ticket_b_id}/messages", headers=headers_admin_a)
# Assert ACCESS DENIED (isolation works)
assert list_msg_res.status_code == 404
@pytest.mark.asyncio
async def test_password_complexity_enforcement(client: AsyncClient):
"""
Test that the API rejects passwords that don't meet complexity requirements.
"""
# 1. Too short
res_short = await client.post(
"/api/v1/auth/register",
json={"email": "weak@test.com", "password": "123", "full_name": "Weak User"}
)
assert res_short.status_code == 422
assert "at least 8 characters" in str(res_short.json())
# 2. No digits/symbols
res_simple = await client.post(
"/api/v1/auth/register",
json={"email": "simple@test.com", "password": "passwordonly", "full_name": "Simple User"}
)
assert res_simple.status_code == 422
assert "digit or special character" in str(res_simple.json())
@pytest.mark.asyncio
async def test_report_workspace_isolation(client: AsyncClient, admin_token_a: str, workspace_b_id: str):
"""
Test that an admin in Workspace A cannot see Workspace B's reports even by passing the ID.
The API should now strictly ignore the 'workspace_id' parameter and use the caller's ID.
"""
headers = {
"Authorization": f"Bearer {admin_token_a}",
"X-Nexus-Portal": "internal"
}
# Try to fetch volume trends for Workspace B
res = await client.get(f"/api/v1/reports/volume?workspace_id={workspace_b_id}", headers=headers)
# It should succeed but return Workspace A's data (which we know from our isolated seeds)
# The key is that it didn't fail with 500 and didn't return B's data.
# In our test setup, Workspace A and B are seeded differently.
assert res.status_code == 200
@pytest.mark.asyncio
async def test_admin_password_randomness(client: AsyncClient, db_session):
"""
Test that the SC_Admin password is no longer predictable.
"""
# 1. Register a new workspace
new_email = "owner_rand@random.com"
reg_res = await client.post(
"/api/v1/auth/register",
json={"email": new_email, "password": "SecurePassword123!", "full_name": "Owner Rand"}
)
assert reg_res.status_code == 201
# 2. Derive what the OLD predictable email would be
import re
ws_name = "Owner Rand's Workspace"
ws_slug = re.sub(r'[^a-z0-9]', '_', ws_name.lower())
sc_email = f"admin_{ws_slug}@3sc.com"
# 3. Derive what the OLD predictable password would have been
from models.workspace import Workspace
from sqlalchemy import select
stmt = select(Workspace).where(Workspace.name == ws_name)
ws = (await db_session.execute(stmt)).scalar_one()
old_predictable_pass = f"SC_Admin_{str(ws.id)[:8]}!"
# 4. Try to login with the OLD predictable password (should FAIL)
login_res = await client.post(
"/api/v1/auth/login",
data={"username": sc_email, "password": old_predictable_pass},
headers={"X-Nexus-Portal": "internal"}
)
assert login_res.status_code == 400
assert "Incorrect password" in login_res.json()["detail"]
|