From 2e481cbb5d519fa630f2342c038f85cb567fcc6e Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Wed, 24 Jan 2024 22:23:28 +0100 Subject: [PATCH] CLI: Test add/remove keys. --- crates/cli/src/lib.rs | 5 ++- crates/cli/tests/smoke/keys.rs | 63 +++++++++++++++++++++++++++++++ crates/cli/tests/smoke/mod.rs | 2 + crates/lib/src/integration/age.rs | 6 +++ 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 crates/cli/tests/smoke/keys.rs diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index 2c1ee88..e96e4d0 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -2,7 +2,8 @@ mod run; pub use run::run; mod error; -pub(crate) use error::{RopsCliError, UndeterminedFormatError, IN_PLACE_PANIC}; +pub use error::RopsCliError; +pub(crate) use error::{UndeterminedFormatError, IN_PLACE_PANIC}; mod args; pub(crate) use args::*; @@ -11,4 +12,4 @@ mod cli; pub(crate) use cli::Cli; mod cryptography_stack; -pub(crate) use cryptography_stack::{DefaultCipher, DefaultHasher}; +pub use cryptography_stack::{DefaultCipher, DefaultHasher}; diff --git a/crates/cli/tests/smoke/keys.rs b/crates/cli/tests/smoke/keys.rs new file mode 100644 index 0000000..95a1f45 --- /dev/null +++ b/crates/cli/tests/smoke/keys.rs @@ -0,0 +1,63 @@ +use std::path::Path; + +use tempfile::NamedTempFile; + +use super::*; + +#[test] +fn adds_keys() { + let encrypted_temp_file = encrypted_tempfile(); + add_age_key_command(encrypted_temp_file.path()); + let updated_rops_file = updated_rops_file(encrypted_temp_file.path()); + + assert_eq!(2, updated_rops_file.metadata().intregation.age.len()) +} + +#[test] +fn removes_keys() { + let encrypted_temp_file = encrypted_tempfile(); + add_age_key_command(encrypted_temp_file.path()); + assert_eq!(2, updated_rops_file(encrypted_temp_file.path()).metadata().intregation.age.len()); + + remove_age_key_command(encrypted_temp_file.path()); + assert_eq!(1, updated_rops_file(encrypted_temp_file.path()).metadata().intregation.age.len()); +} + +fn add_age_key_command(file_path: &Path) { + let mut cmd = base(); + cmd.arg("add"); + finish(cmd, file_path) +} + +fn remove_age_key_command(file_path: &Path) { + let mut cmd = base(); + cmd.arg("remove"); + finish(cmd, file_path) +} + +fn base() -> Command { + AgeIntegration::set_mock_private_key_env_var(); + let mut cmd = Command::package_command(); + cmd.arg("keys"); + cmd +} + +fn finish(mut cmd: Command, file_path: &Path) { + cmd.args(["--age", &::KeyId::mock_other().to_string()]); + let mut cmd = cmd.format_args(); + cmd.arg(file_path); + cmd.run_tty().assert_success(); +} + +fn encrypted_tempfile() -> NamedTempFile { + let temp_file = NamedTempFile::new().unwrap(); + std::fs::write(temp_file.path(), sops_yaml_str!("age_example")).unwrap(); + temp_file +} + +fn updated_rops_file(updated_rops_file_path: &Path) -> RopsFile, YamlFileFormat> { + std::fs::read_to_string(updated_rops_file_path) + .unwrap() + .parse::, YamlFileFormat>>() + .unwrap() +} diff --git a/crates/cli/tests/smoke/mod.rs b/crates/cli/tests/smoke/mod.rs index b9e486f..1135703 100644 --- a/crates/cli/tests/smoke/mod.rs +++ b/crates/cli/tests/smoke/mod.rs @@ -11,6 +11,8 @@ mod decryption; mod editing; +mod keys; + test_binary::build_test_binary_once!(mock_editor, "test_bins"); mod command_utils; diff --git a/crates/lib/src/integration/age.rs b/crates/lib/src/integration/age.rs index 70bd375..b523941 100644 --- a/crates/lib/src/integration/age.rs +++ b/crates/lib/src/integration/age.rs @@ -122,6 +122,12 @@ mod key_id { Self::mock_display().parse().unwrap() } } + + impl MockOtherTestUtil for age::x25519::Recipient { + fn mock_other() -> Self { + "age1qazf43xll4ramx3wcn7h2yl9scycxdhrwge8862vv6zj97pafdvq0d5mn6".parse().unwrap() + } + } } }