Skip to content

Commit

Permalink
Fix json_query not sending token in request (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
bytedream authored Sep 8, 2023
1 parent 86f1b59 commit 065c201
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
6 changes: 5 additions & 1 deletion influxdb/src/integrations/serde_integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&parameters);
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(&parameters);

#[cfg(feature = "surf")]
let request_builder = request_builder.map_err(|err| Error::UrlConstructionError {
Expand Down
84 changes: 84 additions & 0 deletions influxdb/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 065c201

Please sign in to comment.