Skip to content

Commit

Permalink
Make test code as a workspace.
Browse files Browse the repository at this point in the history
  • Loading branch information
bloodnighttw committed Jul 12, 2024
1 parent 01ab322 commit c6b709b
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 128 deletions.
15 changes: 14 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members=[
"reginleif",
"reginleif-test",
"reginleif-utils",
"reginleif-macro"
]
Expand Down
1 change: 1 addition & 0 deletions reginleif-test/src/reginleif.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod auth;
75 changes: 75 additions & 0 deletions reginleif-test/src/reginleif/auth.rs
Original file line number Diff line number Diff line change
@@ -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> = 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);


}

}
1 change: 1 addition & 0 deletions reginleif-test/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod expiring_data;
51 changes: 51 additions & 0 deletions reginleif-test/src/utils/expiring_data.rs
Original file line number Diff line number Diff line change
@@ -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();
}

}
51 changes: 0 additions & 51 deletions reginleif-utils/src/expiring_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,55 +155,4 @@ impl<T> From<T> for ExpiringData<T> 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();
}

}

2 changes: 1 addition & 1 deletion reginleif/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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."
Expand Down
75 changes: 0 additions & 75 deletions reginleif/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> = 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);


}

}

0 comments on commit c6b709b

Please sign in to comment.