diff --git a/tableauserverclient/models/connection_credentials.py b/tableauserverclient/models/connection_credentials.py index aaa2f1bed..f9fd5a9dd 100644 --- a/tableauserverclient/models/connection_credentials.py +++ b/tableauserverclient/models/connection_credentials.py @@ -10,8 +10,11 @@ class ConnectionCredentials: Parameters ---------- - name: str - The username for the connection. + name: str | None + The username for the connection. ``None`` omits the ``name`` attribute. + An empty string is sent as ``name=""``. Publish Workbook and Publish + Data Source mark both ``name`` and ``password`` as optional. They do + not say what a server does when ``name`` is omitted or empty. password: str The password used for the connection. diff --git a/tableauserverclient/server/request_factory.py b/tableauserverclient/server/request_factory.py index 517a26956..88dcbe5f0 100644 --- a/tableauserverclient/server/request_factory.py +++ b/tableauserverclient/server/request_factory.py @@ -48,7 +48,7 @@ def _add_connections_element(connections_element, connection): connection_element.attrib["databaseName"] = connection.database_name if connection.connection_credentials: connection_credentials = connection.connection_credentials - elif connection.username is not None and connection.password is not None and connection.embed_password is not None: + elif connection.password is not None and connection.embed_password is not None: connection_credentials = ConnectionCredentials( connection.username, connection.password, embed=connection.embed_password ) @@ -70,10 +70,11 @@ def _add_view_element(views_element, view_id): def _add_credentials_element(parent_element, connection_credentials): + if connection_credentials.password is None: + raise ValueError("Connection Credentials must have a password") credentials_element = ET.SubElement(parent_element, "connectionCredentials") - if connection_credentials.password is None or connection_credentials.name is None: - raise ValueError("Connection Credentials must have a name and password") - credentials_element.attrib["name"] = connection_credentials.name + if connection_credentials.name is not None: + credentials_element.attrib["name"] = connection_credentials.name credentials_element.attrib["password"] = connection_credentials.password credentials_element.attrib["embed"] = "true" if connection_credentials.embed else "false" if connection_credentials.oauth: diff --git a/test/request_factory/test_workbook_requests.py b/test/request_factory/test_workbook_requests.py index b114e04a0..c545efcc3 100644 --- a/test/request_factory/test_workbook_requests.py +++ b/test/request_factory/test_workbook_requests.py @@ -28,10 +28,23 @@ def test_generate_xml_invalid_connection_credentials() -> None: conn = TSC.ConnectionItem() conn.server_address = "address" creds = TSC.ConnectionCredentials("username", "password") - creds.name = None + creds.password = None conn.connection_credentials = creds with pytest.raises(ValueError): - request = TSC_RF.RequestFactory.Workbook._generate_xml(workbook_item, connections=[conn]) + TSC_RF.RequestFactory.Workbook._generate_xml(workbook_item, connections=[conn]) + + +def test_generate_xml_connection_credentials_without_a_name() -> None: + workbook_item: TSC.WorkbookItem = TSC.WorkbookItem("name", "project_id") + conn = TSC.ConnectionItem() + conn.server_address = "address" + creds = TSC.ConnectionCredentials("username", "password") + creds.name = None + conn.connection_credentials = creds + request = TSC_RF.RequestFactory.Workbook._generate_xml(workbook_item, connections=[conn]) + credentials = request.decode().split("connectionCredentials", 1)[1] + assert "name=" not in credentials.split(">", 1)[0] + assert 'password="password"' in request.decode() def test_generate_xml_valid_connection_credentials() -> None: diff --git a/test/test_datasource.py b/test/test_datasource.py index 861aba430..be112b891 100644 --- a/test/test_datasource.py +++ b/test/test_datasource.py @@ -941,6 +941,25 @@ def test_publish_multi_connection(server) -> None: assert connection_results[1].find("connectionCredentials").get("password", None) == "secret" +def test_publish_connection_password_without_a_name(server) -> None: + new_datasource = TSC.DatasourceItem(name="Sample", project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760") + named = TSC.ConnectionItem() + named.server_address = "sql.test.com" + named.connection_credentials = TSC.ConnectionCredentials(None, "secret", True) + unnamed = TSC.ConnectionItem() + unnamed.server_address = "sql.test.com" + unnamed.password = "secret" + unnamed.embed_password = True + + for connection in (named, unnamed): + response = RequestFactory.Datasource._generate_xml(new_datasource, connections=[connection]) + credentials = fromstring(response).find(".//connectionCredentials") + assert credentials is not None + assert "name" not in credentials.attrib + assert credentials.get("password") == "secret" + assert credentials.get("embed") == "true" + + def test_publish_single_connection(server) -> None: new_datasource = TSC.DatasourceItem(name="Sample", project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760") connection_creds = TSC.ConnectionCredentials("test", "secret", True) diff --git a/test/test_workbook.py b/test/test_workbook.py index c0965f121..ebc5e424a 100644 --- a/test/test_workbook.py +++ b/test/test_workbook.py @@ -926,6 +926,43 @@ def test_publish_multi_connection_flat(server: TSC.Server) -> None: assert connection_results[1].find("connectionCredentials").get("password", None) == "secret" +def test_publish_connection_password_without_a_name(server: TSC.Server) -> None: + new_workbook = TSC.WorkbookItem(name="Sample", show_tabs=False, project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760") + named = TSC.ConnectionItem() + named.server_address = "sql.test.com" + named.connection_credentials = TSC.ConnectionCredentials(None, "secret", True) + unnamed = TSC.ConnectionItem() + unnamed.server_address = "sql.test.com" + unnamed.password = "secret" + unnamed.embed_password = True + + for connection in (named, unnamed): + response = RequestFactory.Workbook._generate_xml(new_workbook, connections=[connection]) + credentials = fromstring(response).find(".//connectionCredentials") + assert credentials is not None + assert "name" not in credentials.attrib + assert credentials.get("password") == "secret" + assert credentials.get("embed") == "true" + + blank = TSC.ConnectionItem() + blank.server_address = "sql.test.com" + blank.connection_credentials = TSC.ConnectionCredentials("", "secret", True) + response = RequestFactory.Workbook._generate_xml(new_workbook, connections=[blank]) + credentials = fromstring(response).find(".//connectionCredentials") + assert credentials is not None + assert credentials.get("name") == "" + + +def test_publish_connection_credentials_require_a_password(server: TSC.Server) -> None: + new_workbook = TSC.WorkbookItem(name="Sample", show_tabs=False, project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760") + connection = TSC.ConnectionItem() + connection.server_address = "sql.test.com" + connection.connection_credentials = TSC.ConnectionCredentials("test", None, True) + + with pytest.raises(ValueError, match="must have a password"): + RequestFactory.Workbook._generate_xml(new_workbook, connections=[connection]) + + def test_synchronous_publish_timeout_error(server: TSC.Server) -> None: with requests_mock.mock() as m: m.register_uri("POST", server.workbooks.baseurl, status_code=504)