address latest review comments, move test to implementation file

This commit is contained in:
holger krekel
2022-04-16 15:42:22 +02:00
parent 3b6fc9959f
commit a8342e37b9
2 changed files with 61 additions and 59 deletions

View File

@@ -336,7 +336,7 @@ impl Context {
// Separate impl block for self address handling
impl Context {
/// 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();
// 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
/// primary address (if exists) as a secondary address.
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:
let mut secondary_new = self.get_all_self_addrs().await?;
// Remove the new primary addr in case it was a secondary addr before:
secondary_new.retain(|a| a != &primary_new);
// add old primary address (if exists) to secondary addresses
let mut secondary_addrs = self.get_all_self_addrs().await?;
// never store a primary address also as a secondary
secondary_addrs.retain(|a| a != &primary_new);
self.set_config(
Config::SecondaryAddrs,
Some(secondary_new.join(" ").as_str()),
Some(secondary_addrs.join(" ").as_str()),
)
.await?;
@@ -368,17 +368,17 @@ impl Context {
}
/// 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();
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());
Ok(ret)
}
/// 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
.get_config(Config::SecondaryAddrs)
.await?
@@ -481,4 +481,54 @@ mod tests {
assert_eq!(t.get_config_bool(c).await?, false);
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(())
}
}

View File

@@ -720,10 +720,7 @@ impl Contact {
.await?;
if let Some(query) = query {
let self_addr = context
.get_config(Config::ConfiguredAddr)
.await?
.unwrap_or_default();
let self_addr = context.get_primary_self_addr().await.unwrap_or_default();
let self_name = context
.get_config(Config::Displayname)
.await?
@@ -2130,49 +2127,4 @@ Hi."#;
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(())
}
}