Skip to content

Commit 712552b

Browse files
authored
fix: initialization of the client without auth credentials (#443)
1 parent 1cad3f2 commit 712552b

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## 1.30.0 [unreleased]
22

3+
## 1.29.1 [unreleased]
4+
5+
### Bug Fixes
6+
1. [#443](https://github.com/influxdata/influxdb-client-python/pull/443): Initialization of the client without auth credentials
7+
38
## 1.29.0 [2022-05-20]
49

510
### Breaking Changes

influxdb_client/client/_base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,17 @@ def __init__(self, url, token, debug=None, timeout=10_000, enable_gzip=False, or
7272

7373
self.conf.username = kwargs.get('username', None)
7474
self.conf.password = kwargs.get('password', None)
75+
# defaults
76+
self.auth_header_name = None
77+
self.auth_header_value = None
7578
# by token
76-
self.auth_header_name = "Authorization"
7779
if self.token:
80+
self.auth_header_name = "Authorization"
7881
self.auth_header_value = "Token " + self.token
7982
# by HTTP basic
8083
auth_basic = kwargs.get('auth_basic', False)
8184
if auth_basic:
85+
self.auth_header_name = "Authorization"
8286
self.auth_header_value = "Basic " + base64.b64encode(token.encode()).decode()
8387
# by username, password
8488
if self.conf.username and self.conf.password:

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
'randomize>=0.13',
2222
'pytest>=5.0.0',
2323
'httpretty==1.0.5',
24-
'psutil>=5.6.3'
24+
'psutil>=5.6.3',
25+
'aioresponses>=0.7.3'
2526
]
2627

2728
extra_requires = [

tests/test_InfluxDBClient.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import threading
55
import unittest
66

7+
import httpretty
78
import pytest
89
from urllib3.exceptions import NewConnectionError, HTTPError
910

@@ -244,6 +245,24 @@ def do_GET(self):
244245
self.httpd_thread.start()
245246

246247

248+
class InfluxDBClientTestMock(unittest.TestCase):
249+
250+
def setUp(self) -> None:
251+
httpretty.enable()
252+
httpretty.reset()
253+
254+
def tearDown(self) -> None:
255+
if self.influxdb_client:
256+
self.influxdb_client.close()
257+
httpretty.disable()
258+
259+
def test_init_without_token(self):
260+
httpretty.register_uri(httpretty.POST, uri="http://localhost/api/v2/query", status=200, body="")
261+
self.influxdb_client = InfluxDBClient("http://localhost")
262+
self.assertIsNotNone(self.influxdb_client)
263+
self.influxdb_client.query_api().query("buckets()", "my-org")
264+
265+
247266
class ServerWithSelfSingedSSL(http.server.SimpleHTTPRequestHandler):
248267
def _set_headers(self):
249268
self.send_response(200)

tests/test_InfluxDBClientAsync.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from datetime import datetime
55

66
import pytest
7+
from aioresponses import aioresponses
78

89
from influxdb_client import Point, WritePrecision
910
from influxdb_client.client.exceptions import InfluxDBError
@@ -238,6 +239,14 @@ async def test_username_password_authorization(self):
238239
self.client = InfluxDBClientAsync(url="http://localhost:8086", username="my-user", password="my-password", debug=True)
239240
await self.client.query_api().query("buckets()", "my-org")
240241

242+
@async_test
243+
@aioresponses()
244+
async def test_init_without_token(self, mocked):
245+
mocked.post('http://localhost/api/v2/query?org=my-org', status=200, body='')
246+
await self.client.close()
247+
self.client = InfluxDBClientAsync("http://localhost")
248+
await self.client.query_api().query("buckets()", "my-org")
249+
241250
async def _prepare_data(self, measurement: str):
242251
_point1 = Point(measurement).tag("location", "Prague").field("temperature", 25.3)
243252
_point2 = Point(measurement).tag("location", "New York").field("temperature", 24.3)

0 commit comments

Comments
 (0)