File: test_client_utils.py

package info (click to toggle)
python-elastic-transport 9.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 644 kB
  • sloc: python: 6,673; makefile: 17
file content (259 lines) | stat: -rw-r--r-- 8,292 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#  Licensed to Elasticsearch B.V. under one or more contributor
#  license agreements. See the NOTICE file distributed with
#  this work for additional information regarding copyright
#  ownership. Elasticsearch B.V. licenses this file to you under
#  the Apache License, Version 2.0 (the "License"); you may
#  not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
# 	http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an
#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
#  specific language governing permissions and limitations
#  under the License.

from platform import python_version

import pytest

from elastic_transport import Urllib3HttpNode, __version__
from elastic_transport.client_utils import (
    basic_auth_to_header,
    client_meta_version,
    create_user_agent,
    parse_cloud_id,
    url_to_node_config,
)


def test_create_user_agent():
    assert create_user_agent(
        "enterprise-search-python", "7.10.0"
    ) == "enterprise-search-python/7.10.0 (Python/{}; elastic-transport/{})".format(
        python_version(),
        __version__,
    )


@pytest.mark.parametrize(
    ["version", "meta_version"],
    [
        ("7.10.0", "7.10.0"),
        ("7.10.0-alpha1", "7.10.0p"),
        ("3.9.0b1", "3.9.0p"),
        ("3.9.pre1", "3.9p"),
        ("3.7.4.post1", "3.7.4"),
        ("3.7.4.post", "3.7.4"),
    ],
)
def test_client_meta_version(version, meta_version):
    assert client_meta_version(version) == meta_version


def test_parse_cloud_id():
    cloud_id = parse_cloud_id(
        "cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVk"
        "MWNmMjIxMTBlMmY5NyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5Ng=="
    )
    assert cloud_id.cluster_name == "cluster"
    assert cloud_id.es_address == (
        "4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io",
        443,
    )
    assert cloud_id.kibana_address == (
        "4fa8821e75634032bed1cf22110e2f96.us-east-1.aws.found.io",
        443,
    )


@pytest.mark.parametrize(
    ["cloud_id", "port"],
    [
        (
            ":dXMtZWFzdC0xLmF3cy5mb3VuZC5pbzo5MjQzJDRmYTg4MjFlNzU2MzQwMzJiZ"
            "WQxY2YyMjExMGUyZjk3JDRmYTg4MjFlNzU2MzQwMzJiZWQxY2YyMjExMGUyZjk2",
            9243,
        ),
        (
            ":dXMtZWFzdC0xLmF3cy5mb3VuZC5pbzo0NDMkNGZhODgyMWU3NTYzNDAzMmJlZD"
            "FjZjIyMTEwZTJmOTckNGZhODgyMWU3NTYzNDAzMmJlZDFjZjIyMTEwZTJmOTY=",
            443,
        ),
    ],
)
def test_parse_cloud_id_ports(cloud_id, port):
    cloud_id = parse_cloud_id(cloud_id)
    assert cloud_id.cluster_name == ""
    assert cloud_id.es_address == (
        "4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io",
        port,
    )
    assert cloud_id.kibana_address == (
        "4fa8821e75634032bed1cf22110e2f96.us-east-1.aws.found.io",
        port,
    )


@pytest.mark.parametrize(
    "cloud_id",
    [
        "cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5NyQ=",
        "cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5Nw==",
    ],
)
def test_parse_cloud_id_no_kibana(cloud_id):
    cloud_id = parse_cloud_id(cloud_id)
    assert cloud_id.cluster_name == "cluster"
    assert cloud_id.es_address == (
        "4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io",
        443,
    )
    assert cloud_id.kibana_address is None


@pytest.mark.parametrize(
    "cloud_id",
    [
        "cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbzo0NDMkJA==",
        "cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbzo0NDM=",
    ],
)
def test_parse_cloud_id_no_es(cloud_id):
    cloud_id = parse_cloud_id(cloud_id)
    assert cloud_id.cluster_name == "cluster"
    assert cloud_id.es_address is None
    assert cloud_id.kibana_address is None


@pytest.mark.parametrize(
    "cloud_id",
    [
        "cluster:",
        "dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5NyQ=",
        "cluster:ā",
    ],
)
def test_invalid_cloud_id(cloud_id):
    with pytest.raises(ValueError) as e:
        parse_cloud_id(cloud_id)
    assert str(e.value) == "Cloud ID is not properly formatted"


@pytest.mark.parametrize(
    ["url", "node_base_url", "path_prefix"],
    [
        ("https://localhost", "https://localhost:443", ""),
        ("http://localhost:3002", "http://localhost:3002", ""),
        ("http://127.0.0.1:3002", "http://127.0.0.1:3002", ""),
        ("http://127.0.0.1:3002/", "http://127.0.0.1:3002", ""),
        (
            "http://127.0.0.1:3002/path-prefix",
            "http://127.0.0.1:3002/path-prefix",
            "/path-prefix",
        ),
        (
            "http://localhost:3002/url-prefix/",
            "http://localhost:3002/url-prefix",
            "/url-prefix",
        ),
        (
            "https://localhost/url-prefix",
            "https://localhost:443/url-prefix",
            "/url-prefix",
        ),
        ("http://[::1]:3002/url-prefix", "http://[::1]:3002/url-prefix", "/url-prefix"),
        ("https://[::1]:0/", "https://[::1]:0", ""),
    ],
)
def test_url_to_node_config(url, node_base_url, path_prefix):
    node_config = url_to_node_config(url)
    assert Urllib3HttpNode(node_config).base_url == node_base_url

    assert "[" not in node_config.host
    assert isinstance(node_config.port, int)
    assert node_config.path_prefix == path_prefix
    assert url.lower().startswith(node_config.scheme)


@pytest.mark.parametrize(
    "url",
    [
        "localhost:0",
        "[::1]:3002/url-prefix",
        "localhost",
        "localhost/",
        "localhost:3",
        "[::1]/url-prefix/",
        "[::1]",
        "[::1]:3002",
        "http://localhost",
        "localhost/url-prefix/",
        "localhost:3002/url-prefix",
        "http://localhost/url-prefix",
    ],
)
def test_url_to_node_config_error_missing_component(url):
    with pytest.raises(ValueError) as e:
        url_to_node_config(url)
    assert (
        str(e.value)
        == "URL must include a 'scheme', 'host', and 'port' component (ie 'https://localhost:9200')"
    )


@pytest.mark.parametrize(
    ["url", "port"],
    [
        ("http://127.0.0.1", 80),
        ("http://[::1]", 80),
        ("HTTPS://localhost", 443),
        ("https://localhost/url-prefix", 443),
    ],
)
def test_url_to_node_config_use_default_ports_for_scheme(url, port):
    node_config = url_to_node_config(url, use_default_ports_for_scheme=True)
    assert node_config.port == port


def test_url_with_auth_into_authorization():
    node_config = url_to_node_config("http://localhost:9200")
    assert node_config.headers == {}

    node_config = url_to_node_config("http://@localhost:9200")
    assert node_config.headers == {}

    node_config = url_to_node_config("http://user:pass@localhost:9200")
    assert node_config.headers == {"Authorization": "Basic dXNlcjpwYXNz"}

    node_config = url_to_node_config("http://user:@localhost:9200")
    assert node_config.headers == {"Authorization": "Basic dXNlcjo="}

    node_config = url_to_node_config("http://user@localhost:9200")
    assert node_config.headers == {"Authorization": "Basic dXNlcjo="}

    node_config = url_to_node_config("http://me@example.com:password@localhost:9200")
    assert node_config.headers == {
        "Authorization": "Basic bWVAZXhhbXBsZS5jb206cGFzc3dvcmQ="
    }

    # ensure username and password are passed to basic auth unmodified
    basic_auth = basic_auth_to_header(("user:@", "@password"))
    node_config = url_to_node_config("http://user:@:@password@localhost:9200")
    assert node_config.headers == {"Authorization": basic_auth}
    node_config = url_to_node_config("http://user%3A%40:%40password@localhost:9200")
    assert node_config.headers == {"Authorization": basic_auth}


@pytest.mark.parametrize(
    "basic_auth", ["", b"", ("",), ("", 1), (1, ""), ["", ""], False, object()]
)
def test_basic_auth_errors(basic_auth):
    with pytest.raises(ValueError) as e:
        basic_auth_to_header(basic_auth)
    assert (
        str(e.value)
        == "'basic_auth' must be a 2-tuple of str/bytes (username, password)"
    )