Skip to content

Commit

Permalink
feat!: [#591] lowercase for auth::enail_on_signup emnum variants
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jun 12, 2024
1 parent eb987e9 commit bb75303
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 26 additions & 0 deletions src/config/v1/auth.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt;
use std::str::FromStr;

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -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,
Expand All @@ -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<Self, Self::Err> {
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);

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions src/web/api/server/v1/contexts/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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":"[email protected]","reply_to":"[email protected]","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":"[email protected]","reply_to":"[email protected]","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"
//! ```
//!
Expand Down Expand Up @@ -159,7 +159,7 @@
//! "website_name": "Torrust",
//! "tracker_url": "udp://localhost:6969",
//! "tracker_mode": "public",
//! "email_on_signup": "Optional"
//! "email_on_signup": "optional"
//! }
//! }
//! ```
Expand Down
2 changes: 1 addition & 1 deletion src/web/api/server/v1/contexts/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
//!
//! ```toml
//! [auth]
//! email_on_signup = "Optional"
//! email_on_signup = "optional"
//! min_password_length = 6
//! max_password_length = 64
//! ```
Expand Down
2 changes: 1 addition & 1 deletion tests/common/contexts/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl From<DomainNetwork> for Network {
impl From<DomainAuth> 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(),
Expand Down

0 comments on commit bb75303

Please sign in to comment.