Skip to content

Fix json_query not sending token in request #128

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 2 commits into from
Sep 8, 2023
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
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