Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing set default permissions for virtual connections #1535

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tableauserverclient/models/project_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def _set_permissions(self, permissions):
self._permissions = permissions

def _set_default_permissions(self, permissions, content_type):
attr = f"_default_{content_type}_permissions"
attr = f"_default_{content_type}_permissions".lower()
setattr(
self,
attr,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version='1.0' encoding='UTF-8'?>
<tsResponse xmlns="http://tableau.com/api"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api https://help.tableau.com/samples/en-us/rest_api/ts-api_3_23.xsd">
<permissions>
<project id="90f09c23-440a-44ee-857c-5744a51d5b08" name="Project3">
<owner id="26183d16-82f7-4fcf-b163-0e607bf292bc"/>
</project>
<granteeCapabilities>
<group id="c8f2773a-c83a-11e8-8c8f-33e6d787b506"/>
<capabilities>
<capability name="Read" mode="Allow"/>
<capability name="Connect" mode="Allow"/>
<capability name="ChangeHierarchy" mode="Deny"/>
<capability name="Delete" mode="Deny"/>
<capability name="ChangePermissions" mode="Deny"/>
</capabilities>
</granteeCapabilities>
</permissions>
</tsResponse>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version='1.0' encoding='UTF-8'?>
<tsResponse xmlns="http://tableau.com/api"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api https://help.tableau.com/samples/en-us/rest_api/ts-api_3_23.xsd">
<permissions>
<project id="90f09c23-440a-44ee-857c-5744a51d5b08" name="Project3">
<owner id="26183d16-82f7-4fcf-b163-0e607bf292bc"/>
</project>
<granteeCapabilities>
<group id="c8f2773a-c83a-11e8-8c8f-33e6d787b506"/>
<capabilities>
<capability mode="Deny" name="Read"/>
<capability mode="Allow" name="Delete"/>
<capability mode="Allow" name="ChangeHierarchy"/>
</capabilities>
</granteeCapabilities>
</permissions>
</tsResponse>
107 changes: 107 additions & 0 deletions test/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
POPULATE_PERMISSIONS_XML = "project_populate_permissions.xml"
POPULATE_WORKBOOK_DEFAULT_PERMISSIONS_XML = "project_populate_workbook_default_permissions.xml"
UPDATE_DATASOURCE_DEFAULT_PERMISSIONS_XML = "project_update_datasource_default_permissions.xml"
POPULATE_VIRTUALCONNECTION_DEFAULT_PERMISSIONS_XML = "project_populate_virtualconnection_default_permissions.xml"
UPDATE_VIRTUALCONNECTION_DEFAULT_PERMISSIONS_XML = "project_update_virtualconnection_default_permissions.xml"


class ProjectTests(unittest.TestCase):
Expand Down Expand Up @@ -303,3 +305,108 @@ def test_delete_workbook_default_permission(self) -> None:
m.delete(f"{self.baseurl}/{endpoint}/Delete/Deny", status_code=204)
m.delete(f"{self.baseurl}/{endpoint}/ChangePermissions/Allow", status_code=204)
self.server.projects.delete_workbook_default_permissions(item=single_project, rule=rules)

def test_populate_virtualconnection_default_permissions(self):
response_xml = read_xml_asset(POPULATE_VIRTUALCONNECTION_DEFAULT_PERMISSIONS_XML)

self.server.version = "3.23"
base_url = self.server.projects.baseurl

with requests_mock.mock() as m:
m.get(
base_url + "/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/default-permissions/virtualConnections",
text=response_xml,
)
project = TSC.ProjectItem("test", "1d0304cd-3796-429f-b815-7258370b9b74")
project._id = "9dbd2263-16b5-46e1-9c43-a76bb8ab65fb"

self.server.projects.populate_virtualconnection_default_permissions(project)
permissions = project.default_virtualconnection_permissions

rule = permissions.pop()

self.assertEqual("c8f2773a-c83a-11e8-8c8f-33e6d787b506", rule.grantee.id)
self.assertEqual("group", rule.grantee.tag_name)
self.assertDictEqual(
rule.capabilities,
{
TSC.Permission.Capability.Read: TSC.Permission.Mode.Allow,
TSC.Permission.Capability.Connect: TSC.Permission.Mode.Allow,
TSC.Permission.Capability.ChangeHierarchy: TSC.Permission.Mode.Deny,
TSC.Permission.Capability.Delete: TSC.Permission.Mode.Deny,
TSC.Permission.Capability.ChangePermissions: TSC.Permission.Mode.Deny,
},
)

def test_update_virtualconnection_default_permissions(self):
response_xml = read_xml_asset(UPDATE_VIRTUALCONNECTION_DEFAULT_PERMISSIONS_XML)

self.server.version = "3.23"
base_url = self.server.projects.baseurl

with requests_mock.mock() as m:
m.put(
base_url + "/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/default-permissions/virtualConnections",
text=response_xml,
)
project = TSC.ProjectItem("test", "1d0304cd-3796-429f-b815-7258370b9b74")
project._id = "9dbd2263-16b5-46e1-9c43-a76bb8ab65fb"

group = TSC.GroupItem("test-group")
group._id = "c8f2773a-c83a-11e8-8c8f-33e6d787b506"

capabilities = {
TSC.Permission.Capability.ChangeHierarchy: TSC.Permission.Mode.Allow,
TSC.Permission.Capability.Delete: TSC.Permission.Mode.Allow,
TSC.Permission.Capability.Read: TSC.Permission.Mode.Deny,
}

rules = [TSC.PermissionsRule(GroupItem.as_reference(group.id), capabilities)]
new_rules = self.server.projects.update_virtualconnection_default_permissions(project, rules)

rule = new_rules.pop()

self.assertEqual(group.id, rule.grantee.id)
self.assertEqual("group", rule.grantee.tag_name)
self.assertDictEqual(
rule.capabilities,
{
TSC.Permission.Capability.ChangeHierarchy: TSC.Permission.Mode.Allow,
TSC.Permission.Capability.Delete: TSC.Permission.Mode.Allow,
TSC.Permission.Capability.Read: TSC.Permission.Mode.Deny,
},
)

def test_delete_virtualconnection_default_permimssions(self):
response_xml = read_xml_asset(POPULATE_VIRTUALCONNECTION_DEFAULT_PERMISSIONS_XML)

self.server.version = "3.23"
base_url = self.server.projects.baseurl

with requests_mock.mock() as m:
m.get(
base_url + "/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/default-permissions/virtualConnections",
text=response_xml,
)

project = TSC.ProjectItem("test", "1d0304cd-3796-429f-b815-7258370b9b74")
project._id = "9dbd2263-16b5-46e1-9c43-a76bb8ab65fb"

group = TSC.GroupItem("test-group")
group._id = "c8f2773a-c83a-11e8-8c8f-33e6d787b506"

self.server.projects.populate_virtualconnection_default_permissions(project)
permissions = project.default_virtualconnection_permissions

del_caps = {
TSC.Permission.Capability.ChangeHierarchy: TSC.Permission.Mode.Deny,
TSC.Permission.Capability.Connect: TSC.Permission.Mode.Allow,
}

rule = TSC.PermissionsRule(GroupItem.as_reference(group.id), del_caps)

endpoint = f"{project.id}/default-permissions/virtualConnections/groups/{group.id}"
m.delete(f"{base_url}/{endpoint}/ChangeHierarchy/Deny", status_code=204)
m.delete(f"{base_url}/{endpoint}/Connect/Allow", status_code=204)

self.server.projects.delete_virtualconnection_default_permissions(project, rule)
Loading