-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
178 additions
and
166 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,22 @@ | ||
use tonic::async_trait; | ||
|
||
#[async_trait] | ||
pub trait Case<S: Send + Sync> { | ||
const NAME: &'static str; | ||
|
||
async fn run(&self, state: &mut S) -> Result<(), String>; | ||
#[derive(Debug,thiserror::Error)] | ||
pub enum Error{ | ||
#[error("unreachable")] | ||
Unreachable | ||
} | ||
|
||
#[async_trait] | ||
trait Runable<S: Send + Sync> { | ||
async fn run(&self, state: &mut S) -> Result<(), String>; | ||
fn name(&self) -> &'static str; | ||
} | ||
// pub struct StackedTestcase<O> where Self:Sized{ | ||
// next:Box<dyn Testcase<O>> | ||
// } | ||
|
||
#[async_trait] | ||
impl<S: Send + Sync, Rhs: Case<S> + Send + Sync + 'static> Runable<S> for Rhs { | ||
async fn run(&self, state: &mut S) -> Result<(), String> { | ||
<Self as Case<S>>::run(self, state).await | ||
} | ||
// impl<I, O> Testcase<I> for StackedTestcase<O> { | ||
|
||
fn name(&self) -> &'static str { | ||
<Self as Case<S>>::NAME | ||
} | ||
} | ||
// } | ||
// #[async_trait] | ||
// pub trait Testcase<I,O>{ | ||
// fn run_inner(&self,state:I)->Result<I,Error>{ | ||
|
||
#[derive(Default)] | ||
pub struct CaseRunner<S: Send + Sync + Default> { | ||
case: Vec<Box<dyn Runable<S> + Send + Sync + 'static>>, | ||
state: S, | ||
} | ||
|
||
impl<S: Send + Sync + Default> CaseRunner<S> { | ||
pub fn add_case<Rhs: Case<S> + Send + Sync + 'static>(&mut self, case: Rhs) { | ||
self.case.push(Box::new(case)); | ||
} | ||
pub async fn run(mut self, title: &'static str) -> S { | ||
log::info!("Start testsuit {}", title); | ||
for (i, case) in self.case.into_iter().enumerate() { | ||
log::info!("Running case {} {}", i, case.name()); | ||
if let Err(err) = case.run(&mut self.state).await { | ||
log::error!("test fail: {}", err); | ||
break; | ||
} | ||
} | ||
log::info!("End"); | ||
|
||
self.state | ||
} | ||
} | ||
// } | ||
// fn stack(self,state:I,next:Box<dyn Testcase<O>>)->Result<O,Error>where O:From<I>; | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,46 @@ | ||
use crate::{ | ||
case::Case, | ||
client::connection, | ||
constant, | ||
grpc::backend::{token_set_client::TokenSetClient, LoginRequest}, | ||
}; | ||
|
||
use super::State; | ||
|
||
pub struct AdminLogin; | ||
|
||
#[tonic::async_trait] | ||
impl Case<State> for AdminLogin { | ||
const NAME: &'static str = "login as admin@admin"; | ||
|
||
async fn run(&self, state: &mut State) -> Result<(), String> { | ||
let mut client = | ||
TokenSetClient::with_origin(connection(), constant::SERVER.try_into().unwrap()); | ||
|
||
let res = client | ||
.create(LoginRequest { | ||
username: constant::ADMIN.to_string(), | ||
password: constant::ADMIN_PWD.to_string(), | ||
expiry: None, | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
let res = res.into_inner(); | ||
|
||
assert!(res.permission.can_root); | ||
assert!(!res.permission.can_link); | ||
assert!(!res.permission.can_manage_announcement); | ||
assert!(!res.permission.can_manage_chat); | ||
assert!(!res.permission.can_manage_contest); | ||
assert!(!res.permission.can_manage_education); | ||
assert!(!res.permission.can_manage_problem); | ||
assert!(!res.permission.can_manage_submit); | ||
assert!(!res.permission.can_manage_user); | ||
assert!(!res.permission.can_publish); | ||
assert!(!res.permission.can_imgur); | ||
|
||
state.token = Some(res.token.signature); | ||
Ok(()) | ||
} | ||
} | ||
// use crate::{ | ||
// case::Case, | ||
// client::connection, | ||
// constant, | ||
// grpc::backend::{token_set_client::TokenSetClient, LoginRequest}, | ||
// }; | ||
|
||
// use super::State; | ||
|
||
// pub struct AdminLogin; | ||
|
||
// #[tonic::async_trait] | ||
// impl Case<State> for AdminLogin { | ||
// const NAME: &'static str = "login as admin@admin"; | ||
|
||
// async fn run(&self, state: &mut State) -> Result<(), String> { | ||
// let mut client = | ||
// TokenSetClient::with_origin(connection(), constant::SERVER.try_into().unwrap()); | ||
|
||
// let res = client | ||
// .create(LoginRequest { | ||
// username: constant::ADMIN.to_string(), | ||
// password: constant::ADMIN_PWD.to_string(), | ||
// expiry: None, | ||
// }) | ||
// .await | ||
// .unwrap(); | ||
|
||
// let res = res.into_inner(); | ||
|
||
// assert!(res.permission.can_root); | ||
// assert!(!res.permission.can_link); | ||
// assert!(!res.permission.can_manage_announcement); | ||
// assert!(!res.permission.can_manage_chat); | ||
// assert!(!res.permission.can_manage_contest); | ||
// assert!(!res.permission.can_manage_education); | ||
// assert!(!res.permission.can_manage_problem); | ||
// assert!(!res.permission.can_manage_submit); | ||
// assert!(!res.permission.can_manage_user); | ||
// assert!(!res.permission.can_publish); | ||
// assert!(!res.permission.can_imgur); | ||
|
||
// state.token = Some(res.token.signature); | ||
// Ok(()) | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
mod login; | ||
use crate::{case::CaseRunner, user::login::AdminLogin}; | ||
// mod login; | ||
// use crate::{case::CaseRunner, user::login::AdminLogin}; | ||
|
||
#[derive(Default)] | ||
pub struct State { | ||
token: Option<String>, | ||
} | ||
// #[derive(Default)] | ||
// pub struct State { | ||
// token: Option<String>, | ||
// } | ||
|
||
pub async fn run() -> State { | ||
let mut user_runner = CaseRunner::<State>::default(); | ||
// pub async fn run() -> State { | ||
// let mut user_runner = CaseRunner::<State>::default(); | ||
|
||
user_runner.add_case(AdminLogin); | ||
// user_runner.add_case(AdminLogin); | ||
|
||
user_runner.run("user").await | ||
} | ||
// user_runner.run("user").await | ||
// } |