Coverage for netrun_rbac \ exceptions.py: 79%

24 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-11-28 14:16 -0800

1""" 

2RBAC Exceptions - Custom exception classes for RBAC operations 

3 

4Extracted from: Intirkast error handling patterns 

5""" 

6 

7 

8class RBACException(Exception): 

9 """Base exception for all RBAC-related errors""" 

10 

11 def __init__(self, message: str, status_code: int = 403): 

12 self.message = message 

13 self.status_code = status_code 

14 super().__init__(self.message) 

15 

16 

17class InsufficientPermissionsError(RBACException): 

18 """ 

19 Raised when user lacks required role or permission 

20 

21 HTTP Status: 403 Forbidden 

22 """ 

23 

24 def __init__(self, required_role: str, user_role: str | None = None): 

25 message = f"Insufficient permissions. Required role: {required_role}" 

26 if user_role: 

27 message += f" (current: {user_role})" 

28 super().__init__(message, status_code=403) 

29 

30 

31class TenantIsolationError(RBACException): 

32 """ 

33 Raised when attempting cross-tenant access 

34 

35 HTTP Status: 403 Forbidden 

36 Security Level: CRITICAL 

37 """ 

38 

39 def __init__(self, message: str = "Cross-tenant access denied"): 

40 super().__init__(message, status_code=403) 

41 

42 

43class ResourceOwnershipError(RBACException): 

44 """ 

45 Raised when attempting to access resource owned by another user 

46 

47 HTTP Status: 403 Forbidden 

48 """ 

49 

50 def __init__(self, message: str = "You can only access your own resources"): 

51 super().__init__(message, status_code=403) 

52 

53 

54class InvalidRoleError(RBACException): 

55 """ 

56 Raised when an invalid role is specified 

57 

58 HTTP Status: 400 Bad Request 

59 """ 

60 

61 def __init__(self, role: str): 

62 message = f"Invalid role: {role}. Must be one of: viewer, member, admin, owner" 

63 super().__init__(message, status_code=400) 

64 

65 

66class MissingTenantContextError(RBACException): 

67 """ 

68 Raised when tenant context is required but not set 

69 

70 HTTP Status: 400 Bad Request 

71 """ 

72 

73 def __init__(self, message: str = "Tenant context not set. Check authentication middleware."): 

74 super().__init__(message, status_code=400)