-
Notifications
You must be signed in to change notification settings - Fork 411
Expand file tree
/
Copy pathtest_service_accounts_validation.py
More file actions
192 lines (172 loc) · 5.1 KB
/
Copy pathtest_service_accounts_validation.py
File metadata and controls
192 lines (172 loc) · 5.1 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from __future__ import annotations
import pytest
from sqlalchemy import select
from tracecat.authz.enums import ScopeSource
from tracecat.db.models import ServiceAccount, ServiceAccountApiKey
from tracecat.exceptions import TracecatValidationError
from tracecat.pagination import CursorPaginationParams
from tracecat.service_accounts.constants import (
is_org_service_account_assignable_scope,
is_workspace_service_account_assignable_scope,
)
from tracecat.service_accounts.service import (
_apply_api_key_created_cursor,
_apply_created_cursor,
)
def test_apply_created_cursor_rejects_invalid_cursor() -> None:
with pytest.raises(
TracecatValidationError, match="Invalid cursor for service accounts"
):
_apply_created_cursor(
select(ServiceAccount),
params=CursorPaginationParams(limit=20, cursor="bad-cursor"),
)
def test_apply_api_key_created_cursor_rejects_invalid_cursor() -> None:
with pytest.raises(
TracecatValidationError, match="Invalid cursor for service account API keys"
):
_apply_api_key_created_cursor(
select(ServiceAccountApiKey),
params=CursorPaginationParams(limit=20, cursor="bad-cursor"),
)
@pytest.mark.parametrize(
"scope_name",
[
"workflow:read",
"case:create",
"case:update",
"case:delete",
"secret:update",
"table:read",
"table:create",
"table:update",
"table:delete",
"workspace:member:invite",
"agent:read",
"agent:create",
"agent:update",
"agent:delete",
"agent:execute",
"action:tools.slack.post_message:execute",
],
)
def test_workspace_service_account_assignable_scope_allows_supported_api_key_scopes(
scope_name: str,
) -> None:
assert is_workspace_service_account_assignable_scope(
name=scope_name,
source=ScopeSource.PLATFORM,
organization_id_present=False,
)
@pytest.mark.parametrize(
"scope_name",
[
"variable:read",
"variable:update",
"inbox:read",
"workspace:create",
"workspace:member:update",
"workspace:service_account:read",
],
)
def test_workspace_service_account_assignable_scope_rejects_user_only_scopes(
scope_name: str,
) -> None:
if scope_name.startswith("variable:"):
pytest.skip("Variable scopes are assignable to service accounts")
assert not is_workspace_service_account_assignable_scope(
name=scope_name,
source=ScopeSource.PLATFORM,
organization_id_present=False,
)
@pytest.mark.parametrize(
"scope_name",
[
"org:read",
"org:secret:read",
"org:secret:create",
"org:workspace:read",
"workspace:create",
"workflow:update",
"table:read",
"org:rbac:read",
"org:rbac:create",
"org:rbac:update",
"org:rbac:delete",
"action:tools.slack.post_message:execute",
],
)
def test_org_service_account_assignable_scope_allows_supported_api_key_scopes(
scope_name: str,
) -> None:
assert is_org_service_account_assignable_scope(
name=scope_name,
source=ScopeSource.PLATFORM,
organization_id_present=False,
)
@pytest.mark.parametrize(
"scope_name",
[
"org:settings:read",
"org:settings:update",
"org:registry:read",
"org:member:invite",
"variable:read",
"org:service_account:read",
],
)
def test_org_service_account_assignable_scope_rejects_user_only_scopes(
scope_name: str,
) -> None:
if scope_name.startswith("variable:"):
pytest.skip("Variable scopes are assignable to service accounts")
assert not is_org_service_account_assignable_scope(
name=scope_name,
source=ScopeSource.PLATFORM,
organization_id_present=False,
)
@pytest.mark.parametrize(
"scope_name",
[
"variable:read",
"variable:create",
"variable:update",
"variable:delete",
],
)
def test_workspace_service_account_assignable_scope_allows_variable_scopes(
scope_name: str,
) -> None:
assert is_workspace_service_account_assignable_scope(
name=scope_name,
source=ScopeSource.PLATFORM,
organization_id_present=False,
)
@pytest.mark.parametrize(
"scope_name",
[
"variable:read",
"variable:create",
"variable:update",
"variable:delete",
],
)
def test_org_service_account_assignable_scope_allows_variable_scopes(
scope_name: str,
) -> None:
assert is_org_service_account_assignable_scope(
name=scope_name,
source=ScopeSource.PLATFORM,
organization_id_present=False,
)
def test_service_account_assignable_scope_rejects_non_platform_scopes() -> None:
assert not is_workspace_service_account_assignable_scope(
name="workflow:read",
source=ScopeSource.CUSTOM,
organization_id_present=False,
)
assert not is_org_service_account_assignable_scope(
name="workflow:read",
source=ScopeSource.PLATFORM,
organization_id_present=True,
)