diff --git a/CHANGELOG.md b/CHANGELOG.md index dc3e2e8d..fd3d4be0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ ## 1.30.0 [unreleased] +## 1.29.1 [unreleased] + +### Bug Fixes +1. [#443](https://github.com/influxdata/influxdb-client-python/pull/443): Initialization of the client without auth credentials + ## 1.29.0 [2022-05-20] ### Breaking Changes diff --git a/influxdb_client/client/_base.py b/influxdb_client/client/_base.py index 600b4095..307f1d3e 100644 --- a/influxdb_client/client/_base.py +++ b/influxdb_client/client/_base.py @@ -72,13 +72,17 @@ def __init__(self, url, token, debug=None, timeout=10_000, enable_gzip=False, or self.conf.username = kwargs.get('username', None) self.conf.password = kwargs.get('password', None) + # defaults + self.auth_header_name = None + self.auth_header_value = None # by token - self.auth_header_name = "Authorization" if self.token: + self.auth_header_name = "Authorization" self.auth_header_value = "Token " + self.token # by HTTP basic auth_basic = kwargs.get('auth_basic', False) if auth_basic: + self.auth_header_name = "Authorization" self.auth_header_value = "Basic " + base64.b64encode(token.encode()).decode() # by username, password if self.conf.username and self.conf.password: diff --git a/setup.py b/setup.py index 59b28f51..93703cac 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,8 @@ 'randomize>=0.13', 'pytest>=5.0.0', 'httpretty==1.0.5', - 'psutil>=5.6.3' + 'psutil>=5.6.3', + 'aioresponses>=0.7.3' ] extra_requires = [ diff --git a/tests/test_InfluxDBClient.py b/tests/test_InfluxDBClient.py index b8bcd766..825decf3 100644 --- a/tests/test_InfluxDBClient.py +++ b/tests/test_InfluxDBClient.py @@ -4,6 +4,7 @@ import threading import unittest +import httpretty import pytest from urllib3.exceptions import NewConnectionError, HTTPError @@ -244,6 +245,24 @@ def do_GET(self): self.httpd_thread.start() +class InfluxDBClientTestMock(unittest.TestCase): + + def setUp(self) -> None: + httpretty.enable() + httpretty.reset() + + def tearDown(self) -> None: + if self.influxdb_client: + self.influxdb_client.close() + httpretty.disable() + + def test_init_without_token(self): + httpretty.register_uri(httpretty.POST, uri="http://localhost/api/v2/query", status=200, body="") + self.influxdb_client = InfluxDBClient("http://localhost") + self.assertIsNotNone(self.influxdb_client) + self.influxdb_client.query_api().query("buckets()", "my-org") + + class ServerWithSelfSingedSSL(http.server.SimpleHTTPRequestHandler): def _set_headers(self): self.send_response(200) diff --git a/tests/test_InfluxDBClientAsync.py b/tests/test_InfluxDBClientAsync.py index ac7c13bf..fb2ddfce 100644 --- a/tests/test_InfluxDBClientAsync.py +++ b/tests/test_InfluxDBClientAsync.py @@ -4,6 +4,7 @@ from datetime import datetime import pytest +from aioresponses import aioresponses from influxdb_client import Point, WritePrecision from influxdb_client.client.exceptions import InfluxDBError @@ -238,6 +239,14 @@ async def test_username_password_authorization(self): self.client = InfluxDBClientAsync(url="http://localhost:8086", username="my-user", password="my-password", debug=True) await self.client.query_api().query("buckets()", "my-org") + @async_test + @aioresponses() + async def test_init_without_token(self, mocked): + mocked.post('http://localhost/api/v2/query?org=my-org', status=200, body='') + await self.client.close() + self.client = InfluxDBClientAsync("http://localhost") + await self.client.query_api().query("buckets()", "my-org") + async def _prepare_data(self, measurement: str): _point1 = Point(measurement).tag("location", "Prague").field("temperature", 25.3) _point2 = Point(measurement).tag("location", "New York").field("temperature", 24.3)