diff --git a/Cargo.lock b/Cargo.lock index e312933..d176d67 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -214,6 +214,7 @@ dependencies = [ "serde_json", "sha2", "task-local-extensions", + "temp-env", "thiserror", "tokio", "tracing", @@ -2082,6 +2083,15 @@ dependencies = [ "pin-utils", ] +[[package]] +name = "temp-env" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96374855068f47402c3121c6eed88d29cb1de8f3ab27090e273e420bdabcf050" +dependencies = [ + "parking_lot", +] + [[package]] name = "termtree" version = "0.5.1" diff --git a/Cargo.toml b/Cargo.toml index dddcb92..0655833 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,12 +24,19 @@ rust_decimal = { version = "1.33", features = ["serde-float"] } async-trait = "0.1" dotenvy = "0.15" clap = { version = "4.4", features = ["derive", "env"] } -reqwest = { version = "0.11", default-features = false, features = ["json", "multipart", "rustls-tls"] } +reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls"] } url = "2.5" wiremock = "0.5" tokio-test = "0.4" - mockall = "0.11" - reqwest-middleware = "0.2" - hyper = { version = "0.14", features = ["full"] } - bytes = "1.0" - comfy-table = "7.1" +mockall = "0.11" +reqwest-middleware = "0.2" +hyper = { version = "0.14", features = ["full"] } +bytes = "1.0" +comfy-table = "7.1" +http = "0.2" +task-local-extensions = "0.1" +aes-gcm = "0.10" +pbkdf2 = "0.12" +rand = "0.8" +sha2 = "0.10" +temp-env = "0.3" diff --git a/banks2ff/Cargo.toml b/banks2ff/Cargo.toml index 5dd7ac9..53e7167 100644 --- a/banks2ff/Cargo.toml +++ b/banks2ff/Cargo.toml @@ -29,17 +29,18 @@ gocardless-client = { path = "../gocardless-client" } reqwest-middleware = { workspace = true } hyper = { workspace = true } bytes = { workspace = true } -http = "0.2" -task-local-extensions = "0.1" +http = { workspace = true } +task-local-extensions = { workspace = true } # Encryption dependencies -aes-gcm = "0.10" -pbkdf2 = "0.12" -rand = "0.8" -sha2 = "0.10" +aes-gcm = { workspace = true } +pbkdf2 = { workspace = true } +rand = { workspace = true } +sha2 = { workspace = true } # CLI formatting dependencies comfy-table = { workspace = true } [dev-dependencies] mockall = { workspace = true } +temp-env = { workspace = true } diff --git a/banks2ff/src/core/config.rs b/banks2ff/src/core/config.rs index 98007b9..51b1fc1 100644 --- a/banks2ff/src/core/config.rs +++ b/banks2ff/src/core/config.rs @@ -105,128 +105,137 @@ impl LoggingConfig { } } - #[cfg(test)] - mod tests { - use super::*; - use std::env; +#[cfg(test)] +mod tests { + use super::*; + use temp_env::with_vars; #[test] fn test_gocardless_config_from_env() { - env::set_var("GOCARDLESS_ID", "test-id"); - env::set_var("GOCARDLESS_KEY", "test-key"); - env::set_var("GOCARDLESS_URL", "https://test.example.com"); - - let config = GoCardlessConfig::from_env().unwrap(); - assert_eq!(config.secret_id, "test-id"); - assert_eq!(config.secret_key, "test-key"); - assert_eq!(config.url, "https://test.example.com"); - - env::remove_var("GOCARDLESS_ID"); - env::remove_var("GOCARDLESS_KEY"); - env::remove_var("GOCARDLESS_URL"); + with_vars( + [ + ("GOCARDLESS_ID", Some("test-id")), + ("GOCARDLESS_KEY", Some("test-key")), + ("GOCARDLESS_URL", Some("https://test.example.com")), + ], + || { + let config = GoCardlessConfig::from_env().unwrap(); + assert_eq!(config.secret_id, "test-id"); + assert_eq!(config.secret_key, "test-key"); + assert_eq!(config.url, "https://test.example.com"); + }, + ); } #[test] fn test_gocardless_config_default_url() { - env::set_var("GOCARDLESS_ID", "test-id"); - env::set_var("GOCARDLESS_KEY", "test-key"); - env::remove_var("GOCARDLESS_URL"); - - let config = GoCardlessConfig::from_env().unwrap(); - assert_eq!(config.url, "https://bankaccountdata.gocardless.com"); - - env::remove_var("GOCARDLESS_ID"); - env::remove_var("GOCARDLESS_KEY"); + with_vars( + [ + ("GOCARDLESS_ID", Some("test-id")), + ("GOCARDLESS_KEY", Some("test-key")), + ("GOCARDLESS_URL", None), + ], + || { + let config = GoCardlessConfig::from_env().unwrap(); + assert_eq!(config.url, "https://bankaccountdata.gocardless.com"); + }, + ); } #[test] fn test_gocardless_config_missing_id() { - env::remove_var("GOCARDLESS_ID"); - env::set_var("GOCARDLESS_KEY", "test-key"); - - let result = GoCardlessConfig::from_env(); - assert!(result.is_err()); - - env::remove_var("GOCARDLESS_KEY"); + with_vars( + [ + ("GOCARDLESS_ID", None), + ("GOCARDLESS_KEY", Some("test-key")), + ], + || { + let result = GoCardlessConfig::from_env(); + assert!(result.is_err()); + }, + ); } #[test] fn test_firefly_config_from_env() { - env::set_var("FIREFLY_III_URL", "https://firefly.test.com"); - env::set_var("FIREFLY_III_API_KEY", "test-api-key"); - - let config = FireflyConfig::from_env().unwrap(); - assert_eq!(config.url, "https://firefly.test.com"); - assert_eq!(config.api_key, "test-api-key"); - - env::remove_var("FIREFLY_III_URL"); - env::remove_var("FIREFLY_III_API_KEY"); + with_vars( + [ + ("FIREFLY_III_URL", Some("https://firefly.test.com")), + ("FIREFLY_III_API_KEY", Some("test-api-key")), + ], + || { + let config = FireflyConfig::from_env().unwrap(); + assert_eq!(config.url, "https://firefly.test.com"); + assert_eq!(config.api_key, "test-api-key"); + }, + ); } #[test] fn test_cache_config_from_env() { - env::set_var("BANKS2FF_CACHE_KEY", "test-cache-key"); - env::set_var("BANKS2FF_CACHE_DIR", "/tmp/test-cache"); - - let config = CacheConfig::from_env().unwrap(); - assert_eq!(config.key, "test-cache-key"); - assert_eq!(config.directory, "/tmp/test-cache"); - - env::remove_var("BANKS2FF_CACHE_KEY"); - env::remove_var("BANKS2FF_CACHE_DIR"); + with_vars( + [ + ("BANKS2FF_CACHE_KEY", Some("test-cache-key")), + ("BANKS2FF_CACHE_DIR", Some("/tmp/test-cache")), + ], + || { + let config = CacheConfig::from_env().unwrap(); + assert_eq!(config.key, "test-cache-key"); + assert_eq!(config.directory, "/tmp/test-cache"); + }, + ); } #[test] fn test_cache_config_default_directory() { - env::remove_var("BANKS2FF_CACHE_DIR"); - env::set_var("BANKS2FF_CACHE_KEY", "test-cache-key"); - - let config = CacheConfig::from_env().unwrap(); - assert_eq!(config.directory, "data/cache"); - - env::remove_var("BANKS2FF_CACHE_KEY"); + with_vars( + [ + ("BANKS2FF_CACHE_DIR", None), + ("BANKS2FF_CACHE_KEY", Some("test-cache-key")), + ], + || { + let config = CacheConfig::from_env().unwrap(); + assert_eq!(config.directory, "data/cache"); + }, + ); } #[test] fn test_logging_config_from_env() { - env::set_var("RUST_LOG", "debug"); - - let config = LoggingConfig::from_env().unwrap(); - assert_eq!(config.level, "debug"); - - env::remove_var("RUST_LOG"); + with_vars([("RUST_LOG", Some("debug"))], || { + let config = LoggingConfig::from_env().unwrap(); + assert_eq!(config.level, "debug"); + }); } #[test] fn test_logging_config_default() { - env::remove_var("RUST_LOG"); - - let config = LoggingConfig::from_env().unwrap(); - assert_eq!(config.level, "warn"); + with_vars([("RUST_LOG", None::<&str>)], || { + let config = LoggingConfig::from_env().unwrap(); + assert_eq!(config.level, "warn"); + }); } #[test] fn test_full_config_from_env() { - env::set_var("GOCARDLESS_ID", "test-id"); - env::set_var("GOCARDLESS_KEY", "test-key"); - env::set_var("FIREFLY_III_URL", "https://firefly.test.com"); - env::set_var("FIREFLY_III_API_KEY", "test-api-key"); - env::set_var("BANKS2FF_CACHE_KEY", "test-cache-key"); - env::set_var("RUST_LOG", "info"); - - let config = Config::from_env().unwrap(); - assert_eq!(config.gocardless.secret_id, "test-id"); - assert_eq!(config.gocardless.secret_key, "test-key"); - assert_eq!(config.firefly.url, "https://firefly.test.com"); - assert_eq!(config.firefly.api_key, "test-api-key"); - assert_eq!(config.cache.key, "test-cache-key"); - assert_eq!(config.logging.level, "info"); - - env::remove_var("GOCARDLESS_ID"); - env::remove_var("GOCARDLESS_KEY"); - env::remove_var("FIREFLY_III_URL"); - env::remove_var("FIREFLY_III_API_KEY"); - env::remove_var("BANKS2FF_CACHE_KEY"); - env::remove_var("RUST_LOG"); + with_vars( + [ + ("GOCARDLESS_ID", Some("test-id")), + ("GOCARDLESS_KEY", Some("test-key")), + ("FIREFLY_III_URL", Some("https://firefly.test.com")), + ("FIREFLY_III_API_KEY", Some("test-api-key")), + ("BANKS2FF_CACHE_KEY", Some("test-cache-key")), + ("RUST_LOG", Some("info")), + ], + || { + let config = Config::from_env().unwrap(); + assert_eq!(config.gocardless.secret_id, "test-id"); + assert_eq!(config.gocardless.secret_key, "test-key"); + assert_eq!(config.firefly.url, "https://firefly.test.com"); + assert_eq!(config.firefly.api_key, "test-api-key"); + assert_eq!(config.cache.key, "test-cache-key"); + assert_eq!(config.logging.level, "info"); + }, + ); } }