feat: Created a separate 'ssl' property for the config so users don't have to specify an ssl_cert_path to use SSL or use the uri workaround for HTTPS API access
This commit is contained in:
+74
-1
@@ -447,6 +447,78 @@ mod tests {
|
||||
assert_none!(config.port);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn test_deserialize_optional_env_var_bool_is_bool() {
|
||||
let yaml_data = r#"
|
||||
host: localhost
|
||||
api_token: "test123"
|
||||
ssl: true
|
||||
"#;
|
||||
|
||||
let config: ServarrConfig = serde_yaml::from_str(yaml_data).unwrap();
|
||||
|
||||
assert_some_eq_x!(&config.ssl, &true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn test_deserialize_optional_env_var_bool_is_string() {
|
||||
let yaml_data = r#"
|
||||
host: localhost
|
||||
api_token: "test123"
|
||||
ssl: "true"
|
||||
"#;
|
||||
|
||||
let config: ServarrConfig = serde_yaml::from_str(yaml_data).unwrap();
|
||||
|
||||
assert_some_eq_x!(&config.ssl, &true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn test_deserialize_optional_env_var_bool_is_present() {
|
||||
unsafe { std::env::set_var("TEST_VAR_DESERIALIZE_OPTION_BOOL", "true") };
|
||||
let yaml_data = r#"
|
||||
host: localhost
|
||||
api_token: "test123"
|
||||
ssl: ${TEST_VAR_DESERIALIZE_OPTION_BOOL}
|
||||
"#;
|
||||
|
||||
let config: ServarrConfig = serde_yaml::from_str(yaml_data).unwrap();
|
||||
|
||||
assert_some_eq_x!(&config.ssl, &true);
|
||||
unsafe { std::env::remove_var("TEST_VAR_DESERIALIZE_OPTION_BOOL") };
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn test_deserialize_optional_env_var_bool_defaults_to_false() {
|
||||
unsafe { std::env::set_var("TEST_VAR_DESERIALIZE_OPTION_BOOL_FALSEY", "test") };
|
||||
let yaml_data = r#"
|
||||
host: localhost
|
||||
api_token: "test123"
|
||||
ssl: ${TEST_VAR_DESERIALIZE_OPTION_BOOL_FALSEY}
|
||||
"#;
|
||||
|
||||
let config: ServarrConfig = serde_yaml::from_str(yaml_data).unwrap();
|
||||
|
||||
assert_some_eq_x!(&config.ssl, &false);
|
||||
unsafe { std::env::remove_var("TEST_VAR_DESERIALIZE_OPTION_BOOL_FALSEY") };
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_optional_env_var_bool_empty() {
|
||||
let yaml_data = r#"
|
||||
host: localhost
|
||||
api_token: "test123"
|
||||
"#;
|
||||
|
||||
let config: ServarrConfig = serde_yaml::from_str(yaml_data).unwrap();
|
||||
|
||||
assert_none!(config.ssl);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn test_deserialize_optional_env_var_header_map_is_present() {
|
||||
@@ -674,7 +746,7 @@ mod tests {
|
||||
let mut custom_headers = HeaderMap::new();
|
||||
custom_headers.insert("X-Custom-Header", "value".parse().unwrap());
|
||||
let expected_str = format!(
|
||||
"ServarrConfig {{ name: Some(\"{name}\"), host: Some(\"{host}\"), port: Some({port}), uri: Some(\"{uri}\"), weight: Some({weight}), api_token: Some(\"***********\"), api_token_file: Some(\"{api_token_file}\"), ssl_cert_path: Some(\"{ssl_cert_path}\"), custom_headers: Some({{\"x-custom-header\": \"value\"}}), monitored_storage_paths: Some([\"/path1\", \"/path2\"]) }}"
|
||||
"ServarrConfig {{ name: Some(\"{name}\"), host: Some(\"{host}\"), port: Some({port}), uri: Some(\"{uri}\"), weight: Some({weight}), api_token: Some(\"***********\"), api_token_file: Some(\"{api_token_file}\"), ssl: Some(true), ssl_cert_path: Some(\"{ssl_cert_path}\"), custom_headers: Some({{\"x-custom-header\": \"value\"}}), monitored_storage_paths: Some([\"/path1\", \"/path2\"]) }}"
|
||||
);
|
||||
let servarr_config = ServarrConfig {
|
||||
name: Some(name),
|
||||
@@ -685,6 +757,7 @@ mod tests {
|
||||
api_token: Some(api_token),
|
||||
api_token_file: Some(api_token_file),
|
||||
ssl_cert_path: Some(ssl_cert_path),
|
||||
ssl: Some(true),
|
||||
custom_headers: Some(custom_headers),
|
||||
monitored_storage_paths: Some(monitored_storage),
|
||||
};
|
||||
|
||||
@@ -431,6 +431,8 @@ pub struct ServarrConfig {
|
||||
pub api_token: Option<String>,
|
||||
#[serde(default, deserialize_with = "deserialize_optional_env_var")]
|
||||
pub api_token_file: Option<String>,
|
||||
#[serde(default, deserialize_with = "deserialize_optional_env_var_bool")]
|
||||
pub ssl: Option<bool>,
|
||||
#[serde(default, deserialize_with = "deserialize_optional_env_var")]
|
||||
pub ssl_cert_path: Option<String>,
|
||||
#[serde(
|
||||
@@ -486,6 +488,7 @@ impl Default for ServarrConfig {
|
||||
api_token: Some(String::new()),
|
||||
api_token_file: None,
|
||||
ssl_cert_path: None,
|
||||
ssl: None,
|
||||
custom_headers: None,
|
||||
monitored_storage_paths: None,
|
||||
}
|
||||
@@ -532,6 +535,29 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_optional_env_var_bool<'de, D>(deserializer: D) -> Result<Option<bool>, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
#[derive(Deserialize)]
|
||||
#[serde(untagged)]
|
||||
enum StringOrBool {
|
||||
Bool(bool),
|
||||
String(String),
|
||||
}
|
||||
|
||||
match StringOrBool::deserialize(deserializer)? {
|
||||
StringOrBool::Bool(b) => Ok(Some(b)),
|
||||
StringOrBool::String(s) => {
|
||||
let val = interpolate_env_vars(&s)
|
||||
.to_lowercase()
|
||||
.parse()
|
||||
.unwrap_or(false);
|
||||
Ok(Some(val))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_optional_env_var_header_map<'de, D>(
|
||||
deserializer: D,
|
||||
) -> Result<Option<HeaderMap>, D::Error>
|
||||
|
||||
Reference in New Issue
Block a user