Skip to content

fix: initialization of the client without auth credentials #443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 5 additions & 1 deletion influxdb_client/client/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
19 changes: 19 additions & 0 deletions tests/test_InfluxDBClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import threading
import unittest

import httpretty
import pytest
from urllib3.exceptions import NewConnectionError, HTTPError

Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_InfluxDBClientAsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down