-
Notifications
You must be signed in to change notification settings - Fork 3k
/
test_policies.py
209 lines (176 loc) · 6.12 KB
/
test_policies.py
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import pytest
import tenacity
from tests.utils import get_root_urn, get_sleep_info
TEST_POLICY_NAME = "Updated Platform Policy"
sleep_sec, sleep_times = get_sleep_info()
@pytest.fixture(scope="module", autouse=True)
def test_frontend_list_policies(auth_session):
"""Fixture to execute setup before and tear down after all tests are run"""
res_data = listPolicies(auth_session)
assert res_data
assert res_data["data"]
assert res_data["data"]["listPolicies"]
assert res_data["data"]["listPolicies"]["start"] == 0
assert res_data["data"]["listPolicies"]["count"] > 0
assert len(res_data["data"]["listPolicies"]["policies"]) > 0
# Verify that policy to be created does not exist before the test.
# If it does, this test class's state is tainted
result = filter(
lambda x: x["name"] == TEST_POLICY_NAME,
res_data["data"]["listPolicies"]["policies"],
)
assert len(list(result)) == 0
# Run remaining tests.
yield
res_data = listPolicies(auth_session)
assert res_data
assert res_data["data"]
assert res_data["data"]["listPolicies"]
# Verify that policy that was created is no longer in the list
result = filter(
lambda x: x["name"] == TEST_POLICY_NAME,
res_data["data"]["listPolicies"]["policies"],
)
assert len(list(result)) == 0
@tenacity.retry(
stop=tenacity.stop_after_attempt(sleep_times), wait=tenacity.wait_fixed(sleep_sec)
)
def _ensure_policy_present(auth_session, new_urn):
res_data = listPolicies(auth_session)
assert res_data
assert res_data["data"]
assert res_data["data"]["listPolicies"]
# Verify that the updated policy appears in the list and has the appropriate changes
result = list(
filter(
lambda x: x["urn"] == new_urn, res_data["data"]["listPolicies"]["policies"]
)
)
print(result)
assert len(result) == 1
assert result[0]["description"] == "Updated Metadaata Policy"
assert result[0]["privileges"] == ["EDIT_ENTITY_TAGS", "EDIT_ENTITY_GLOSSARY_TERMS"]
assert result[0]["actors"]["allUsers"]
def test_frontend_policy_operations(auth_session):
json = {
"query": """mutation createPolicy($input: PolicyUpdateInput!) {\n
createPolicy(input: $input) }""",
"variables": {
"input": {
"type": "METADATA",
"name": "Test Metadata Policy",
"description": "My Metadaata Policy",
"state": "ACTIVE",
"resources": {"type": "dataset", "allResources": True},
"privileges": ["EDIT_ENTITY_TAGS"],
"actors": {
"users": [get_root_urn()],
"resourceOwners": False,
"allUsers": False,
"allGroups": False,
},
}
},
}
response = auth_session.post(
f"{auth_session.frontend_url()}/api/v2/graphql", json=json
)
response.raise_for_status()
res_data = response.json()
assert res_data
assert res_data["data"]
assert res_data["data"]["createPolicy"]
new_urn = res_data["data"]["createPolicy"]
update_json = {
"query": """mutation updatePolicy($urn: String!, $input: PolicyUpdateInput!) {\n
updatePolicy(urn: $urn, input: $input) }""",
"variables": {
"urn": new_urn,
"input": {
"type": "METADATA",
"state": "ACTIVE",
"name": "Test Metadata Policy",
"description": "Updated Metadaata Policy",
"privileges": ["EDIT_ENTITY_TAGS", "EDIT_ENTITY_GLOSSARY_TERMS"],
"actors": {
"resourceOwners": False,
"allUsers": True,
"allGroups": False,
},
},
},
}
response = auth_session.post(
f"{auth_session.frontend_url()}/api/v2/graphql", json=update_json
)
response.raise_for_status()
res_data = response.json()
# Check updated was submitted successfully
assert res_data
assert res_data["data"]
assert res_data["data"]["updatePolicy"]
assert res_data["data"]["updatePolicy"] == new_urn
_ensure_policy_present(auth_session, new_urn)
# Now test that the policy can be deleted
json = {
"query": """mutation deletePolicy($urn: String!) {\n
deletePolicy(urn: $urn) }""",
"variables": {"urn": new_urn},
}
response = auth_session.post(
f"{auth_session.frontend_url()}/api/v2/graphql", json=json
)
response.raise_for_status()
res_data = response.json()
res_data = listPolicies(auth_session)
assert res_data
assert res_data["data"]
assert res_data["data"]["listPolicies"]
# Verify that the URN is no longer in the list
result = filter(
lambda x: x["urn"] == new_urn,
res_data["data"]["listPolicies"]["policies"],
)
assert len(list(result)) == 0
def listPolicies(auth_session):
json = {
"query": """query listPolicies($input: ListPoliciesInput!) {\n
listPolicies(input: $input) {\n
start\n
count\n
total\n
policies {\n
urn\n
type\n
name\n
description\n
state\n
resources {\n
type\n
allResources\n
resources\n
}\n
privileges\n
actors {\n
users\n
groups\n
allUsers\n
allGroups\n
resourceOwners\n
}\n
editable\n
}\n
}\n
}""",
"variables": {
"input": {
"start": "0",
"count": "20",
}
},
}
response = auth_session.post(
f"{auth_session.frontend_url()}/api/v2/graphql", json=json
)
response.raise_for_status()
return response.json()