1
0
Fork 0
mirror of https://codeberg.org/tyy/aspm synced 2024-12-23 15:19:28 -07:00
aspm/crates/naja-cli/tests/cli.rs

214 lines
4.6 KiB
Rust
Raw Normal View History

mod consts;
use anyhow::Context;
use assert_cmd::prelude::*;
use consts::*;
use predicates::prelude::*;
use std::process::Command;
use tempfile::TempDir;
2024-07-08 15:45:26 -06:00
static KEY_ALIAS: &str = "TESTKEY";
static KEY_PASSWORD: &str = "TESTKEYPASSWORD";
// TODO: Also test ed25519 key generation
fn assert_key_generated(datadir: &str) -> Result<(), anyhow::Error> {
2024-07-11 20:38:57 -06:00
Command::cargo_bin("naja")?
.env("NAJA_DATA_DIR", datadir)
.arg("keys")
.arg("generate")
.env("KEY_PASSWORD", KEY_PASSWORD)
.arg("es256")
.arg("--key-alias")
.arg(KEY_ALIAS)
.assert()
.success()
.stdout(predicate::str::starts_with(
"Successfully generated a new key!",
));
Ok(())
2024-07-08 15:45:26 -06:00
}
#[test]
2024-07-08 15:45:26 -06:00
fn help_works() -> Result<(), anyhow::Error> {
let tempdir = TempDir::new()?;
let datadir = tempdir
.path()
.to_str()
.context("Tempdir path was not valid utf8")?;
2024-07-11 20:38:57 -06:00
Command::cargo_bin("naja")?
.env("NAJA_DATA_DIR", datadir)
.arg("--help")
.assert()
.success()
.stdout(predicate::str::starts_with(env!("CARGO_PKG_DESCRIPTION")));
2024-07-11 20:38:57 -06:00
Command::cargo_bin("naja")?
.env("NAJA_DATA_DIR", datadir)
.arg("-h")
.assert()
.success()
.stdout(predicate::str::starts_with(env!("CARGO_PKG_DESCRIPTION")));
2024-07-11 20:38:57 -06:00
Command::cargo_bin("naja")?
.env("NAJA_DATA_DIR", datadir)
.arg("help")
.assert()
.success()
.stdout(predicate::str::starts_with(env!("CARGO_PKG_DESCRIPTION")));
2024-07-11 20:38:57 -06:00
Command::cargo_bin("naja")?
.env("NAJA_DATA_DIR", datadir)
.assert()
.code(2)
.stderr(predicate::str::starts_with(env!("CARGO_PKG_DESCRIPTION")));
Ok(())
}
2024-07-08 15:45:26 -06:00
#[test]
fn keys_generate_works() -> Result<(), anyhow::Error> {
let tempdir = TempDir::new()?;
let datadir = tempdir
.path()
.to_str()
.context("Tempdir path was not valid utf8")?;
2024-07-08 15:45:26 -06:00
assert_key_generated(datadir)
2024-07-08 15:45:26 -06:00
}
#[test]
fn keys_list_works() -> Result<(), anyhow::Error> {
let tempdir = TempDir::new()?;
let datadir = tempdir
.path()
.to_str()
.context("Tempdir path was not valid utf8")?;
assert_key_generated(datadir)?;
2024-07-11 20:38:57 -06:00
Command::cargo_bin("naja")?
.env("NAJA_DATA_DIR", datadir)
.arg("keys")
.arg("list")
.assert()
.success()
.stdout(predicate::str::contains(KEY_ALIAS));
Ok(())
2024-07-08 15:45:26 -06:00
}
// This test takes a bit due to testing each export format individually, causing the password to be hashed multiple times
#[test]
fn keys_export_works() -> Result<(), anyhow::Error> {
let tempdir = TempDir::new()?;
let datadir = tempdir
.path()
.to_str()
.context("Tempdir path was not valid utf8")?;
assert_key_generated(datadir)?;
for export_format in ["pkcs8", "asp-tool", "jwk"] {
2024-07-11 20:38:57 -06:00
Command::cargo_bin("naja")?
.env("NAJA_DATA_DIR", datadir)
.arg("keys")
.arg("export")
.env("KEY_PASSWORD", KEY_PASSWORD)
.arg(export_format)
.arg(KEY_ALIAS)
.assert()
.success()
.stderr(predicate::str::contains("Exported key \""));
}
Ok(())
}
#[test]
fn keys_import_works() -> Result<(), anyhow::Error> {
let tempdir = TempDir::new()?;
let datadir = tempdir
.path()
.to_str()
.context("Tempdir path was not valid utf8")?;
for (export_format, key) in [
("pkcs8", TEST_KEY_PKCS8),
("asp-tool", TEST_KEY_ASPTOOL),
("jwk", TEST_KEY_JWK),
] {
2024-07-11 20:38:57 -06:00
Command::cargo_bin("naja")?
.env("NAJA_DATA_DIR", datadir)
.arg("keys")
.arg("import")
.env("KEY_PASSWORD", TEST_KEY_PASSWORD)
.arg("--key-alias")
.arg(KEY_ALIAS)
.arg(export_format)
.arg(key)
.assert()
.success()
.stdout(
predicate::str::contains("Successfully imported key!")
.and(predicate::str::contains(TEST_FINGERPRINT)),
);
2024-07-11 20:38:57 -06:00
Command::cargo_bin("naja")?
.env("NAJA_DATA_DIR", datadir)
.arg("keys")
.arg("list")
.assert()
.success()
.stdout(predicate::str::contains(TEST_FINGERPRINT));
2024-07-11 20:38:57 -06:00
Command::cargo_bin("naja")?
.env("NAJA_DATA_DIR", datadir)
.arg("keys")
.arg("delete")
.arg("--no-confirm")
.arg(TEST_FINGERPRINT)
.assert()
.success()
.stdout(predicate::str::contains(format!(
"Successfully deleted key with fingerprint {TEST_FINGERPRINT}"
)));
}
Ok(())
2024-07-08 15:45:26 -06:00
}
#[test]
fn keys_delete_works() -> Result<(), anyhow::Error> {
let tempdir = TempDir::new()?;
let datadir = tempdir
.path()
.to_str()
.context("Tempdir path was not valid utf8")?;
assert_key_generated(datadir)?;
2024-07-11 20:38:57 -06:00
Command::cargo_bin("naja")?
.env("NAJA_DATA_DIR", datadir)
.arg("keys")
.arg("delete")
.arg("--no-confirm")
.arg(KEY_ALIAS)
.assert()
.success()
.stdout(predicate::str::contains(
"Successfully deleted key with fingerprint ",
));
2024-07-11 20:38:57 -06:00
Command::cargo_bin("naja")?
.env("NAJA_DATA_DIR", datadir)
.arg("keys")
.arg("list")
.assert()
.success()
.stdout(predicate::str::contains("Saved keys (0 total):"));
Ok(())
}