Skip to content
Open
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
6 changes: 4 additions & 2 deletions tableauserverclient/models/connection_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ class ConnectionCredentials:

Parameters
----------
name: str
The username for the connection.
name: str | None
The username for the connection. Omit it (``None``) to send a password
without a name. The server then keeps the username already stored on
the connection. An empty string is sent as ``name=""`` and clears it.

password: str
The password used for the connection.
Expand Down
9 changes: 5 additions & 4 deletions tableauserverclient/server/request_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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:
Expand Down
37 changes: 37 additions & 0 deletions test/test_workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down