Skip pushing a new primary subject if it's the same as the current one.

Also adds additional tests to resource.rs
This commit is contained in:
Karcsesz 2024-02-24 12:19:22 +01:00
parent 2cf8786e34
commit fa4288a704

View file

@ -45,6 +45,10 @@ impl Resource {
} }
pub fn add_new_primary_subject(&mut self, mut subject: String) { 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"); debug!("Swapping new and old subject");
std::mem::swap(&mut subject, &mut self.subject); std::mem::swap(&mut subject, &mut self.subject);
debug!("Pushing current subject into aliases"); debug!("Pushing current subject into aliases");
@ -95,6 +99,15 @@ pub mod test_data {
links: None, 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)] #[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)
}
} }