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) + } }