From 065c2015d26811ba88c0f4f4b6d49bc93fe0e072 Mon Sep 17 00:00:00 2001 From: ByteDream <63594396+ByteDream@users.noreply.github.com> Date: Fri, 8 Sep 2023 12:15:27 +0200 Subject: [PATCH] Fix json_query not sending token in request (#128) --- .../src/integrations/serde_integration/mod.rs | 6 +- influxdb/tests/integration_tests.rs | 84 +++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/influxdb/src/integrations/serde_integration/mod.rs b/influxdb/src/integrations/serde_integration/mod.rs index 7c9681c..974853e 100644 --- a/influxdb/src/integrations/serde_integration/mod.rs +++ b/influxdb/src/integrations/serde_integration/mod.rs @@ -140,7 +140,11 @@ impl Client { let url = &format!("{}/query", &self.url); let mut parameters = self.parameters.as_ref().clone(); parameters.insert("q", read_query); - let request_builder = self.client.get(url).query(¶meters); + let mut request_builder = self.client.get(url); + if let Some(ref token) = self.token { + request_builder = request_builder.header("Authorization", format!("Token {}", token)) + } + let request_builder = request_builder.query(¶meters); #[cfg(feature = "surf")] let request_builder = request_builder.map_err(|err| Error::UrlConstructionError { diff --git a/influxdb/tests/integration_tests.rs b/influxdb/tests/integration_tests.rs index be75b06..e3750cc 100644 --- a/influxdb/tests/integration_tests.rs +++ b/influxdb/tests/integration_tests.rs @@ -2,6 +2,8 @@ extern crate influxdb; #[path = "./utilities.rs"] mod utilities; + +use serde::Deserialize; use utilities::{ assert_result_err, assert_result_ok, create_client, create_db, delete_db, run_test, }; @@ -271,6 +273,88 @@ async fn test_write_and_read_field() { .await; } +/// INTEGRATION TEST +/// +/// This test case tests the authentication on json reads +#[async_std::test] +#[cfg(feature = "use-serde")] +#[cfg(not(tarpaulin_include))] +async fn test_json_non_authed_read() { + const TEST_NAME: &str = "test_json_non_authed_read"; + + run_test( + || async move { + let client = + Client::new("http://127.0.0.1:9086", TEST_NAME).with_auth("admin", "password"); + let query = format!("CREATE DATABASE {}", TEST_NAME); + client + .query(ReadQuery::new(query)) + .await + .expect("could not setup db"); + let non_authed_client = Client::new("http://127.0.0.1:9086", TEST_NAME); + + let read_query = ReadQuery::new("SELECT * FROM weather"); + let read_result = non_authed_client.json_query(read_query).await; + assert_result_err(&read_result); + match read_result { + Err(Error::AuthorizationError) => {} + _ => panic!( + "Should be a AuthorizationError: {}", + read_result.unwrap_err() + ), + } + }, + || async move { + let client = + Client::new("http://127.0.0.1:9086", TEST_NAME).with_auth("admin", "password"); + let query = format!("DROP DATABASE {}", TEST_NAME); + + client + .query(ReadQuery::new(query)) + .await + .expect("could not clean up db"); + }, + ) + .await +} + +/// INTEGRATION TEST +/// +/// This test case tests the authentication on json reads +#[async_std::test] +#[cfg(feature = "use-serde")] +#[cfg(not(tarpaulin_include))] +async fn test_json_authed_read() { + const TEST_NAME: &str = "test_json_authed_read"; + + run_test( + || async move { + let client = + Client::new("http://127.0.0.1:9086", TEST_NAME).with_auth("admin", "password"); + let query = format!("CREATE DATABASE {}", TEST_NAME); + client + .query(ReadQuery::new(query)) + .await + .expect("could not setup db"); + + let read_query = ReadQuery::new("SELECT * FROM weather"); + let read_result = client.json_query(read_query).await; + assert_result_ok(&read_result); + }, + || async move { + let client = + Client::new("http://127.0.0.1:9086", TEST_NAME).with_auth("admin", "password"); + let query = format!("DROP DATABASE {}", TEST_NAME); + + client + .query(ReadQuery::new(query)) + .await + .expect("could not clean up db"); + }, + ) + .await +} + /// INTEGRATION TEST /// /// This integration tests that writing data and retrieving the data again is working