1
0
Fork 0
mirror of https://codeberg.org/tyy/aspm synced 2024-12-24 08:49:28 -07:00
aspm/crates/aspm/tests/cli.rs
2024-07-08 17:45:26 -04:00

138 lines
No EOL
3.8 KiB
Rust

use anyhow::Context;
use assert_cmd::prelude::*;
use predicates::prelude::*;
use tempfile::TempDir;
use std::process::Command;
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> {
Command::cargo_bin("aspm")?
.env("ASPM_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(())
}
#[test]
fn help_works() -> Result<(), anyhow::Error> {
let tempdir = TempDir::new()?;
let datadir = tempdir.path().to_str().context("Tempdir path was not valid utf8")?;
Command::cargo_bin("aspm")?
.env("ASPM_DATA_DIR", datadir)
.arg("--help")
.assert()
.success()
.stdout(predicate::str::starts_with(env!("CARGO_PKG_DESCRIPTION")));
Command::cargo_bin("aspm")?
.env("ASPM_DATA_DIR", datadir)
.arg("-h")
.assert()
.success()
.stdout(predicate::str::starts_with(env!("CARGO_PKG_DESCRIPTION")));
Command::cargo_bin("aspm")?
.env("ASPM_DATA_DIR", datadir)
.arg("help")
.assert()
.success()
.stdout(predicate::str::starts_with(env!("CARGO_PKG_DESCRIPTION")));
Command::cargo_bin("aspm")?
.env("ASPM_DATA_DIR", datadir)
.assert()
.code(2)
.stderr(predicate::str::starts_with(env!("CARGO_PKG_DESCRIPTION")));
Ok(())
}
#[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")?;
assert_key_generated(datadir)
}
#[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)?;
Command::cargo_bin("aspm")?
.env("ASPM_DATA_DIR", datadir)
.arg("keys")
.arg("list")
.assert()
.success()
.stdout(predicate::str::contains(KEY_ALIAS));
Ok(())
}
// 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"] {
Command::cargo_bin("aspm")?
.env("ASPM_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_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)?;
Command::cargo_bin("aspm")?
.env("ASPM_DATA_DIR", datadir)
.arg("keys")
.arg("delete")
.arg("--no-confirm")
.arg(KEY_ALIAS)
.assert()
.success()
.stdout(predicate::str::contains("Successfully deleted key with fingerprint "));
Command::cargo_bin("aspm")?
.env("ASPM_DATA_DIR", datadir)
.arg("keys")
.arg("list")
.assert()
.success()
.stdout(predicate::str::contains("Saved keys (0 total):"));
Ok(())
}