Spaces:
Paused

File size: 1,919 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
import pytest
from httpx import AsyncClient
from uuid import UUID

@pytest.mark.asyncio
async def test_create_escalation_rule_invalid_workspace(client: AsyncClient, admin_token_a: str):
    """
    Test that creating an escalation rule with a non-existent workspace_id
    returns a 404 instead of a 500 IntegrityError.
    """
    headers = {
        "Authorization": f"Bearer {admin_token_a}",
        "X-Nexus-Portal": "internal"
    }
    
    # Common placeholder UUID that doesn't exist in seeded test DB
    invalid_ws_id = "3fa85f64-5717-4562-b3fc-2c963f66afa6"
    
    payload = {
        "name": "Invalid Workspace Rule",
        "priority_level": "P1",
        "threshold_minutes": 30,
        "is_active": True,
        "workspace_id": invalid_ws_id
    }
    
    response = await client.post("/api/v1/escalations/rules", json=payload, headers=headers)
    
    # Before fix, this returns 500 due to IntegrityError
    # After fix, it should return 404 or 400
    assert response.status_code == 404
    assert "Workspace not found" in response.json()["detail"]

@pytest.mark.asyncio
async def test_create_escalation_rule_success(client: AsyncClient, admin_token_a: str, workspace_b_id: str):
    """
    Test that creating an escalation rule with a valid workspace_id (Workspace B)
    by an Admin works correctly.
    """
    headers = {
        "Authorization": f"Bearer {admin_token_a}",
        "X-Nexus-Portal": "internal"
    }
    
    payload = {
        "name": "Valid Workspace B Rule",
        "priority_level": "P2",
        "threshold_minutes": 60,
        "is_active": True,
        "workspace_id": workspace_b_id
    }
    
    response = await client.post("/api/v1/escalations/rules", json=payload, headers=headers)
    
    assert response.status_code == 201
    data = response.json()
    assert data["name"] == "Valid Workspace B Rule"
    assert data["workspace_id"] == workspace_b_id