8000 Fix issues with connections publishing workbooks by nosnilmot · Pull Request #1171 · tableau/server-client-python · GitHub
[go: up one dir, main page]

Skip to content

Fix issues with connections publishing workbooks #1171

8000
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

Merged
merged 1 commit into from
Jan 17, 2023
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
14 changes: 11 additions & 3 deletions tableauserverclient/server/request_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from tableauserverclient.models.metric_item import MetricItem

from ..models import ConnectionCredentials
from ..models import ConnectionItem
from ..models import DataAlertItem
from ..models import FlowItem
Expand Down Expand Up @@ -55,6 +56,13 @@ def _add_connections_element(connections_element, connection):
connection_element.attrib["serverPort"] = connection.server_port
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:
connection_credentials = ConnectionCredentials(
connection.username, connection.password, embed=connection.embed_password
)
else:
connection_credentials = None
if connection_credentials:
_add_credentials_element(connection_element, connection_credentials)


Expand All @@ -66,7 +74,7 @@ def _add_hiddenview_element(views_element, view_name):

def _add_credentials_element(parent_element, connection_credentials):
credentials_element = ET.SubElement(parent_element, "connectionCredentials")
if not connection_credentials.password or not connection_credentials.name:
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
credentials_element.attrib["password"] = connection_credentials.password
Expand Down Expand Up @@ -177,7 +185,7 @@ def _generate_xml(self, datasource_item, connection_credentials=None, connection
if connection_credentials is not None:
_add_credentials_element(datasource_element, connection_credentials)

if connections is not None:
if connections is not None and len(connections) > 0:
connections_element = ET.SubElement(datasource_element, "connections")
for connection in connections:
_add_connections_element(connections_element, connection)
Expand Down Expand Up @@ -899,7 +907,7 @@ def _generate_xml(
if connection_credentials is not None:
_add_credentials_element(workbook_element, connection_credentials)

if connections is not None:
if connections is not None and len(connections) > 0:
connections_element = ET.SubElement(workbook_element, "connections")
for connection in connections:
_add_connections_element(connections_element, connection)
Expand Down
51 changes: 51 additions & 0 deletions test/test_workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,30 @@ def test_publish_multi_connection(self) -> None:
self.assertEqual(connection_results[1].get("serverAddress", None), "pgsql.test.com")
self.assertEqual(connection_results[1].find("connectionCredentials").get("password", None), "secret") # type: ignore[union-attr]

def test_publish_multi_connection_flat(self) -> None:
new_workbook = TSC.WorkbookItem(
name="Sample", show_tabs=False, project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760"
)
connection1 = TSC.ConnectionItem()
connection1.server_address = "mysql.test.com"
connection1.username = "test"
connection1.password = "secret"
connection1.embed_password = True
connection2 = TSC.ConnectionItem()
connection2.server_address = "pgsql.test.com"
connection2.username = "test"
connection2.password = "secret"
connection2.embed_password = True

response = RequestFactory.Workbook._generate_xml(new_workbook, connections=[connection1, connection2])
# Can't use ConnectionItem parser due to xml namespace problems
connection_results = fromstring(response).findall(".//connection")

self.assertEqual(connection_results[0].get("serverAddress", None), "mysql.test.com")
self.assertEqual(connection_results[0].find("connectionCredentials").get("name", None), "test") # type: ignore[union-attr]
self.assertEqual(connection_results[1].get("serverAddress", None), "pgsql.test.com")
self.assertEqual(connection_results[1].find("connectionCredentials").get("password", None), "secret") # type: ignore[union-attr]

def test_publish_single_connection(self) -> None:
new_workbook = TSC.WorkbookItem(
name="Sample", show_tabs=False, project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760"
Expand All @@ -762,6 +786,33 @@ def test_publish_single_connection(self) -> None:
self.assertEqual(credentials[0].get("password", None), "secret")
self.assertEqual(credentials[0].get("embed", None), "true")

def test_publish_single_connection_username_none(self) -> None:
new_workbook = TSC.WorkbookItem(
name="Sample", show_tabs=False, project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760"
)
connection_creds = TSC.ConnectionCredentials(None, "secret", True)

self.assertRaises(
ValueError,
RequestFactory.Workbook._generate_xml,
new_workbook,
connection_credentials=connection_creds,
)

def test_publish_single_connection_username_empty(self) -> None:
new_workbook = TSC.WorkbookItem(
name="Sample", show_tabs=False, project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760"
)
connection_creds = TSC.ConnectionCredentials("", "secret", True)

response = RequestFactory.Workbook._generate_xml(new_workbook, connection_credentials=connection_creds)
# Can't use ConnectionItem parser due to xml namespace problems
credentials = fromstring(response).findall(".//connectionCredentials")
self.assertEqual(len(credentials), 1)
self.assertEqual(credentials[0].get("name", None), "")
self.assertEqual(credentials[0].get("password", None), "secret")
self.assertEqual(credentials[0].get("embed", None), "true")

def test_credentials_and_multi_connect_raises_exception(self) -> None:
new_workbook = TSC.WorkbookItem(
name="Sample", show_tabs=False, project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760"
Expand Down
0