From a68c67f0145e9481e96fb1b0347be22d2ab7c405 Mon Sep 17 00:00:00 2001 From: Aurelia Date: Fri, 6 Dec 2024 16:54:46 +0100 Subject: [PATCH] CLI shared recovery create - add test for interactive threshold --- .cspell/custom-words.txt | 1 + Cargo.lock | 35 +++++++++++++++++-- Cargo.toml | 1 + cli/Cargo.toml | 1 + .../integration/shared_recovery/create.rs | 33 +++++++++++++++-- 5 files changed, 67 insertions(+), 4 deletions(-) diff --git a/.cspell/custom-words.txt b/.cspell/custom-words.txt index 261e5a58914..34c60ee9bd3 100644 --- a/.cspell/custom-words.txt +++ b/.cspell/custom-words.txt @@ -562,6 +562,7 @@ Reqwest rerunfailures reseted retrier +rexpect Richcmp Rlib rmvb diff --git a/Cargo.lock b/Cargo.lock index 3ff340e48c8..6b6829a1a7b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -507,6 +507,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "comma" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55b672471b4e9f9e95499ea597ff64941a309b2cdbffcc46f2cc5e2d971fd335" + [[package]] name = "concurrent-queue" version = "2.4.0" @@ -758,7 +764,7 @@ version = "3.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90eeab0aa92f3f9b4e87f258c72b139c207d251f9cbc1080a0086b86a8870dd3" dependencies = [ - "nix", + "nix 0.29.0", "windows-sys 0.59.0", ] @@ -1224,7 +1230,7 @@ dependencies = [ "libc", "log", "memchr", - "nix", + "nix 0.29.0", "page_size", "pkg-config", "smallvec", @@ -2611,6 +2617,17 @@ dependencies = [ "syn-mid", ] +[[package]] +name = "nix" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053" +dependencies = [ + "bitflags 2.6.0", + "cfg-if 1.0.0", + "libc", +] + [[package]] name = "nix" version = "0.29.0" @@ -2902,6 +2919,7 @@ dependencies = [ "log", "predicates", "reqwest", + "rexpect", "rpassword", "rstest", "serde", @@ -3466,6 +3484,19 @@ dependencies = [ "thiserror", ] +[[package]] +name = "rexpect" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c020234fb542618dc3e3d43724e9d93f87e1db74040a76a8c4e830220fb9b20d" +dependencies = [ + "comma", + "nix 0.27.1", + "regex", + "tempfile", + "thiserror", +] + [[package]] name = "rmp" version = "0.8.14" diff --git a/Cargo.toml b/Cargo.toml index 61ec6dc86a1..1e1d3dc1975 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -173,6 +173,7 @@ regex = { version = "1.11.1", default-features = false } regex-syntax = { version = "0.8.4", default-features = false } reqwest = { version = "0.12.9", default-features = false } reqwest-eventsource = { version = "0.6.0", default-features = false } +rexpect = "0.6.0" rmp-serde = { version = "1.3.0", default-features = false } rpassword = { version = "7.3.1", default-features = false } rsa = { version = "0.8.2", default-features = false } diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 90fd994eae2..2297c340f67 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -46,6 +46,7 @@ libparsec = { workspace = true, features = ["cli-tests"] } assert_cmd = { workspace = true } predicates = { workspace = true, features = ["regex"] } +rexpect = { workspace = true } rstest = { workspace = true } uuid = { workspace = true, features = ["v6", "std", "rng"] } diff --git a/cli/tests/integration/shared_recovery/create.rs b/cli/tests/integration/shared_recovery/create.rs index 9168b358c81..6d4fcc67e66 100644 --- a/cli/tests/integration/shared_recovery/create.rs +++ b/cli/tests/integration/shared_recovery/create.rs @@ -1,8 +1,8 @@ use libparsec::{tmp_path, TmpPath}; -use crate::testenv_utils::{TestOrganization, DEFAULT_DEVICE_PASSWORD}; - use crate::integration_tests::bootstrap_cli_test; +use crate::testenv_utils::{TestOrganization, DEFAULT_DEVICE_PASSWORD}; +use rexpect::session::spawn; #[rstest::rstest] #[tokio::test] @@ -84,3 +84,32 @@ async fn create_shared_recovery_inexistent_email(tmp_path: TmpPath) { ) .stderr(predicates::str::contains("A user is missing")); } + +#[rstest::rstest] +#[tokio::test] +async fn create_shared_recovery_default(tmp_path: TmpPath) { + let (_, TestOrganization { bob, .. }, _) = bootstrap_cli_test(&tmp_path).await.unwrap(); + let mut cmd = assert_cmd::Command::cargo_bin("parsec-cli").unwrap(); + cmd.args([ + "shared-recovery", + "create", + "--device", + &bob.device_id.hex(), + ]); + + let program = cmd.get_program().to_str().unwrap().to_string(); + let program = cmd + .get_args() + .fold(program, |acc, s| format!("{acc} {s:?}")); + + let mut p = spawn(&dbg!(program), Some(1000)).unwrap(); + + p.exp_string("Enter password for the device:").unwrap(); + p.send_line(DEFAULT_DEVICE_PASSWORD).unwrap(); + + 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_regex(".*Shared recovery setup has been created.*") + .unwrap(); + p.exp_eof().unwrap(); +}