mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 09:26:29 +03:00
fix: save custom username if user entered it
This commit is contained in:
@@ -93,6 +93,16 @@ def test_configure_alternative_port(acfactory) -> None:
|
|||||||
account.configure()
|
account.configure()
|
||||||
|
|
||||||
|
|
||||||
|
def test_configure_username(acfactory) -> None:
|
||||||
|
account = acfactory.new_preconfigured_account()
|
||||||
|
|
||||||
|
addr = account.get_config("addr")
|
||||||
|
account.set_config("mail_user", addr)
|
||||||
|
account.configure()
|
||||||
|
|
||||||
|
assert account.get_config("configured_mail_user") == addr
|
||||||
|
|
||||||
|
|
||||||
def test_account(acfactory) -> None:
|
def test_account(acfactory) -> None:
|
||||||
alice, bob = acfactory.get_online_accounts(2)
|
alice, bob = acfactory.get_online_accounts(2)
|
||||||
|
|
||||||
|
|||||||
@@ -219,7 +219,7 @@ pub enum Config {
|
|||||||
|
|
||||||
/// Configured IMAP server username.
|
/// Configured IMAP server username.
|
||||||
///
|
///
|
||||||
/// This is replaced by `configured_imap_servers` for new configurations.
|
/// This is set if user has configured username manually.
|
||||||
ConfiguredMailUser,
|
ConfiguredMailUser,
|
||||||
|
|
||||||
/// Configured IMAP server password.
|
/// Configured IMAP server password.
|
||||||
@@ -253,7 +253,7 @@ pub enum Config {
|
|||||||
|
|
||||||
/// Configured SMTP server username.
|
/// Configured SMTP server username.
|
||||||
///
|
///
|
||||||
/// This is replaced by `configured_smtp_servers` for new configurations.
|
/// This is set if user has configured username manually.
|
||||||
ConfiguredSendUser,
|
ConfiguredSendUser,
|
||||||
|
|
||||||
/// Configured SMTP server password.
|
/// Configured SMTP server password.
|
||||||
|
|||||||
@@ -332,6 +332,7 @@ async fn get_configured_param(
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
|
imap_user: param.imap.user.clone(),
|
||||||
imap_password: param.imap.password.clone(),
|
imap_password: param.imap.password.clone(),
|
||||||
smtp: servers
|
smtp: servers
|
||||||
.iter()
|
.iter()
|
||||||
@@ -353,6 +354,7 @@ async fn get_configured_param(
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
|
smtp_user: param.smtp.user.clone(),
|
||||||
smtp_password,
|
smtp_password,
|
||||||
socks5_config: param.socks5_config.clone(),
|
socks5_config: param.socks5_config.clone(),
|
||||||
provider,
|
provider,
|
||||||
@@ -610,7 +612,9 @@ pub enum Error {
|
|||||||
mod tests {
|
mod tests {
|
||||||
#![allow(clippy::indexing_slicing)]
|
#![allow(clippy::indexing_slicing)]
|
||||||
|
|
||||||
|
use super::*;
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
use crate::login_param::EnteredServerLoginParam;
|
||||||
use crate::test_utils::TestContext;
|
use crate::test_utils::TestContext;
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
@@ -622,4 +626,24 @@ mod tests {
|
|||||||
t.set_config(Config::MailPw, Some("123456")).await.unwrap();
|
t.set_config(Config::MailPw, Some("123456")).await.unwrap();
|
||||||
assert!(t.configure().await.is_err());
|
assert!(t.configure().await.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
|
async fn test_get_configured_param() -> Result<()> {
|
||||||
|
let t = &TestContext::new().await;
|
||||||
|
let entered_param = EnteredLoginParam {
|
||||||
|
addr: "alice@example.org".to_string(),
|
||||||
|
|
||||||
|
imap: EnteredServerLoginParam {
|
||||||
|
user: "alice@example.net".to_string(),
|
||||||
|
password: "foobar".to_string(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let configured_param = get_configured_param(t, &entered_param).await?;
|
||||||
|
assert_eq!(configured_param.imap_user, "alice@example.net");
|
||||||
|
assert_eq!(configured_param.smtp_user, "");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ pub enum ConfiguredCertificateChecks {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Login parameters for a single server, either IMAP or SMTP
|
/// Login parameters for a single server, either IMAP or SMTP
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
||||||
pub struct EnteredServerLoginParam {
|
pub struct EnteredServerLoginParam {
|
||||||
/// Server hostname or IP address.
|
/// Server hostname or IP address.
|
||||||
pub server: String,
|
pub server: String,
|
||||||
@@ -99,7 +99,7 @@ pub struct EnteredServerLoginParam {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Login parameters entered by the user.
|
/// Login parameters entered by the user.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
||||||
pub struct EnteredLoginParam {
|
pub struct EnteredLoginParam {
|
||||||
/// Email address.
|
/// Email address.
|
||||||
pub addr: String,
|
pub addr: String,
|
||||||
@@ -360,10 +360,22 @@ pub struct ConfiguredLoginParam {
|
|||||||
|
|
||||||
pub imap: Vec<ConfiguredServerLoginParam>,
|
pub imap: Vec<ConfiguredServerLoginParam>,
|
||||||
|
|
||||||
|
// Custom IMAP user.
|
||||||
|
//
|
||||||
|
// This overwrites autoconfig from the provider database
|
||||||
|
// if non-empty.
|
||||||
|
pub imap_user: String,
|
||||||
|
|
||||||
pub imap_password: String,
|
pub imap_password: String,
|
||||||
|
|
||||||
pub smtp: Vec<ConfiguredServerLoginParam>,
|
pub smtp: Vec<ConfiguredServerLoginParam>,
|
||||||
|
|
||||||
|
// Custom SMTP user.
|
||||||
|
//
|
||||||
|
// This overwrites autoconfig from the provider database
|
||||||
|
// if non-empty.
|
||||||
|
pub smtp_user: String,
|
||||||
|
|
||||||
pub smtp_password: String,
|
pub smtp_password: String,
|
||||||
|
|
||||||
pub socks5_config: Option<Socks5Config>,
|
pub socks5_config: Option<Socks5Config>,
|
||||||
@@ -457,8 +469,14 @@ impl ConfiguredLoginParam {
|
|||||||
let imap;
|
let imap;
|
||||||
let smtp;
|
let smtp;
|
||||||
|
|
||||||
let legacy_mail_user = context.get_config(Config::ConfiguredMailUser).await?;
|
let mail_user = context
|
||||||
let legacy_send_user = context.get_config(Config::ConfiguredSendUser).await?;
|
.get_config(Config::ConfiguredMailUser)
|
||||||
|
.await?
|
||||||
|
.unwrap_or_default();
|
||||||
|
let send_user = context
|
||||||
|
.get_config(Config::ConfiguredSendUser)
|
||||||
|
.await?
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
if let Some(provider) = provider {
|
if let Some(provider) = provider {
|
||||||
let addr_localpart = if let Some(at) = addr.find('@') {
|
let addr_localpart = if let Some(at) = addr.find('@') {
|
||||||
@@ -484,8 +502,8 @@ impl ConfiguredLoginParam {
|
|||||||
port: server.port,
|
port: server.port,
|
||||||
security,
|
security,
|
||||||
},
|
},
|
||||||
user: if let Some(legacy_mail_user) = &legacy_mail_user {
|
user: if !mail_user.is_empty() {
|
||||||
legacy_mail_user.clone()
|
mail_user.clone()
|
||||||
} else {
|
} else {
|
||||||
match server.username_pattern {
|
match server.username_pattern {
|
||||||
UsernamePattern::Email => addr.to_string(),
|
UsernamePattern::Email => addr.to_string(),
|
||||||
@@ -513,8 +531,8 @@ impl ConfiguredLoginParam {
|
|||||||
port: server.port,
|
port: server.port,
|
||||||
security,
|
security,
|
||||||
},
|
},
|
||||||
user: if let Some(legacy_send_user) = &legacy_send_user {
|
user: if !send_user.is_empty() {
|
||||||
legacy_send_user.clone()
|
send_user.clone()
|
||||||
} else {
|
} else {
|
||||||
match server.username_pattern {
|
match server.username_pattern {
|
||||||
UsernamePattern::Email => addr.to_string(),
|
UsernamePattern::Email => addr.to_string(),
|
||||||
@@ -543,7 +561,6 @@ impl ConfiguredLoginParam {
|
|||||||
.await?
|
.await?
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
let mail_user = legacy_mail_user.unwrap_or_default();
|
|
||||||
let mail_security: Socket = context
|
let mail_security: Socket = context
|
||||||
.get_config_parsed::<i32>(Config::ConfiguredMailSecurity)
|
.get_config_parsed::<i32>(Config::ConfiguredMailSecurity)
|
||||||
.await?
|
.await?
|
||||||
@@ -558,7 +575,6 @@ impl ConfiguredLoginParam {
|
|||||||
.get_config_parsed::<u16>(Config::ConfiguredSendPort)
|
.get_config_parsed::<u16>(Config::ConfiguredSendPort)
|
||||||
.await?
|
.await?
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let send_user = legacy_send_user.unwrap_or_default();
|
|
||||||
let send_security: Socket = context
|
let send_security: Socket = context
|
||||||
.get_config_parsed::<i32>(Config::ConfiguredSendSecurity)
|
.get_config_parsed::<i32>(Config::ConfiguredSendSecurity)
|
||||||
.await?
|
.await?
|
||||||
@@ -571,7 +587,7 @@ impl ConfiguredLoginParam {
|
|||||||
port: mail_port,
|
port: mail_port,
|
||||||
security: mail_security.try_into()?,
|
security: mail_security.try_into()?,
|
||||||
},
|
},
|
||||||
user: mail_user,
|
user: mail_user.clone(),
|
||||||
}];
|
}];
|
||||||
smtp = vec![ConfiguredServerLoginParam {
|
smtp = vec![ConfiguredServerLoginParam {
|
||||||
connection: ConnectionCandidate {
|
connection: ConnectionCandidate {
|
||||||
@@ -579,7 +595,7 @@ impl ConfiguredLoginParam {
|
|||||||
port: send_port,
|
port: send_port,
|
||||||
security: send_security.try_into()?,
|
security: send_security.try_into()?,
|
||||||
},
|
},
|
||||||
user: send_user,
|
user: send_user.clone(),
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -588,8 +604,10 @@ impl ConfiguredLoginParam {
|
|||||||
Ok(Some(ConfiguredLoginParam {
|
Ok(Some(ConfiguredLoginParam {
|
||||||
addr,
|
addr,
|
||||||
imap,
|
imap,
|
||||||
|
imap_user: mail_user,
|
||||||
imap_password: mail_pw,
|
imap_password: mail_pw,
|
||||||
smtp,
|
smtp,
|
||||||
|
smtp_user: send_user,
|
||||||
smtp_password: send_pw,
|
smtp_password: send_pw,
|
||||||
certificate_checks,
|
certificate_checks,
|
||||||
provider,
|
provider,
|
||||||
@@ -615,9 +633,16 @@ impl ConfiguredLoginParam {
|
|||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
context
|
||||||
|
.set_config(Config::ConfiguredMailUser, Some(&self.imap_user))
|
||||||
|
.await?;
|
||||||
context
|
context
|
||||||
.set_config(Config::ConfiguredMailPw, Some(&self.imap_password))
|
.set_config(Config::ConfiguredMailPw, Some(&self.imap_password))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
context
|
||||||
|
.set_config(Config::ConfiguredSendUser, Some(&self.smtp_user))
|
||||||
|
.await?;
|
||||||
context
|
context
|
||||||
.set_config(Config::ConfiguredSendPw, Some(&self.smtp_password))
|
.set_config(Config::ConfiguredSendPw, Some(&self.smtp_password))
|
||||||
.await?;
|
.await?;
|
||||||
@@ -643,7 +668,6 @@ impl ConfiguredLoginParam {
|
|||||||
context
|
context
|
||||||
.set_config(Config::ConfiguredMailSecurity, None)
|
.set_config(Config::ConfiguredMailSecurity, None)
|
||||||
.await?;
|
.await?;
|
||||||
context.set_config(Config::ConfiguredMailUser, None).await?;
|
|
||||||
context
|
context
|
||||||
.set_config(Config::ConfiguredSendServer, None)
|
.set_config(Config::ConfiguredSendServer, None)
|
||||||
.await?;
|
.await?;
|
||||||
@@ -651,7 +675,6 @@ impl ConfiguredLoginParam {
|
|||||||
context
|
context
|
||||||
.set_config(Config::ConfiguredSendSecurity, None)
|
.set_config(Config::ConfiguredSendSecurity, None)
|
||||||
.await?;
|
.await?;
|
||||||
context.set_config(Config::ConfiguredSendUser, None).await?;
|
|
||||||
|
|
||||||
let server_flags = match self.oauth2 {
|
let server_flags = match self.oauth2 {
|
||||||
true => DC_LP_AUTH_OAUTH2,
|
true => DC_LP_AUTH_OAUTH2,
|
||||||
@@ -748,6 +771,7 @@ mod tests {
|
|||||||
},
|
},
|
||||||
user: "alice".to_string(),
|
user: "alice".to_string(),
|
||||||
}],
|
}],
|
||||||
|
imap_user: "".to_string(),
|
||||||
imap_password: "foo".to_string(),
|
imap_password: "foo".to_string(),
|
||||||
smtp: vec![ConfiguredServerLoginParam {
|
smtp: vec![ConfiguredServerLoginParam {
|
||||||
connection: ConnectionCandidate {
|
connection: ConnectionCandidate {
|
||||||
@@ -757,6 +781,7 @@ mod tests {
|
|||||||
},
|
},
|
||||||
user: "alice@example.org".to_string(),
|
user: "alice@example.org".to_string(),
|
||||||
}],
|
}],
|
||||||
|
smtp_user: "".to_string(),
|
||||||
smtp_password: "bar".to_string(),
|
smtp_password: "bar".to_string(),
|
||||||
// socks5_config is not saved by `save_to_database`, using default value
|
// socks5_config is not saved by `save_to_database`, using default value
|
||||||
socks5_config: None,
|
socks5_config: None,
|
||||||
@@ -840,6 +865,7 @@ mod tests {
|
|||||||
user: user.to_string(),
|
user: user.to_string(),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
imap_user: "alice@posteo.de".to_string(),
|
||||||
imap_password: "foobarbaz".to_string(),
|
imap_password: "foobarbaz".to_string(),
|
||||||
smtp: vec![
|
smtp: vec![
|
||||||
ConfiguredServerLoginParam {
|
ConfiguredServerLoginParam {
|
||||||
@@ -859,6 +885,7 @@ mod tests {
|
|||||||
user: user.to_string(),
|
user: user.to_string(),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
smtp_user: "alice@posteo.de".to_string(),
|
||||||
smtp_password: "foobarbaz".to_string(),
|
smtp_password: "foobarbaz".to_string(),
|
||||||
socks5_config: None,
|
socks5_config: None,
|
||||||
provider: get_provider_by_id("posteo"),
|
provider: get_provider_by_id("posteo"),
|
||||||
|
|||||||
Reference in New Issue
Block a user