mirror of
https://github.com/datahub-project/datahub.git
synced 2025-11-15 02:43:19 +00:00
210 lines
6.1 KiB
Python
210 lines
6.1 KiB
Python
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()
|