def test_http_auth_disable_with_none():
    client = EnterpriseSearch(bearer_auth="api-token", node_class=DummyNode)
    client.perform_request("GET", "/")

    calls = client.transport.node_pool.get().calls
    assert len(calls) == 1
    assert calls[-1][1]["headers"]["Authorization"] == "Bearer api-token"

    client.options(bearer_auth=None).get_version()
    assert len(calls) == 2
    assert "Authorization" not in calls[-1][1]["headers"]

    client.options(basic_auth=None).get_version()
    assert len(calls) == 3
    assert "Authorization" not in calls[-1][1]["headers"]
示例#2
0
def test_sub_client_auth():
    client = EnterpriseSearch(node_class=DummyNode, meta_header=False)

    # Using options on individual clients
    client.options(bearer_auth="enterprise-search").perform_request(
        "GET", "/enterprise-search")
    client.app_search.options(bearer_auth="app-search").perform_request(
        "GET", "/app-search")
    client.workplace_search.options(
        bearer_auth="workplace-search").perform_request(
            "GET", "/workplace-search")

    # Authenticating doesn't modify other clients
    client.options(bearer_auth="not-app-search").app_search.perform_request(
        "GET", "/not-app-search")
    client.options(
        bearer_auth="not-workplace-search").workplace_search.perform_request(
            "GET", "/not-workplace-search")

    # The Authorziation header gets hidden
    calls = client.transport.node_pool.get().calls
    headers = [(target, kwargs["headers"].get("Authorization", None))
               for ((_, target), kwargs) in calls]

    assert headers == [
        ("/enterprise-search", "Bearer enterprise-search"),
        ("/app-search", "Bearer app-search"),
        ("/workplace-search", "Bearer workplace-search"),
        ("/not-app-search", None),
        ("/not-workplace-search", None),
    ]