Skip to content

Commit

Permalink
CLI: Test add/remove keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
gibbz00 committed Jan 24, 2024
1 parent a4c8e15 commit 2e481cb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
5 changes: 3 additions & 2 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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};
63 changes: 63 additions & 0 deletions crates/cli/tests/smoke/keys.rs
Original file line number Diff line number Diff line change
@@ -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", &<AgeIntegration as Integration>::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<EncryptedFile<DefaultCipher, DefaultHasher>, YamlFileFormat> {
std::fs::read_to_string(updated_rops_file_path)
.unwrap()
.parse::<RopsFile<EncryptedFile<DefaultCipher, DefaultHasher>, YamlFileFormat>>()
.unwrap()
}
2 changes: 2 additions & 0 deletions crates/cli/tests/smoke/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ mod decryption;

mod editing;

mod keys;

test_binary::build_test_binary_once!(mock_editor, "test_bins");

mod command_utils;
Expand Down
6 changes: 6 additions & 0 deletions crates/lib/src/integration/age.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
}

Expand Down

0 comments on commit 2e481cb

Please sign in to comment.