mirror of
https://github.com/chatmail/core.git
synced 2026-05-20 15:26:30 +03:00
address latest review comments, move test to implementation file
This commit is contained in:
@@ -336,7 +336,7 @@ impl Context {
|
|||||||
// Separate impl block for self address handling
|
// Separate impl block for self address handling
|
||||||
impl Context {
|
impl Context {
|
||||||
/// determine whether the specified addr maps to the/a self addr
|
/// determine whether the specified addr maps to the/a self addr
|
||||||
pub async fn is_self_addr(&self, addr: &str) -> Result<bool> {
|
pub(crate) async fn is_self_addr(&self, addr: &str) -> Result<bool> {
|
||||||
let addr = addr_normalize(addr).to_lowercase();
|
let addr = addr_normalize(addr).to_lowercase();
|
||||||
|
|
||||||
// The addresses we get here are already normalized and lowercase TODO is this true?
|
// The addresses we get here are already normalized and lowercase TODO is this true?
|
||||||
@@ -349,15 +349,15 @@ impl Context {
|
|||||||
/// Sets `primary_new` as the new primary self address and saves the old
|
/// Sets `primary_new` as the new primary self address and saves the old
|
||||||
/// primary address (if exists) as a secondary address.
|
/// primary address (if exists) as a secondary address.
|
||||||
pub(crate) async fn set_primary_self_addr(&self, primary_new: &str) -> Result<()> {
|
pub(crate) async fn set_primary_self_addr(&self, primary_new: &str) -> Result<()> {
|
||||||
let primary_new = addr_normalize(primary_new).to_lowercase(); // TODO check if this might make problems
|
let primary_new = addr_normalize(primary_new).to_lowercase();
|
||||||
|
|
||||||
// Get all self addrs, including the old primary addr:
|
// add old primary address (if exists) to secondary addresses
|
||||||
let mut secondary_new = self.get_all_self_addrs().await?;
|
let mut secondary_addrs = self.get_all_self_addrs().await?;
|
||||||
// Remove the new primary addr in case it was a secondary addr before:
|
// never store a primary address also as a secondary
|
||||||
secondary_new.retain(|a| a != &primary_new);
|
secondary_addrs.retain(|a| a != &primary_new);
|
||||||
self.set_config(
|
self.set_config(
|
||||||
Config::SecondaryAddrs,
|
Config::SecondaryAddrs,
|
||||||
Some(secondary_new.join(" ").as_str()),
|
Some(secondary_addrs.join(" ").as_str()),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -368,17 +368,17 @@ impl Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns all primary and secondary self addresses.
|
/// Returns all primary and secondary self addresses.
|
||||||
pub async fn get_all_self_addrs(&self) -> Result<Vec<String>> {
|
pub(crate) async fn get_all_self_addrs(&self) -> Result<Vec<String>> {
|
||||||
let mut ret = Vec::new();
|
let mut ret = Vec::new();
|
||||||
|
|
||||||
ret.extend(self.get_config(Config::ConfiguredAddr).await?.into_iter());
|
ret.push(self.get_primary_self_addr().await.unwrap_or_default());
|
||||||
ret.extend(self.get_secondary_self_addrs().await?.into_iter());
|
ret.extend(self.get_secondary_self_addrs().await?.into_iter());
|
||||||
|
|
||||||
Ok(ret)
|
Ok(ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns all secondary self addresses.
|
/// Returns all secondary self addresses.
|
||||||
pub async fn get_secondary_self_addrs(&self) -> Result<Vec<String>> {
|
pub(crate) async fn get_secondary_self_addrs(&self) -> Result<Vec<String>> {
|
||||||
let secondary_addrs = self
|
let secondary_addrs = self
|
||||||
.get_config(Config::SecondaryAddrs)
|
.get_config(Config::SecondaryAddrs)
|
||||||
.await?
|
.await?
|
||||||
@@ -481,4 +481,54 @@ mod tests {
|
|||||||
assert_eq!(t.get_config_bool(c).await?, false);
|
assert_eq!(t.get_config_bool(c).await?, false);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[async_std::test]
|
||||||
|
async fn test_self_addrs() -> Result<()> {
|
||||||
|
let alice = TestContext::new_alice().await;
|
||||||
|
|
||||||
|
assert!(alice.is_self_addr("alice@example.org").await?);
|
||||||
|
assert_eq!(alice.get_all_self_addrs().await?, vec!["alice@example.org"]);
|
||||||
|
assert!(!alice.is_self_addr("alice@alice.com").await?);
|
||||||
|
|
||||||
|
// Test adding the same primary address
|
||||||
|
alice.set_primary_self_addr("alice@example.org").await?;
|
||||||
|
alice.set_primary_self_addr("Alice@Example.Org").await?;
|
||||||
|
assert_eq!(alice.get_all_self_addrs().await?, vec!["alice@example.org"]);
|
||||||
|
|
||||||
|
// Test adding a new (primary) self address
|
||||||
|
alice.set_primary_self_addr(" Alice@alice.com ").await?;
|
||||||
|
assert!(alice.is_self_addr("aliCe@example.org").await?);
|
||||||
|
assert!(alice.is_self_addr("alice@alice.com").await?);
|
||||||
|
assert_eq!(
|
||||||
|
alice.get_all_self_addrs().await?,
|
||||||
|
vec!["alice@alice.com", "alice@example.org"]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Check that the entry is not duplicated
|
||||||
|
alice.set_primary_self_addr("alice@alice.com").await?;
|
||||||
|
assert_eq!(
|
||||||
|
alice.get_all_self_addrs().await?,
|
||||||
|
vec!["alice@alice.com", "alice@example.org"]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Test switching back
|
||||||
|
alice.set_primary_self_addr("alice@example.org").await?;
|
||||||
|
assert_eq!(
|
||||||
|
alice.get_all_self_addrs().await?,
|
||||||
|
vec!["alice@example.org", "alice@alice.com"]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Test setting a new primary self address, the previous self address
|
||||||
|
// should be kept as a secondary self address
|
||||||
|
alice.set_primary_self_addr("alice@alice.xyz").await?;
|
||||||
|
assert_eq!(
|
||||||
|
alice.get_all_self_addrs().await?,
|
||||||
|
vec!["alice@alice.xyz", "alice@example.org", "alice@alice.com"]
|
||||||
|
);
|
||||||
|
assert!(alice.is_self_addr("alice@example.org").await?);
|
||||||
|
assert!(alice.is_self_addr("alice@alice.com").await?);
|
||||||
|
assert!(alice.is_self_addr("Alice@alice.xyz").await?);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -720,10 +720,7 @@ impl Contact {
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
if let Some(query) = query {
|
if let Some(query) = query {
|
||||||
let self_addr = context
|
let self_addr = context.get_primary_self_addr().await.unwrap_or_default();
|
||||||
.get_config(Config::ConfiguredAddr)
|
|
||||||
.await?
|
|
||||||
.unwrap_or_default();
|
|
||||||
let self_name = context
|
let self_name = context
|
||||||
.get_config(Config::Displayname)
|
.get_config(Config::Displayname)
|
||||||
.await?
|
.await?
|
||||||
@@ -2130,49 +2127,4 @@ Hi."#;
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_std::test]
|
|
||||||
async fn test_self_addrs() -> Result<()> {
|
|
||||||
let alice = TestContext::new_alice().await;
|
|
||||||
|
|
||||||
assert!(alice.is_self_addr("alice@example.org").await?);
|
|
||||||
assert_eq!(alice.get_all_self_addrs().await?, vec!["alice@example.org"]);
|
|
||||||
assert!(!alice.is_self_addr("alice@alice.com").await?);
|
|
||||||
|
|
||||||
// Test adding a new (primary) self address
|
|
||||||
alice.set_primary_self_addr(" Alice@alice.com ").await?;
|
|
||||||
assert!(alice.is_self_addr("aliCe@example.org").await?);
|
|
||||||
assert!(alice.is_self_addr("alice@alice.com").await?);
|
|
||||||
assert_eq!(
|
|
||||||
alice.get_all_self_addrs().await?,
|
|
||||||
vec!["alice@alice.com", "alice@example.org"]
|
|
||||||
);
|
|
||||||
|
|
||||||
// Check that the entry is not duplicated
|
|
||||||
alice.set_primary_self_addr("alice@alice.com").await?;
|
|
||||||
assert_eq!(
|
|
||||||
alice.get_all_self_addrs().await?,
|
|
||||||
vec!["alice@alice.com", "alice@example.org"]
|
|
||||||
);
|
|
||||||
|
|
||||||
// Test switching back
|
|
||||||
alice.set_primary_self_addr("alice@example.org").await?;
|
|
||||||
assert_eq!(
|
|
||||||
alice.get_all_self_addrs().await?,
|
|
||||||
vec!["alice@example.org", "alice@alice.com"]
|
|
||||||
);
|
|
||||||
|
|
||||||
// Test setting a new primary self address, the previous self address
|
|
||||||
// should be kept as a secondary self address
|
|
||||||
alice.set_primary_self_addr("alice@alice.xyz").await?;
|
|
||||||
assert_eq!(
|
|
||||||
alice.get_all_self_addrs().await?,
|
|
||||||
vec!["alice@alice.xyz", "alice@example.org", "alice@alice.com"]
|
|
||||||
);
|
|
||||||
assert!(alice.is_self_addr("alice@example.org").await?);
|
|
||||||
assert!(alice.is_self_addr("alice@alice.com").await?);
|
|
||||||
assert!(alice.is_self_addr("Alice@alice.xyz").await?);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user