@@ -1427,6 +1427,77 @@ def test_chunked_response(self):
14271427 'values' : [['qps' ], ['uptime' ], ['df' ], ['mount' ]]
14281428 }]}).__repr__ ())
14291429
1430+ def test_auth_default (self ):
1431+ """Test auth with default settings."""
1432+ with requests_mock .Mocker () as m :
1433+ m .register_uri (
1434+ requests_mock .GET ,
1435+ "http://localhost:8086/ping" ,
1436+ status_code = 204 ,
1437+ headers = {'X-Influxdb-Version' : '1.2.3' }
1438+ )
1439+
1440+ cli = InfluxDBClient ()
1441+ cli .ping ()
1442+
1443+ self .assertEqual (m .last_request .headers ["Authorization" ],
1444+ "Basic cm9vdDpyb290" )
1445+
1446+ def test_auth_username_password (self ):
1447+ """Test auth with custom username and password."""
1448+ with requests_mock .Mocker () as m :
1449+ m .register_uri (
1450+ requests_mock .GET ,
1451+ "http://localhost:8086/ping" ,
1452+ status_code = 204 ,
1453+ headers = {'X-Influxdb-Version' : '1.2.3' }
1454+ )
1455+
1456+ cli = InfluxDBClient (username = 'my-username' ,
1457+ password = 'my-password' )
1458+ cli .ping ()
1459+
1460+ self .assertEqual (m .last_request .headers ["Authorization" ],
1461+ "Basic bXktdXNlcm5hbWU6bXktcGFzc3dvcmQ=" )
1462+
1463+ def test_auth_username_password_none (self ):
1464+ """Test auth with not defined username or password."""
1465+ with requests_mock .Mocker () as m :
1466+ m .register_uri (
1467+ requests_mock .GET ,
1468+ "http://localhost:8086/ping" ,
1469+ status_code = 204 ,
1470+ headers = {'X-Influxdb-Version' : '1.2.3' }
1471+ )
1472+
1473+ cli = InfluxDBClient (username = None , password = None )
1474+ cli .ping ()
1475+ self .assertFalse ('Authorization' in m .last_request .headers )
1476+
1477+ cli = InfluxDBClient (username = None )
1478+ cli .ping ()
1479+ self .assertFalse ('Authorization' in m .last_request .headers )
1480+
1481+ cli = InfluxDBClient (password = None )
1482+ cli .ping ()
1483+ self .assertFalse ('Authorization' in m .last_request .headers )
1484+
1485+ def test_auth_token (self ):
1486+ """Test auth with custom authorization header."""
1487+ with requests_mock .Mocker () as m :
1488+ m .register_uri (
1489+ requests_mock .GET ,
1490+ "http://localhost:8086/ping" ,
1491+ status_code = 204 ,
1492+ headers = {'X-Influxdb-Version' : '1.2.3' }
1493+ )
1494+
1495+ cli = InfluxDBClient (username = None , password = None ,
1496+ headers = {"Authorization" : "my-token" })
1497+ cli .ping ()
1498+ self .assertEqual (m .last_request .headers ["Authorization" ],
1499+ "my-token" )
1500+
14301501
14311502class FakeClient (InfluxDBClient ):
14321503 """Set up a fake client instance of InfluxDBClient."""
0 commit comments