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
3 changes: 1 addition & 2 deletions tableauserverclient/server/endpoint/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ def __init__(self, parent_srv: "Server"):
def set_parameters(http_options, auth_token, content, content_type, parameters) -> dict[str, Any]:
parameters = parameters or {}
parameters.update(http_options)
if "headers" not in parameters:
parameters["headers"] = {}
parameters["headers"] = dict(parameters.get("headers") or {})

if auth_token is not None:
parameters["headers"][TABLEAU_AUTH_HEADER] = auth_token
Expand Down
67 changes: 67 additions & 0 deletions test/test_http_options_isolation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import copy
from pathlib import Path

import requests_mock

import tableauserverclient as TSC
from tableauserverclient.server.endpoint.endpoint import TABLEAU_AUTH_HEADER

SIGN_IN_XML = (Path(__file__).parent / "assets" / "auth_sign_in.xml").read_text()
TOKEN_A = "eIX6mvFsqyansa4KqEI1UwOpS8ggRs2l"


def _shared_options() -> dict:
return {"headers": {"X-Corp": "1"}, "timeout": 30}


def _sign_in_and_call(server: TSC.Server, m: requests_mock.Mocker) -> None:
m.post(server.auth.baseurl + "/signin", text=SIGN_IN_XML)
server.auth.sign_in(TSC.TableauAuth("user", "password", site_id="Samples"))
m.get(server.workbooks.baseurl, text="<tsResponse/>")
server.workbooks.get_request(server.workbooks.baseurl)


def test_sign_in_does_not_mutate_caller_http_options() -> None:
shared = _shared_options()
original = copy.deepcopy(shared)
server_a = TSC.Server("http://a.example", http_options=shared)
server_b = TSC.Server("http://b.example", http_options=shared)

with requests_mock.mock() as m:
_sign_in_and_call(server_a, m)

assert shared == original
assert TABLEAU_AUTH_HEADER not in server_a.http_options["headers"]
assert TABLEAU_AUTH_HEADER not in server_b.http_options["headers"]
assert server_a.http_options["headers"]["X-Corp"] == "1"


def test_sign_in_to_second_server_does_not_send_first_server_token() -> None:
shared = _shared_options()
server_a = TSC.Server("http://a.example", http_options=shared)
server_b = TSC.Server("http://b.example", http_options=shared)

with requests_mock.mock() as m:
_sign_in_and_call(server_a, m)
assert m.last_request.headers[TABLEAU_AUTH_HEADER] == TOKEN_A
m.post(server_b.auth.baseurl + "/signin", text=SIGN_IN_XML)
server_b.auth.sign_in(TSC.TableauAuth("user", "password", site_id="Samples"))
sign_in_b = m.last_request

assert sign_in_b.hostname == "b.example"
assert TABLEAU_AUTH_HEADER not in sign_in_b.headers
assert sign_in_b.headers["X-Corp"] == "1"


def test_requests_after_sign_out_carry_no_token() -> None:
server = TSC.Server("http://a.example", http_options=_shared_options())

with requests_mock.mock() as m:
_sign_in_and_call(server, m)
m.post(server.auth.baseurl + "/signout", text="")
server.auth.sign_out()
m.post(server.auth.baseurl + "/signin", text=SIGN_IN_XML)
server.auth.sign_in(TSC.TableauAuth("user", "password", site_id="Samples"))
sign_in_again = m.last_request

assert TABLEAU_AUTH_HEADER not in sign_in_again.headers
Loading