From fa4288a7049be9fe8c70543f68786b55ea0c5a86 Mon Sep 17 00:00:00 2001 From: Karcsesz Date: Sat, 24 Feb 2024 12:19:22 +0100 Subject: [PATCH] Skip pushing a new primary subject if it's the same as the current one. Also adds additional tests to resource.rs --- src/schema/resource.rs | 54 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/schema/resource.rs b/src/schema/resource.rs index 3fac470..6957dda 100644 --- a/src/schema/resource.rs +++ b/src/schema/resource.rs @@ -45,6 +45,10 @@ impl Resource { } pub fn add_new_primary_subject(&mut self, mut subject: String) { + if subject == self.subject { + debug!("New and old subjects match, skipping..."); + return; + } debug!("Swapping new and old subject"); std::mem::swap(&mut subject, &mut self.subject); debug!("Pushing current subject into aliases"); @@ -95,6 +99,15 @@ pub mod test_data { links: None, } } + + pub fn user_with_matching_subject_and_alias() -> Resource { + Resource { + subject: "acct:user@domain.tld".to_string(), + aliases: Some(vec!["acct:user@domain.tld".to_string()]), + properties: None, + links: None, + } + } } #[cfg(test)] @@ -115,4 +128,45 @@ mod tests { } ) } + + #[test] + fn insert_new_primary_subject_into_new_user() { + let mut data = Resource::new("testing_subject".to_string()); + data.add_new_primary_subject("new_subject".to_string()); + assert_eq!( + data, + Resource { + subject: "new_subject".to_string(), + aliases: Some(vec!["testing_subject".to_string()]), + properties: None, + links: None + } + ) + } + + #[test] + fn insert_new_primary_subject_into_new_user_multiple_times() { + let mut data = Resource::new("testing_subject".to_string()); + for _ in 0..10 { + data.add_new_primary_subject("new_subject".to_string()) + } + assert_eq!( + data, + Resource { + subject: "new_subject".to_string(), + aliases: Some(vec!["testing_subject".to_string()]), + properties: None, + links: None + } + ) + } + + #[test] + fn check_preventing_alias_deduplication() { + let mut data = test_data::user_with_matching_subject_and_alias(); + data.add_new_primary_subject("new_subject".to_string()); + let mut check = test_data::user_with_matching_subject_and_alias(); + check.subject = "new_subject".to_string(); + assert_eq!(data, check) + } }