use assert_cmd::Command;
use predicates::prelude::*;
use tempfile::TempDir;
fn test_cmd(temp_dir: &TempDir) -> Command {
let mut cmd = Command::cargo_bin("redisctl").unwrap();
let config_file = temp_dir.path().join("config.toml");
cmd.arg("--config-file").arg(config_file);
cmd
}
fn redisctl() -> Command {
Command::cargo_bin("redisctl").unwrap()
}
#[test]
fn test_profile_list() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("list")
.assert()
.success();
}
#[test]
fn test_profile_set_cloud() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("test-cloud")
.arg("--deployment")
.arg("cloud")
.arg("--api-key")
.arg("test-key")
.arg("--api-secret")
.arg("test-secret")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("list")
.assert()
.success()
.stdout(predicate::str::contains("test-cloud"));
}
#[test]
fn test_profile_set_enterprise() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("test-enterprise")
.arg("--deployment")
.arg("enterprise")
.arg("--url")
.arg("https://localhost:9443")
.arg("--username")
.arg("admin@redis.local")
.arg("--password")
.arg("password123")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("list")
.assert()
.success()
.stdout(predicate::str::contains("test-enterprise"));
}
#[test]
fn test_profile_get_nonexistent() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("show")
.arg("nonexistent")
.assert()
.failure();
}
#[test]
fn test_profile_get_existing() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("myprofile")
.arg("--deployment")
.arg("cloud")
.arg("--api-key")
.arg("key123")
.arg("--api-secret")
.arg("secret456")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("show")
.arg("myprofile")
.assert()
.success()
.stdout(predicate::str::contains("myprofile"));
}
#[test]
fn test_profile_delete() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("to-delete")
.arg("--deployment")
.arg("cloud")
.arg("--api-key")
.arg("key")
.arg("--api-secret")
.arg("secret")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("remove")
.arg("to-delete")
.write_stdin("y\n")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("list")
.assert()
.success()
.stdout(predicate::str::contains("to-delete").not());
}
#[test]
fn test_profile_delete_nonexistent() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("remove")
.arg("nonexistent")
.assert()
.failure();
}
#[test]
fn test_profile_set_default_subcommand() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("myprofile")
.arg("--deployment")
.arg("cloud")
.arg("--api-key")
.arg("key")
.arg("--api-secret")
.arg("secret")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("default-cloud")
.arg("myprofile")
.assert()
.success();
}
#[test]
fn test_profile_list_shows_multiple() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("profile1")
.arg("--deployment")
.arg("cloud")
.arg("--api-key")
.arg("key1")
.arg("--api-secret")
.arg("secret1")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("profile2")
.arg("--deployment")
.arg("cloud")
.arg("--api-key")
.arg("key2")
.arg("--api-secret")
.arg("secret2")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("profile3")
.arg("--deployment")
.arg("enterprise")
.arg("--url")
.arg("https://localhost:9443")
.arg("--username")
.arg("admin@redis.local")
.arg("--password")
.arg("pass")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("list")
.assert()
.success()
.stdout(predicate::str::contains("profile1"))
.stdout(predicate::str::contains("profile2"))
.stdout(predicate::str::contains("profile3"));
}
#[test]
fn test_profile_list_json_output() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("json-test")
.arg("--deployment")
.arg("cloud")
.arg("--api-key")
.arg("key")
.arg("--api-secret")
.arg("secret")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("list")
.arg("-o")
.arg("json")
.assert()
.success()
.stdout(predicate::str::contains("json-test"));
}
#[test]
fn test_profile_set_with_config_file_flag() {
let temp_dir = TempDir::new().unwrap();
let custom_config = temp_dir.path().join("custom-config.toml");
let mut cmd = Command::cargo_bin("redisctl").unwrap();
cmd.arg("--config-file")
.arg(&custom_config)
.arg("profile")
.arg("set")
.arg("custom-file-profile")
.arg("--deployment")
.arg("cloud")
.arg("--api-key")
.arg("key")
.arg("--api-secret")
.arg("secret")
.assert()
.success();
assert!(
custom_config.exists(),
"Custom config file should be created"
);
let mut cmd2 = Command::cargo_bin("redisctl").unwrap();
cmd2.arg("--config-file")
.arg(&custom_config)
.arg("profile")
.arg("list")
.assert()
.success()
.stdout(predicate::str::contains("custom-file-profile"));
}
#[test]
fn test_profile_update_existing() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("updateme")
.arg("--deployment")
.arg("cloud")
.arg("--api-key")
.arg("old-key")
.arg("--api-secret")
.arg("old-secret")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("updateme")
.arg("--deployment")
.arg("cloud")
.arg("--api-key")
.arg("new-key")
.arg("--api-secret")
.arg("new-secret")
.assert()
.success();
}
#[test]
fn test_profile_validate() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("valid-profile")
.arg("--deployment")
.arg("cloud")
.arg("--api-key")
.arg("key")
.arg("--api-secret")
.arg("secret")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("validate")
.assert()
.success();
}
#[test]
fn test_profile_set_enterprise_with_insecure() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("insecure-test")
.arg("--deployment")
.arg("enterprise")
.arg("--url")
.arg("https://localhost:9443")
.arg("--username")
.arg("admin@redis.local")
.arg("--password")
.arg("pass")
.arg("--insecure")
.assert()
.success();
}
#[test]
fn test_profile_set_cloud_with_custom_url() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("custom-url")
.arg("--deployment")
.arg("cloud")
.arg("--api-key")
.arg("key")
.arg("--api-secret")
.arg("secret")
.arg("--api-url")
.arg("https://custom-api.example.com/v1")
.assert()
.success();
}
#[test]
fn test_profile_set_with_type_flag_cloud() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("type-test-cloud")
.arg("--type")
.arg("cloud")
.arg("--api-key")
.arg("test-key")
.arg("--api-secret")
.arg("test-secret")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("list")
.assert()
.success()
.stdout(predicate::str::contains("type-test-cloud"));
}
#[test]
fn test_profile_set_with_type_flag_enterprise() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("type-test-enterprise")
.arg("--type")
.arg("enterprise")
.arg("--url")
.arg("https://localhost:9443")
.arg("--username")
.arg("admin@redis.local")
.arg("--password")
.arg("password123")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("list")
.assert()
.success()
.stdout(predicate::str::contains("type-test-enterprise"));
}
#[test]
fn test_profile_set_deployment_alias_still_works() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("alias-test")
.arg("--deployment")
.arg("cloud")
.arg("--api-key")
.arg("key")
.arg("--api-secret")
.arg("secret")
.assert()
.success();
}
#[test]
fn test_profile_set_help_shows_type_flag() {
redisctl()
.arg("profile")
.arg("set")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("--type <TYPE>"))
.stdout(predicate::str::contains("Platform type"));
}
#[test]
fn test_profile_set_help_shows_deployment_alias() {
redisctl()
.arg("profile")
.arg("set")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("--deployment"));
}
#[test]
fn test_profile_set_help_examples_use_type_flag() {
redisctl()
.arg("profile")
.arg("set")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("--type cloud"))
.stdout(predicate::str::contains("--type enterprise"));
}
#[test]
fn test_profile_help_examples_accurate() {
redisctl()
.arg("profile")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("profile set mycloud --type cloud"))
.stdout(predicate::str::contains(
"profile set myenterprise --type enterprise",
));
}
#[test]
fn test_main_help_profile_examples_accurate() {
redisctl()
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("profile set mycloud --type cloud"))
.stdout(predicate::str::contains(
"profile set myenterprise --type enterprise",
));
}
#[test]
fn test_profile_set_requires_type_flag() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("test-profile")
.arg("--api-key")
.arg("key")
.arg("--api-secret")
.arg("secret")
.assert()
.failure()
.stderr(predicate::str::contains("--type"));
}
#[test]
fn test_profile_list_help() {
redisctl()
.arg("profile")
.arg("list")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("List all configured profiles"));
}
#[test]
fn test_profile_show_help() {
redisctl()
.arg("profile")
.arg("show")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains(
"Show details of a specific profile",
));
}
#[test]
fn test_profile_validate_help() {
redisctl()
.arg("profile")
.arg("validate")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains(
"Validate configuration file and profiles",
));
}
#[test]
fn test_profile_remove_help() {
redisctl()
.arg("profile")
.arg("remove")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("Remove a profile"));
}
#[test]
fn test_profile_path_help() {
redisctl()
.arg("profile")
.arg("path")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains(
"Show the path to the configuration file",
));
}
#[test]
fn test_profile_set_database() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("test-database")
.arg("--type")
.arg("database")
.arg("--host")
.arg("localhost")
.arg("--port")
.arg("6379")
.arg("--password")
.arg("secret123")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("list")
.assert()
.success()
.stdout(predicate::str::contains("test-database"));
}
#[test]
fn test_profile_set_database_with_all_options() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("full-database")
.arg("--type")
.arg("database")
.arg("--host")
.arg("redis.example.com")
.arg("--port")
.arg("12345")
.arg("--password")
.arg("secret")
.arg("--username")
.arg("myuser")
.arg("--db")
.arg("5")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("show")
.arg("full-database")
.assert()
.success()
.stdout(predicate::str::contains("database"));
}
#[test]
fn test_profile_set_database_no_tls() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("local-redis")
.arg("--type")
.arg("database")
.arg("--host")
.arg("localhost")
.arg("--port")
.arg("6379")
.arg("--no-tls")
.arg("--password")
.arg("localpass")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("list")
.assert()
.success()
.stdout(predicate::str::contains("local-redis"));
}
#[test]
fn test_profile_set_database_requires_host() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("bad-database")
.arg("--type")
.arg("database")
.arg("--port")
.arg("6379")
.assert()
.failure()
.stderr(predicate::str::contains("--host"));
}
#[test]
fn test_profile_set_database_requires_port() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("bad-database")
.arg("--type")
.arg("database")
.arg("--host")
.arg("localhost")
.assert()
.failure()
.stderr(predicate::str::contains("--port"));
}
#[test]
fn test_profile_default_database() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("my-cache")
.arg("--type")
.arg("database")
.arg("--host")
.arg("localhost")
.arg("--port")
.arg("6379")
.arg("--password")
.arg("secret")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("default-database")
.arg("my-cache")
.assert()
.success();
}
#[test]
fn test_profile_default_database_alias() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("my-cache")
.arg("--type")
.arg("database")
.arg("--host")
.arg("localhost")
.arg("--port")
.arg("6379")
.arg("--password")
.arg("secret")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("def-db")
.arg("my-cache")
.assert()
.success();
}
#[test]
fn test_profile_default_database_nonexistent() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("default-database")
.arg("nonexistent")
.assert()
.failure();
}
#[test]
fn test_profile_help_shows_database_type() {
redisctl()
.arg("profile")
.arg("set")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("database"));
}
#[test]
fn test_profile_help_shows_database_options() {
redisctl()
.arg("profile")
.arg("set")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("--host"))
.stdout(predicate::str::contains("--port"))
.stdout(predicate::str::contains("--no-tls"))
.stdout(predicate::str::contains("--db"));
}
#[test]
fn test_profile_help_shows_database_example() {
redisctl()
.arg("profile")
.arg("set")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("--type database"));
}
#[test]
fn test_profile_help_shows_default_database_command() {
redisctl()
.arg("profile")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("default-database"));
}
#[test]
fn test_profile_list_shows_database_profiles() {
let temp_dir = TempDir::new().unwrap();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("cloud-profile")
.arg("--type")
.arg("cloud")
.arg("--api-key")
.arg("key")
.arg("--api-secret")
.arg("secret")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("ent-profile")
.arg("--type")
.arg("enterprise")
.arg("--url")
.arg("https://localhost:9443")
.arg("--username")
.arg("admin")
.arg("--password")
.arg("pass")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("set")
.arg("db-profile")
.arg("--type")
.arg("database")
.arg("--host")
.arg("localhost")
.arg("--port")
.arg("6379")
.arg("--password")
.arg("secret")
.assert()
.success();
test_cmd(&temp_dir)
.arg("profile")
.arg("list")
.assert()
.success()
.stdout(predicate::str::contains("cloud-profile"))
.stdout(predicate::str::contains("ent-profile"))
.stdout(predicate::str::contains("db-profile"));
}