From 83a0f326f435d66fa7661161546e39284409efb0 Mon Sep 17 00:00:00 2001 From: art0007i Date: Wed, 23 Aug 2023 08:41:08 +0200 Subject: [PATCH 1/2] use post request and correct content type --- src/api_client/mod.rs | 2 +- src/query/users.rs | 4 ++++ tests/common/mod.rs | 2 +- tests/login.rs | 28 ++++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 tests/login.rs diff --git a/src/api_client/mod.rs b/src/api_client/mod.rs index 5e0e00a..c2807f9 100644 --- a/src/api_client/mod.rs +++ b/src/api_client/mod.rs @@ -173,7 +173,7 @@ impl racal::reqwest::ApiClient for UnauthenticatedCVR { &self, req: RequestBuilder, ) -> Result { self.http_rate_limiter.until_ready().await; - Ok(req) + Ok(req.header("Content-Type", "application/json")) } } diff --git a/src/query/users.rs b/src/query/users.rs index da7f7f9..a0c8538 100644 --- a/src/query/users.rs +++ b/src/query/users.rs @@ -115,6 +115,10 @@ impl Queryable> for AuthType { fn body(&self, _: &NoAuthentication) -> Option>> { Some(serde_json::to_vec(self)) } + + fn method(&self, _state: &NoAuthentication) -> racal::RequestMethod { + racal::RequestMethod::Post + } } /// Adds an user to the blocked users list diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 77e5a2c..d5d89e2 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -8,7 +8,7 @@ use chilloutvr::{ }; use once_cell::sync::Lazy; -const USER_AGENT: &str = concat!( +pub const USER_AGENT: &str = concat!( env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"), " (", diff --git a/tests/login.rs b/tests/login.rs new file mode 100644 index 0000000..7f8cb2b --- /dev/null +++ b/tests/login.rs @@ -0,0 +1,28 @@ +#![cfg(feature = "http_client")] + +use chilloutvr::{ + api_client::{ApiClient, ApiError, ApiConfiguration}, + query::{LoginCredentials, AuthType}, +}; + +mod common; + +#[tokio::test] +#[ignore] +async fn login() -> Result<(), ApiError> { + let config = ApiConfiguration::new(common::USER_AGENT.to_owned()); + let client = chilloutvr::api_client::UnauthenticatedCVR::new(config).expect("Failed to create unauthenticated client."); + + let query = AuthType::LoginProfile(LoginCredentials { + email: "email@Address".to_owned(), + password: "pa$$word".to_owned(), + }); + + let results = client.query(query).await?.data; + + dbg!(&results); + + assert!(!results.access_key.is_empty()); + + Ok(()) +} \ No newline at end of file From ade654be591aa3782e69d159a7d9230d31d66bc7 Mon Sep 17 00:00:00 2001 From: art0007i Date: Mon, 28 Aug 2023 00:34:07 +0200 Subject: [PATCH 2/2] undo janky header hack and redo login test --- .gitignore | 1 + src/api_client/mod.rs | 2 +- tests/common/mod.rs | 11 +++++++++-- tests/login.rs | 30 +++++++++++++++++++++--------- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index bc9820c..e834bba 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ Cargo.lock # Auth user-auth.json +user-credentials.json diff --git a/src/api_client/mod.rs b/src/api_client/mod.rs index c2807f9..5e0e00a 100644 --- a/src/api_client/mod.rs +++ b/src/api_client/mod.rs @@ -173,7 +173,7 @@ impl racal::reqwest::ApiClient for UnauthenticatedCVR { &self, req: RequestBuilder, ) -> Result { self.http_rate_limiter.until_ready().await; - Ok(req.header("Content-Type", "application/json")) + Ok(req) } } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index d5d89e2..d6b6f00 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -2,13 +2,13 @@ // Something's funky with checking if these are used or not. #![allow(dead_code)] use chilloutvr::{ - api_client::{ApiConfiguration, AuthenticatedCVR}, + api_client::{ApiConfiguration, AuthenticatedCVR, UnauthenticatedCVR}, model::{ResponseDataWrapper, UserAuth}, query::SavedLoginCredentials, }; use once_cell::sync::Lazy; -pub const USER_AGENT: &str = concat!( +const USER_AGENT: &str = concat!( env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"), " (", @@ -39,3 +39,10 @@ pub fn api_client() -> AuthenticatedCVR { ) .unwrap() } + +pub fn unauthenticated_api_client() -> UnauthenticatedCVR { + UnauthenticatedCVR::new( + ApiConfiguration::new(USER_AGENT.to_owned()) + ) + .unwrap() +} diff --git a/tests/login.rs b/tests/login.rs index 7f8cb2b..c9fbd26 100644 --- a/tests/login.rs +++ b/tests/login.rs @@ -1,8 +1,8 @@ #![cfg(feature = "http_client")] use chilloutvr::{ - api_client::{ApiClient, ApiError, ApiConfiguration}, - query::{LoginCredentials, AuthType}, + api_client::{ApiClient, ApiError}, + query::AuthType, }; mod common; @@ -10,15 +10,27 @@ mod common; #[tokio::test] #[ignore] async fn login() -> Result<(), ApiError> { - let config = ApiConfiguration::new(common::USER_AGENT.to_owned()); - let client = chilloutvr::api_client::UnauthenticatedCVR::new(config).expect("Failed to create unauthenticated client."); + let client = common::unauthenticated_api_client(); - let query = AuthType::LoginProfile(LoginCredentials { - email: "email@Address".to_owned(), - password: "pa$$word".to_owned(), - }); +/* +example credentials file: - let results = client.query(query).await?.data; +{ + "auth_type": "loginProfile", + "username": "example@example.com", + "password": "hunter2" +} + +*/ + + let credentials = serde_json::from_slice::( + &std::fs::read("user-credentials.json").expect( + "must have a prepared `user-credentials.json` file for live API testing", + ), + ) + .expect("`user-credentials.json` file to parse into auth type"); + + let results = client.query(credentials).await?.data; dbg!(&results);