Tests for user management CLI

Signed-off-by: Olivier 'reivilibre <olivier@librepush.net>
This commit is contained in:
Olivier 'reivilibre' 2024-07-06 16:21:04 +01:00
parent 67e7d6bf5f
commit 6cd72b4177
2 changed files with 173 additions and 2 deletions

View File

@ -15,12 +15,15 @@ use crate::{
struct TestSystem {
database: PgTempDB,
web: Router,
config: Arc<Configuration>,
store: Arc<IdCoopStore>,
}
const RSA_KEY_PAIR_PEM: &[u8] = include_bytes!("tests/keypair.pem");
const RSA_PUBLIC_KEY_PEM: &[u8] = include_bytes!("tests/publickey.crt");
// #[rstest::fixture]
mod test_cli;
async fn basic_system() -> TestSystem {
let temp_db = pgtemp::PgTempDBBuilder::new()
.with_dbname("test_idcoop")
@ -75,13 +78,17 @@ async fn basic_system() -> TestSystem {
.expect("failed to decode builtin RSA keypair"),
};
let router = make_router(Arc::new(store), Arc::new(config), Arc::new(secrets))
let config = Arc::new(config);
let store = Arc::new(store);
let router = make_router(store.clone(), config.clone(), Arc::new(secrets))
.await
.expect("failed to make router");
TestSystem {
database: temp_db,
web: router,
config,
store,
}
}

164
src/tests/test_cli.rs Normal file
View File

@ -0,0 +1,164 @@
use rstest::rstest;
use crate::cli::{handle_user_command, UserCommand};
use super::basic_system;
#[rstest]
#[tokio::test]
async fn test_cli_add_user() {
let sys = basic_system().await;
handle_user_command(
UserCommand::Add {
username: "jonathan".to_owned(),
locked: true,
},
&sys.config,
&sys.store,
)
.await
.unwrap();
let _: () = sys
.store
.txn(|mut txn| {
Box::pin(async move {
let user = txn.lookup_user_by_name("jonathan".to_owned()).await?;
assert!(user.unwrap().locked);
Ok(())
})
})
.await
.unwrap();
}
#[rstest]
#[tokio::test]
async fn test_cli_lock_and_unlock_user() {
let sys = basic_system().await;
handle_user_command(
UserCommand::Add {
username: "jonathan".to_owned(),
locked: false,
},
&sys.config,
&sys.store,
)
.await
.unwrap();
let _: () = sys
.store
.txn(|mut txn| {
Box::pin(async move {
let user = txn.lookup_user_by_name("jonathan".to_owned()).await?;
assert!(!user.unwrap().locked);
Ok(())
})
})
.await
.unwrap();
handle_user_command(
UserCommand::Lock {
username: "jonathan".to_owned(),
},
&sys.config,
&sys.store,
)
.await
.unwrap();
let _: () = sys
.store
.txn(|mut txn| {
Box::pin(async move {
let user = txn.lookup_user_by_name("jonathan".to_owned()).await?;
assert!(user.unwrap().locked);
Ok(())
})
})
.await
.unwrap();
handle_user_command(
UserCommand::Unlock {
username: "jonathan".to_owned(),
},
&sys.config,
&sys.store,
)
.await
.unwrap();
let _: () = sys
.store
.txn(|mut txn| {
Box::pin(async move {
let user = txn.lookup_user_by_name("jonathan".to_owned()).await?;
assert!(!user.unwrap().locked);
Ok(())
})
})
.await
.unwrap();
}
#[rstest]
#[tokio::test]
async fn test_cli_del_user() {
let sys = basic_system().await;
handle_user_command(
UserCommand::Add {
username: "jonathan".to_owned(),
locked: true,
},
&sys.config,
&sys.store,
)
.await
.unwrap();
let _: () = sys
.store
.txn(|mut txn| {
Box::pin(async move {
let user = txn.lookup_user_by_name("jonathan".to_owned()).await?;
assert!(user.unwrap().locked);
Ok(())
})
})
.await
.unwrap();
handle_user_command(
UserCommand::Delete {
username: "jonathan".to_owned(),
},
&sys.config,
&sys.store,
)
.await
.unwrap();
let _: () = sys
.store
.txn(|mut txn| {
Box::pin(async move {
let user = txn.lookup_user_by_name("jonathan".to_owned()).await?;
assert!(user.is_none());
Ok(())
})
})
.await
.unwrap();
}