diff --git a/Cargo.lock b/Cargo.lock index afa3ffc..5acd42c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -724,7 +724,7 @@ dependencies = [ [[package]] name = "reginleif" -version = "0.1.2" +version = "0.1.3" dependencies = [ "anyhow", "async-trait", @@ -746,6 +746,19 @@ dependencies = [ "syn", ] +[[package]] +name = "reginleif-test" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-trait", + "reginleif", + "reginleif-macro", + "reginleif-utils", + "reqwest", + "tokio", +] + [[package]] name = "reginleif-utils" version = "0.1.3" diff --git a/Cargo.toml b/Cargo.toml index ebe6223..4c7803e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members=[ "reginleif", + "reginleif-test", "reginleif-utils", "reginleif-macro" ] diff --git a/reginleif-test/src/reginleif.rs b/reginleif-test/src/reginleif.rs new file mode 100644 index 0000000..c209e9c --- /dev/null +++ b/reginleif-test/src/reginleif.rs @@ -0,0 +1 @@ +mod auth; \ No newline at end of file diff --git a/reginleif-test/src/reginleif/auth.rs b/reginleif-test/src/reginleif/auth.rs new file mode 100644 index 0000000..bc9b473 --- /dev/null +++ b/reginleif-test/src/reginleif/auth.rs @@ -0,0 +1,75 @@ + +#[cfg(test)] +mod test{ + use std::time::Duration; + use reginleif::auth::account::Account; + use reginleif::auth::microsoft::{DeviceCode, MicrosoftAuthError}; + use reginleif::auth::minecraft::{MinecraftAuth, Profile}; + use reginleif::auth::xbox::{XboxLiveToken, XboxSecurityToken}; + use reginleif_utils::expiring_data::ExpiringData; + + #[tokio::test] + #[ignore] + async fn test_auth_token(){ + + let client = reqwest::Client::new(); + let client_id = "47f3e635-2886-4628-a1c2-fd8a9f4d7a5f"; + let res = DeviceCode::fetch(&client,client_id).await; + + let device_code = match res{ + Ok(device_code) => { + println!("auth url: {}",device_code.verification_uri); + println!("user code: {}",device_code.user_code); + device_code + } + Err(e) => { + panic!("Error: {}",e); // error while fetching token. + } + }; + + + let mut res = loop{ + let result = device_code.exchange(&client,client_id).await; + let res = match result{ + Ok(res) => {res} + Err(e) => { + match e { + MicrosoftAuthError::AuthorizationPending => { + tokio::time::sleep(device_code.interval).await; + continue; + } + _=> {panic!("Error: {}",e);} + } + } + }; + break res; + }; + + println!("{:?}",res); + + let cloned = res.clone(); + + let xbox_live_token = XboxLiveToken::fetch(&client,&res.data.access_token).await.unwrap(); + println!("{:?}",xbox_live_token); + let xbox_security_token = XboxSecurityToken::fetch(&client,xbox_live_token).await.unwrap(); + println!("{:?}",xbox_security_token); + + let minecraft_auth = MinecraftAuth::fetch(&client,xbox_security_token).await.unwrap(); + println!("{:?}",minecraft_auth); + let profile = Profile::fetch(&client,&minecraft_auth).await.unwrap(); + println!("{:?}",profile); + + let account:Account = (minecraft_auth,profile,res.clone()).into(); + let mut account: ExpiringData = account.into(); + account.refresh(&client_id.to_string()).await.unwrap(); + println!("{:?}",account); + + tokio::time::sleep(Duration::from_secs(2)).await; + res.refresh(&(client,client_id.to_string())).await.unwrap(); + + assert_ne!(cloned.created_at,res.created_at); + + + } + +} \ No newline at end of file diff --git a/reginleif-test/src/utils.rs b/reginleif-test/src/utils.rs new file mode 100644 index 0000000..77aecda --- /dev/null +++ b/reginleif-test/src/utils.rs @@ -0,0 +1 @@ +mod expiring_data; \ No newline at end of file diff --git a/reginleif-test/src/utils/expiring_data.rs b/reginleif-test/src/utils/expiring_data.rs new file mode 100644 index 0000000..43bb6ae --- /dev/null +++ b/reginleif-test/src/utils/expiring_data.rs @@ -0,0 +1,51 @@ +#[cfg(test)] +mod test{ + use std::time::Duration; + use reginleif_macro::{Expirable, NoRefresh}; + use reginleif_utils::expiring_data::{ExpiringData, Refreshable}; + + #[derive(Expirable,NoRefresh,Default)] + struct TestStruct1{ + #[dur] duration: Duration + } + + #[derive(Expirable,Default)] + struct TestStruct2{ + #[dur] duration: Duration + } + + #[async_trait::async_trait] + impl Refreshable for TestStruct2{ + + type Args = (); + + async fn refresh(&mut self,_:&()) -> anyhow::Result<()> { + Ok(()) // do nothing in test + } + } + + #[tokio::test] + pub async fn test_expire(){ + let test:ExpiringData<_> = TestStruct1::default().into(); + tokio::time::sleep(Duration::from_secs(2)).await; + let temp = test.is_expired(); + let _test = test.get_ref(); + assert!(temp) + } + + #[tokio::test] + #[should_panic] + pub async fn test_no_refresh(){ + let mut test:ExpiringData<_> = TestStruct1::default().into(); + test.refresh(&()).await.expect("it's should be panic!"); + } + + #[tokio::test] + pub async fn test_expire2(){ + let mut test:ExpiringData<_> = TestStruct2::default().into(); + tokio::time::sleep(Duration::from_secs(2)).await; + test.refresh(&()).await.unwrap(); + test.try_ref(&()).await.unwrap(); + } + +} \ No newline at end of file diff --git a/reginleif-utils/src/expiring_data.rs b/reginleif-utils/src/expiring_data.rs index e661438..936a73b 100644 --- a/reginleif-utils/src/expiring_data.rs +++ b/reginleif-utils/src/expiring_data.rs @@ -155,55 +155,4 @@ impl From for ExpiringData where T:Expirable + Refreshable{ } } -#[cfg(test)] -mod test{ - use std::time::Duration; - use reginleif_macro::{Expirable, NoRefresh}; - use crate::expiring_data::{ExpiringData, Refreshable}; - - #[derive(Expirable,NoRefresh,Default)] - struct TestStruct1{ - #[dur] duration: Duration - } - - #[derive(Expirable,Default)] - struct TestStruct2{ - #[dur] duration: Duration - } - - #[async_trait::async_trait] - impl Refreshable for TestStruct2{ - - type Args = (); - - async fn refresh(&mut self,_:&()) -> anyhow::Result<()> { - Ok(()) // do nothing in test - } - } - - #[tokio::test] - pub async fn test_expire(){ - let test:ExpiringData<_> = TestStruct1::default().into(); - tokio::time::sleep(Duration::from_secs(2)).await; - let temp = test.is_expired(); - let _test = test.get_ref(); - assert!(temp) - } - - #[tokio::test] - #[should_panic] - pub async fn test_no_refresh(){ - let mut test:ExpiringData<_> = TestStruct1::default().into(); - test.refresh(&()).await.expect("it's should be panic!"); - } - - #[tokio::test] - pub async fn test_expire2(){ - let mut test:ExpiringData<_> = TestStruct2::default().into(); - tokio::time::sleep(Duration::from_secs(2)).await; - test.refresh(&()).await.unwrap(); - test.try_ref(&()).await.unwrap(); - } - -} diff --git a/reginleif/Cargo.toml b/reginleif/Cargo.toml index b08e09a..1816075 100644 --- a/reginleif/Cargo.toml +++ b/reginleif/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "reginleif" -version = "0.1.2" +version = "0.1.3" edition = "2021" license = "Apache-2.0" description = "The core library of nolauncher." diff --git a/reginleif/src/auth.rs b/reginleif/src/auth.rs index 4cea060..2d9f36c 100644 --- a/reginleif/src/auth.rs +++ b/reginleif/src/auth.rs @@ -7,78 +7,3 @@ pub mod minecraft; /// A wrapper for Account data. pub mod account; mod constant; - -#[cfg(test)] -mod test{ - use std::time::Duration; - use crate::auth::account::Account; - use crate::auth::microsoft::{DeviceCode, MicrosoftAuthError}; - use crate::auth::minecraft::{MinecraftAuth, Profile}; - use crate::auth::xbox::{XboxLiveToken, XboxSecurityToken}; - use reginleif_utils::expiring_data::ExpiringData; - - #[tokio::test] - #[ignore] - async fn test_auth_token(){ - - let client = reqwest::Client::new(); - let client_id = "47f3e635-2886-4628-a1c2-fd8a9f4d7a5f"; - let res = DeviceCode::fetch(&client,client_id).await; - - let device_code = match res{ - Ok(device_code) => { - println!("auth url: {}",device_code.verification_uri); - println!("user code: {}",device_code.user_code); - device_code - } - Err(e) => { - panic!("Error: {}",e); // error while fetching token. - } - }; - - - let mut res = loop{ - let result = device_code.exchange(&client,client_id).await; - let res = match result{ - Ok(res) => {res} - Err(e) => { - match e { - MicrosoftAuthError::AuthorizationPending => { - tokio::time::sleep(device_code.interval).await; - continue; - } - _=> {panic!("Error: {}",e);} - } - } - }; - break res; - }; - - println!("{:?}",res); - - let cloned = res.clone(); - - let xbox_live_token = XboxLiveToken::fetch(&client,&res.data.access_token).await.unwrap(); - println!("{:?}",xbox_live_token); - let xbox_security_token = XboxSecurityToken::fetch(&client,xbox_live_token).await.unwrap(); - println!("{:?}",xbox_security_token); - - let minecraft_auth = MinecraftAuth::fetch(&client,xbox_security_token).await.unwrap(); - println!("{:?}",minecraft_auth); - let profile = Profile::fetch(&client,&minecraft_auth).await.unwrap(); - println!("{:?}",profile); - - let account:Account = (minecraft_auth,profile,res.clone()).into(); - let mut account: ExpiringData = account.into(); - account.refresh(&client_id.to_string()).await.unwrap(); - println!("{:?}",account); - - tokio::time::sleep(Duration::from_secs(2)).await; - res.refresh(&(client,client_id.to_string())).await.unwrap(); - - assert_ne!(cloned.created_at,res.created_at); - - - } - -} \ No newline at end of file