|
| 1 | +# Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +# license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright |
| 4 | +# ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +# the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import pytest |
| 19 | +import requests |
| 20 | +from elastic_transport import RequestsHttpNode, Urllib3HttpNode |
| 21 | +from elastic_transport.client_utils import DEFAULT |
| 22 | +from requests.auth import HTTPBasicAuth |
| 23 | + |
| 24 | +from elasticsearch import AsyncElasticsearch, Elasticsearch |
| 25 | + |
| 26 | + |
| 27 | +class CustomRequestHttpNode(RequestsHttpNode): |
| 28 | + pass |
| 29 | + |
| 30 | + |
| 31 | +class CustomUrllib3HttpNode(Urllib3HttpNode): |
| 32 | + pass |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.parametrize("client_class", [Elasticsearch, AsyncElasticsearch]) |
| 36 | +@pytest.mark.parametrize( |
| 37 | + "node_class", ["urllib3", "aiohttp", None, DEFAULT, CustomUrllib3HttpNode] |
| 38 | +) |
| 39 | +def test_error_for_requests_auth_node_class(client_class, node_class): |
| 40 | + http_auth = HTTPBasicAuth("username", "password") |
| 41 | + |
| 42 | + with pytest.raises(ValueError) as e: |
| 43 | + client_class( |
| 44 | + "http://localhost:9200", http_auth=http_auth, node_class=node_class |
| 45 | + ) |
| 46 | + assert str(e.value) == ( |
| 47 | + "Using a custom 'requests.auth.AuthBase' class for " |
| 48 | + "'http_auth' must be used with node_class='requests'" |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def test_error_for_requests_auth_async(): |
| 53 | + http_auth = HTTPBasicAuth("username", "password") |
| 54 | + |
| 55 | + with pytest.raises(ValueError) as e: |
| 56 | + AsyncElasticsearch( |
| 57 | + "http://localhost:9200", http_auth=http_auth, node_class="requests" |
| 58 | + ) |
| 59 | + assert str(e.value) == ( |
| 60 | + "Specified 'node_class' is not async, should be async instead" |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize( |
| 65 | + "node_class", ["requests", RequestsHttpNode, CustomRequestHttpNode] |
| 66 | +) |
| 67 | +def test_requests_auth(node_class): |
| 68 | + http_auth = HTTPBasicAuth("username", "password") |
| 69 | + |
| 70 | + client = Elasticsearch( |
| 71 | + "http://localhost:9200", http_auth=http_auth, node_class=node_class |
| 72 | + ) |
| 73 | + node = client.transport.node_pool.get() |
| 74 | + assert isinstance(node, RequestsHttpNode) |
| 75 | + assert isinstance(node.session, requests.Session) |
| 76 | + assert node.session.auth is http_auth |
0 commit comments