From bb75303e8a4d61541e45a52cf5e7a7116ee5919f Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Wed, 12 Jun 2024 17:07:36 +0100 Subject: [PATCH] feat!: [#591] lowercase for auth::enail_on_signup emnum variants --- src/config/mod.rs | 2 +- src/config/v1/auth.rs | 26 +++++++++++++++++++ src/lib.rs | 2 +- .../api/server/v1/contexts/settings/mod.rs | 6 ++--- src/web/api/server/v1/contexts/user/mod.rs | 2 +- tests/common/contexts/settings/mod.rs | 2 +- 6 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index ccdff762..9c75a557 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -339,7 +339,7 @@ mod tests { port = 3001 [auth] - email_on_signup = "Optional" + email_on_signup = "optional" min_password_length = 6 max_password_length = 64 secret_key = "MaxVerstappenWC2021" diff --git a/src/config/v1/auth.rs b/src/config/v1/auth.rs index 6b7f0786..29bfaa59 100644 --- a/src/config/v1/auth.rs +++ b/src/config/v1/auth.rs @@ -1,4 +1,5 @@ use std::fmt; +use std::str::FromStr; use serde::{Deserialize, Serialize}; @@ -50,6 +51,7 @@ impl Auth { /// Whether the email is required on signup or not. #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +#[serde(rename_all = "lowercase")] pub enum EmailOnSignup { /// The email is required on signup. Required, @@ -65,6 +67,30 @@ impl Default for EmailOnSignup { } } +impl fmt::Display for EmailOnSignup { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let display_str = match self { + EmailOnSignup::Required => "required", + EmailOnSignup::Optional => "optional", + EmailOnSignup::None => "none", + }; + write!(f, "{display_str}") + } +} + +impl FromStr for EmailOnSignup { + type Err = String; + + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_str() { + "required" => Ok(EmailOnSignup::Required), + "optional" => Ok(EmailOnSignup::Optional), + "none" => Ok(EmailOnSignup::None), + _ => Err(format!("Unknown config 'email_on_signup' option (required, optional, none): {s}")), + } + } +} + #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct SecretKey(String); diff --git a/src/lib.rs b/src/lib.rs index 0a26c192..fa7fc351 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -180,7 +180,7 @@ //! port = 3001 //! //! [auth] -//! email_on_signup = "Optional" +//! email_on_signup = "optional" //! min_password_length = 6 //! max_password_length = 64 //! secret_key = "MaxVerstappenWC2021" diff --git a/src/web/api/server/v1/contexts/settings/mod.rs b/src/web/api/server/v1/contexts/settings/mod.rs index 089137e6..b30cd96b 100644 --- a/src/web/api/server/v1/contexts/settings/mod.rs +++ b/src/web/api/server/v1/contexts/settings/mod.rs @@ -45,7 +45,7 @@ //! "base_url": null //! }, //! "auth": { -//! "email_on_signup": "Optional", +//! "email_on_signup": "optional", //! "min_password_length": 6, //! "max_password_length": 64, //! "secret_key": "MaxVerstappenWC2021" @@ -102,7 +102,7 @@ //! --header "Content-Type: application/json" \ //! --header "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7InVzZXJfaWQiOjEsInVzZXJuYW1lIjoiaW5kZXhhZG1pbiIsImFkbWluaXN0cmF0b3IiOnRydWV9LCJleHAiOjE2ODYyMTU3ODh9.4k8ty27DiWwOk4WVcYEhIrAndhpXMRWnLZ3i_HlJnvI" \ //! --request POST \ -//! --data '{"website":{"name":"Torrust"},"tracker":{"url":"udp://localhost:6969","mode":"public","api_url":"http://localhost:1212/","token":"MyAccessToken","token_valid_seconds":7257600},"net":{"port":3001,"base_url":null},"auth":{"email_on_signup":"Optional","min_password_length":6,"max_password_length":64,"secret_key":"MaxVerstappenWC2021"},"database":{"connect_url":"sqlite://./storage/database/data.db?mode=rwc"},"mail":{"email_verification_enabled":false,"from":"example@email.com","reply_to":"noreply@email.com","username":"","password":"","server":"","port":25},"image_cache":{"max_request_timeout_ms":1000,"capacity":128000000,"entry_size_limit":4000000,"user_quota_period_seconds":3600,"user_quota_bytes":64000000},"api":{"default_torrent_page_size":10,"max_torrent_page_size":30},"tracker_statistics_importer":{"torrent_info_update_interval":3600}}' \ +//! --data '{"website":{"name":"Torrust"},"tracker":{"url":"udp://localhost:6969","mode":"public","api_url":"http://localhost:1212/","token":"MyAccessToken","token_valid_seconds":7257600},"net":{"port":3001,"base_url":null},"auth":{"email_on_signup":"optional","min_password_length":6,"max_password_length":64,"secret_key":"MaxVerstappenWC2021"},"database":{"connect_url":"sqlite://./storage/database/data.db?mode=rwc"},"mail":{"email_verification_enabled":false,"from":"example@email.com","reply_to":"noreply@email.com","username":"","password":"","server":"","port":25},"image_cache":{"max_request_timeout_ms":1000,"capacity":128000000,"entry_size_limit":4000000,"user_quota_period_seconds":3600,"user_quota_bytes":64000000},"api":{"default_torrent_page_size":10,"max_torrent_page_size":30},"tracker_statistics_importer":{"torrent_info_update_interval":3600}}' \ //! "http://127.0.0.1:3001/v1/settings" //! ``` //! @@ -159,7 +159,7 @@ //! "website_name": "Torrust", //! "tracker_url": "udp://localhost:6969", //! "tracker_mode": "public", -//! "email_on_signup": "Optional" +//! "email_on_signup": "optional" //! } //! } //! ``` diff --git a/src/web/api/server/v1/contexts/user/mod.rs b/src/web/api/server/v1/contexts/user/mod.rs index a13c2bb8..2e3ce8d0 100644 --- a/src/web/api/server/v1/contexts/user/mod.rs +++ b/src/web/api/server/v1/contexts/user/mod.rs @@ -45,7 +45,7 @@ //! //! ```toml //! [auth] -//! email_on_signup = "Optional" +//! email_on_signup = "optional" //! min_password_length = 6 //! max_password_length = 64 //! ``` diff --git a/tests/common/contexts/settings/mod.rs b/tests/common/contexts/settings/mod.rs index a3e1b080..8ca66a66 100644 --- a/tests/common/contexts/settings/mod.rs +++ b/tests/common/contexts/settings/mod.rs @@ -133,7 +133,7 @@ impl From for Network { impl From for Auth { fn from(auth: DomainAuth) -> Self { Self { - email_on_signup: format!("{:?}", auth.email_on_signup), + email_on_signup: auth.email_on_signup.to_string(), min_password_length: auth.min_password_length, max_password_length: auth.max_password_length, secret_key: auth.secret_key.to_string(),