Skip to content

Commit 065c201

Browse files
authored
Fix json_query not sending token in request (#128)
1 parent 86f1b59 commit 065c201

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

influxdb/src/integrations/serde_integration/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ impl Client {
140140
let url = &format!("{}/query", &self.url);
141141
let mut parameters = self.parameters.as_ref().clone();
142142
parameters.insert("q", read_query);
143-
let request_builder = self.client.get(url).query(&parameters);
143+
let mut request_builder = self.client.get(url);
144+
if let Some(ref token) = self.token {
145+
request_builder = request_builder.header("Authorization", format!("Token {}", token))
146+
}
147+
let request_builder = request_builder.query(&parameters);
144148

145149
#[cfg(feature = "surf")]
146150
let request_builder = request_builder.map_err(|err| Error::UrlConstructionError {

influxdb/tests/integration_tests.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ extern crate influxdb;
22

33
#[path = "./utilities.rs"]
44
mod utilities;
5+
6+
use serde::Deserialize;
57
use utilities::{
68
assert_result_err, assert_result_ok, create_client, create_db, delete_db, run_test,
79
};
@@ -271,6 +273,88 @@ async fn test_write_and_read_field() {
271273
.await;
272274
}
273275

276+
/// INTEGRATION TEST
277+
///
278+
/// This test case tests the authentication on json reads
279+
#[async_std::test]
280+
#[cfg(feature = "use-serde")]
281+
#[cfg(not(tarpaulin_include))]
282+
async fn test_json_non_authed_read() {
283+
const TEST_NAME: &str = "test_json_non_authed_read";
284+
285+
run_test(
286+
|| async move {
287+
let client =
288+
Client::new("http://127.0.0.1:9086", TEST_NAME).with_auth("admin", "password");
289+
let query = format!("CREATE DATABASE {}", TEST_NAME);
290+
client
291+
.query(ReadQuery::new(query))
292+
.await
293+
.expect("could not setup db");
294+
let non_authed_client = Client::new("http://127.0.0.1:9086", TEST_NAME);
295+
296+
let read_query = ReadQuery::new("SELECT * FROM weather");
297+
let read_result = non_authed_client.json_query(read_query).await;
298+
assert_result_err(&read_result);
299+
match read_result {
300+
Err(Error::AuthorizationError) => {}
301+
_ => panic!(
302+
"Should be a AuthorizationError: {}",
303+
read_result.unwrap_err()
304+
),
305+
}
306+
},
307+
|| async move {
308+
let client =
309+
Client::new("http://127.0.0.1:9086", TEST_NAME).with_auth("admin", "password");
310+
let query = format!("DROP DATABASE {}", TEST_NAME);
311+
312+
client
313+
.query(ReadQuery::new(query))
314+
.await
315+
.expect("could not clean up db");
316+
},
317+
)
318+
.await
319+
}
320+
321+
/// INTEGRATION TEST
322+
///
323+
/// This test case tests the authentication on json reads
324+
#[async_std::test]
325+
#[cfg(feature = "use-serde")]
326+
#[cfg(not(tarpaulin_include))]
327+
async fn test_json_authed_read() {
328+
const TEST_NAME: &str = "test_json_authed_read";
329+
330+
run_test(
331+
|| async move {
332+
let client =
333+
Client::new("http://127.0.0.1:9086", TEST_NAME).with_auth("admin", "password");
334+
let query = format!("CREATE DATABASE {}", TEST_NAME);
335+
client
336+
.query(ReadQuery::new(query))
337+
.await
338+
.expect("could not setup db");
339+
340+
let read_query = ReadQuery::new("SELECT * FROM weather");
341+
let read_result = client.json_query(read_query).await;
342+
assert_result_ok(&read_result);
343+
},
344+
|| async move {
345+
let client =
346+
Client::new("http://127.0.0.1:9086", TEST_NAME).with_auth("admin", "password");
347+
let query = format!("DROP DATABASE {}", TEST_NAME);
348+
349+
client
350+
.query(ReadQuery::new(query))
351+
.await
352+
.expect("could not clean up db");
353+
},
354+
)
355+
.await
356+
}
357+
274358
/// INTEGRATION TEST
275359
///
276360
/// This integration tests that writing data and retrieving the data again is working

0 commit comments

Comments
 (0)