Skip to content

Commit

Permalink
[CLI] Ask for confirmation before creating shared recovery.
Browse files Browse the repository at this point in the history
  • Loading branch information
AureliaDolo committed Jan 17, 2025
1 parent a75459d commit a748596
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
33 changes: 31 additions & 2 deletions cli/src/commands/shared_recovery/create.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::{collections::HashMap, num::NonZeroU8};

use dialoguer::Input;
use dialoguer::{Confirm, Input};
use itertools::Itertools;
use libparsec::{UserID, UserProfile};

use crate::utils::{
poll_server_for_new_certificates, start_spinner, StartedClient, GREEN_CHECKMARK,
poll_server_for_new_certificates, start_spinner, StartedClient, BULLET_CHAR, GREEN_CHECKMARK,
};

// TODO: should provide the recipients and their share count as a single parameter
Expand All @@ -25,6 +26,9 @@ crate::clap_parser_with_shared_opts_builder!(
/// Threshold number of shares required to proceed with recovery.
#[arg(short, long, requires = "recipients")]
threshold: Option<NonZeroU8>,
/// Whether to ask for confirmation or not
#[arg(long, default_value_t)]
no_confirmation: bool,
}
);

Expand All @@ -35,8 +39,10 @@ pub async fn create_shared_recovery(args: Args, client: &StartedClient) -> anyho
recipients,
weights,
threshold,
no_confirmation,
..
} = args;
dbg!(no_confirmation);

poll_server_for_new_certificates(client).await?;

Expand Down Expand Up @@ -93,6 +99,29 @@ pub async fn create_shared_recovery(args: Args, client: &StartedClient) -> anyho
per_recipient_shares.len()
)) .interact_text()?
};

println!(
"The following shared recovery setup will be created:\n{BULLET_CHAR} Threshold: {threshold}\n{}",
per_recipient_shares
.iter()
.map(|(recipient, share)| {
let user = &users
.iter()
.find(|x| x.id == *recipient)
.expect("missing recipient")
.human_handle;
format!("{BULLET_CHAR} User {user} will have {share} share(s)") // TODO: special case if there is only one share
})
.join("\n"));
if !no_confirmation
&& !Confirm::new()
.with_prompt("Do you want to proceed?")
.interact()?
{
println!("Shared recovery creation aborted.");
return Ok(());
}

let mut handle = start_spinner("Creating shared recovery setup".into());

client
Expand Down
6 changes: 4 additions & 2 deletions cli/tests/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ fn shared_recovery_create(
"1",
"1",
"--threshold",
"1"
"1",
"--no-confirmation"
)
.stdout(predicates::str::contains(
"Shared recovery setup has been created",
Expand All @@ -194,7 +195,8 @@ fn shared_recovery_create(
"--weights",
"1",
"--threshold",
"1"
"1",
"--no-confirmation"
)
.stdout(predicates::str::contains(
"Shared recovery setup has been created",
Expand Down
12 changes: 9 additions & 3 deletions cli/tests/integration/shared_recovery/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ async fn create_shared_recovery_ok(tmp_path: TmpPath) {
"1",
"1",
"--threshold",
"1"
"1",
"--no-confirmation"
)
.stdout(predicates::str::contains(
"Shared recovery setup has been created",
Expand Down Expand Up @@ -57,7 +58,8 @@ async fn create_shared_recovery_incoherent_weights(tmp_path: TmpPath) {
"--weights",
"1",
"--threshold",
"1"
"1",
"--no-confirmation"
)
.stderr(predicates::str::contains("incoherent weights count"));
}
Expand All @@ -79,7 +81,8 @@ async fn create_shared_recovery_inexistent_email(tmp_path: TmpPath) {
"--weights",
"1",
"--threshold",
"1"
"1",
"--no-confirmation"
)
.stderr(predicates::str::contains("A user is missing"));
}
Expand Down Expand Up @@ -116,6 +119,9 @@ async fn create_shared_recovery_default(tmp_path: TmpPath) {

p.exp_regex(".*The threshold is the minimum number of recipients that one must gather to recover the account.*").unwrap();
p.send_line("1").unwrap();
p.exp_string("The following shared recovery setup will be created")
.unwrap();
p.send_line("y").unwrap();
p.exp_regex(".*Shared recovery setup has been created.*")
.unwrap();
p.exp_eof().unwrap();
Expand Down
3 changes: 2 additions & 1 deletion cli/tests/integration/shared_recovery/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ async fn info_shared_recovery_ok(tmp_path: TmpPath) {
"1",
"1",
"--threshold",
"1"
"1",
"--no-confirmation"
)
.stdout(predicates::str::contains(
"Shared recovery setup has been created",
Expand Down

0 comments on commit a748596

Please sign in to comment.