diff --git a/tableauserverclient/models/workbook_item.py b/tableauserverclient/models/workbook_item.py index 75ca0034e..872b70926 100644 --- a/tableauserverclient/models/workbook_item.py +++ b/tableauserverclient/models/workbook_item.py @@ -61,7 +61,13 @@ class as arguments. The workbook item specifies the project. User-defined description of the workbook. encrypt_extracts : bool | None - Indicates whether extracts are encrypted. + Assign this before publishing to send ``encryptExtracts``. ``True`` + encrypts the workbook's extracts and ``False`` leaves them unencrypted. + ``None`` omits the attribute. A value read from the server is kept on + the item but is not sent on publish until this property is assigned. + Assign ``None`` to omit it again after a previous assignment. The + server rejects ``false`` on a site that enforces encryption (409037) + and ``true`` on a site where encryption is disabled (409036). has_extracts : bool | None Indicates whether the workbook has extracts. @@ -169,6 +175,7 @@ def __init__( self._owner: UserItem | None = None self._location: LocationItem | None = None self._encrypt_extracts: bool | None = None + self._encrypt_extracts_assigned = False self._default_view_id: str | None = None self._share_description: str | None = None self._last_published_at: datetime.datetime | None = None @@ -357,6 +364,13 @@ def location(self) -> LocationItem | None: def encrypt_extracts(self) -> bool | None: return self._encrypt_extracts + @encrypt_extracts.setter + def encrypt_extracts(self, value: bool | None) -> None: + if value is not None and not isinstance(value, bool): + raise ValueError("Boolean expected for encrypt_extracts flag.") + self._encrypt_extracts = value + self._encrypt_extracts_assigned = True + @property def default_view_id(self) -> str | None: return self._default_view_id diff --git a/tableauserverclient/server/request_factory.py b/tableauserverclient/server/request_factory.py index 517a26956..b209beb5d 100644 --- a/tableauserverclient/server/request_factory.py +++ b/tableauserverclient/server/request_factory.py @@ -1070,6 +1070,8 @@ def _generate_xml( workbook_element.attrib["name"] = workbook_item.name if workbook_item.show_tabs: workbook_element.attrib["showTabs"] = str(workbook_item.show_tabs).lower() + if workbook_item._encrypt_extracts_assigned and workbook_item.encrypt_extracts is not None: + workbook_element.attrib["encryptExtracts"] = str(workbook_item.encrypt_extracts).lower() project_element = ET.SubElement(workbook_element, "project") project_element.attrib["id"] = str(workbook_item.project_id) diff --git a/test/test_workbook.py b/test/test_workbook.py index c0965f121..b98fc9b20 100644 --- a/test/test_workbook.py +++ b/test/test_workbook.py @@ -884,6 +884,45 @@ def test_publish_file_object_of_unknown_type_raises_exception(server: TSC.Server server.workbooks.publish(new_workbook, file_object, server.PublishMode.CreateNew) +def test_publish_sends_encrypt_extracts_when_set(server: TSC.Server) -> None: + new_workbook = TSC.WorkbookItem(name="Sample", show_tabs=False, project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760") + omitted = RequestFactory.Workbook._generate_xml(new_workbook) + assert "encryptExtracts" not in fromstring(omitted).find("workbook").attrib + + new_workbook.encrypt_extracts = True + encrypted = RequestFactory.Workbook._generate_xml(new_workbook) + assert fromstring(encrypted).find("workbook").get("encryptExtracts") == "true" + + new_workbook.encrypt_extracts = False + plain = RequestFactory.Workbook._generate_xml(new_workbook) + assert fromstring(plain).find("workbook").get("encryptExtracts") == "false" + + new_workbook.encrypt_extracts = None + cleared = RequestFactory.Workbook._generate_xml(new_workbook) + assert "encryptExtracts" not in fromstring(cleared).find("workbook").attrib + + with pytest.raises(ValueError): + new_workbook.encrypt_extracts = "true" # type: ignore[assignment] + + +def test_publish_does_not_send_encrypt_extracts_read_from_the_server(server: TSC.Server) -> None: + response = """ + + + + + + """ + fetched = TSC.WorkbookItem.from_response(response, server.namespace)[0] + assert fetched.encrypt_extracts is True + omitted = RequestFactory.Workbook._generate_xml(fetched) + assert "encryptExtracts" not in fromstring(omitted).find("workbook").attrib + + fetched.encrypt_extracts = False + chosen = RequestFactory.Workbook._generate_xml(fetched) + assert fromstring(chosen).find("workbook").get("encryptExtracts") == "false" + + def test_publish_multi_connection(server: TSC.Server) -> None: new_workbook = TSC.WorkbookItem(name="Sample", show_tabs=False, project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760") connection1 = TSC.ConnectionItem()