use secretspec_derive::declare_secrets;
mod basic_generation {
use super::*;
declare_secrets!("tests/fixtures/basic.toml");
#[test]
fn test_struct_fields_exist() {
fn _test_field_types(s: SecretSpec) {
let _: String = s.api_key;
let _: String = s.database_url;
let _: Option<String> = s.optional_secret;
}
}
}
mod profile_generation {
use super::*;
declare_secrets!("tests/fixtures/profiles.toml");
#[test]
fn test_profile_enum_variants() {
let _dev = Profile::Development;
let _staging = Profile::Staging;
let _prod = Profile::Production;
}
#[test]
fn test_profile_specific_types() {
fn _test_development(profile: SecretSpecProfile) {
match profile {
SecretSpecProfile::Development {
api_key,
database_url,
redis_url,
} => {
let _: Option<String> = api_key; let _: String = database_url; let _: Option<String> = redis_url; }
_ => panic!("Expected Development variant"),
}
}
fn _test_production(profile: SecretSpecProfile) {
match profile {
SecretSpecProfile::Production {
api_key,
database_url,
redis_url,
} => {
let _: String = api_key; let _: String = database_url; let _: String = redis_url; }
_ => panic!("Expected Production variant"),
}
}
}
#[test]
fn test_union_type_fields() {
fn _test_field_types(s: SecretSpec) {
let _: Option<String> = s.api_key; let _: String = s.database_url; let _: Option<String> = s.redis_url; }
}
}
mod complex_generation {
use super::*;
declare_secrets!("tests/fixtures/complex.toml");
#[test]
fn test_complex_field_types() {
fn _test_field_types(s: SecretSpec) {
let _: String = s.always_required;
let _: String = s.required_with_default; let _: Option<String> = s.always_optional;
let _: Option<String> = s.complex_secret; let _: Option<String> = s.multi_profile; }
}
#[test]
fn test_all_profiles_generated() {
let _dev = Profile::Development;
let _staging = Profile::Staging;
let _prod = Profile::Production;
let _test = Profile::Test;
}
}
mod empty_generation {
use super::*;
declare_secrets!("tests/fixtures/empty.toml");
#[test]
fn test_empty_struct() {
let _size = std::mem::size_of::<SecretSpec>();
fn _test_no_fields(_s: SecretSpec) {
}
}
}
mod json_serialization {
use super::*;
use serde_json;
declare_secrets!("tests/fixtures/basic.toml");
#[test]
fn test_secret_spec_secrets_json_serialization() {
use secretspec::Resolved;
let spec = SecretSpec {
api_key: "test_key".to_string(),
database_url: "postgres://localhost/db".to_string(),
optional_secret: Some("optional".to_string()),
};
let secrets_wrapper = Resolved::new(spec, "dotenv".to_string(), "production".to_string());
let json = serde_json::to_string(&secrets_wrapper).expect("Failed to serialize Resolved");
let parsed: serde_json::Value = serde_json::from_str(&json).expect("Failed to parse JSON");
assert_eq!(parsed["provider"], "dotenv");
assert_eq!(parsed["profile"], "production");
assert_eq!(parsed["secrets"]["api_key"], "test_key");
let deserialized: Resolved<SecretSpec> =
serde_json::from_str(&json).expect("Failed to deserialize Resolved");
assert_eq!(deserialized.provider, "dotenv");
assert_eq!(deserialized.profile, "production");
assert_eq!(deserialized.secrets.api_key, "test_key");
}
}
mod profile_inheritance {
use super::*;
declare_secrets!("tests/fixtures/profile_inheritance.toml");
#[test]
fn test_profile_inheritance_compilation() {
let _default = Profile::Default;
let _dev = Profile::Development;
let _prod = Profile::Production;
let _staging = Profile::Staging;
}
#[test]
fn test_union_type_with_inheritance() {
fn _test_field_types(s: SecretSpec) {
let _: Option<String> = s.database_url;
let _: Option<String> = s.api_key;
let _: Option<String> = s.log_level;
let _: Option<String> = s.cache_ttl;
let _: Option<String> = s.debug_mode;
let _: Option<String> = s.enable_profiling;
}
}
#[test]
fn test_profile_specific_with_inheritance() {
fn _test_default(profile: SecretSpecProfile) {
match profile {
SecretSpecProfile::Default {
database_url,
api_key,
log_level,
cache_ttl,
} => {
let _: String = database_url; let _: String = api_key; let _: Option<String> = log_level; let _: Option<String> = cache_ttl; }
_ => panic!("Expected Default variant"),
}
}
fn _test_development(profile: SecretSpecProfile) {
match profile {
SecretSpecProfile::Development {
database_url,
debug_mode,
} => {
let _: Option<String> = database_url; let _: Option<String> = debug_mode; }
_ => panic!("Expected Development variant"),
}
}
fn _test_production(profile: SecretSpecProfile) {
match profile {
SecretSpecProfile::Production {
database_url,
api_key,
log_level,
} => {
let _: String = database_url; let _: String = api_key; let _: Option<String> = log_level; }
_ => panic!("Expected Production variant"),
}
}
fn _test_staging(profile: SecretSpecProfile) {
match profile {
SecretSpecProfile::Staging {
database_url,
log_level,
enable_profiling,
} => {
let _: String = database_url; let _: Option<String> = log_level; let _: Option<String> = enable_profiling; }
_ => panic!("Expected Staging variant"),
}
}
}
#[test]
fn test_builder_works_with_inherited_profiles() {
let _builder = SecretSpec::builder();
let _ = SecretSpec::builder()
.with_profile("development")
.with_provider("dotenv://.env");
let _ = SecretSpec::builder()
.with_profile(Profile::Production)
.with_provider("keyring://");
}
}