diff --git a/crates/db_schema/src/impls/federation_allowlist.rs b/crates/db_schema/src/impls/federation_allowlist.rs index b448ba24e..1e2139df1 100644 --- a/crates/db_schema/src/impls/federation_allowlist.rs +++ b/crates/db_schema/src/impls/federation_allowlist.rs @@ -46,3 +46,61 @@ impl AdminAllowInstance { Ok(()) } } + +#[cfg(test)] +mod tests { + + use super::*; + use crate::{ + source::{ + instance::Instance, + person::{Person, PersonInsertForm}, + }, + traits::Crud, + utils::build_db_pool_for_tests, + }; + use pretty_assertions::assert_eq; + use serial_test::serial; + + #[tokio::test] + #[serial] + async fn test_allowlist_insert_and_clear() -> Result<(), Error> { + let pool = &build_db_pool_for_tests(); + let pool = &mut pool.into(); + let instances = vec![ + Instance::read_or_create(pool, "tld1.xyz".to_string()).await?, + Instance::read_or_create(pool, "tld2.xyz".to_string()).await?, + Instance::read_or_create(pool, "tld3.xyz".to_string()).await?, + ]; + let new_person_3 = PersonInsertForm::test_form(instances[0].id, "xyz"); + let person = Person::create(pool, &new_person_3).await?; + let forms: Vec<_> = instances + .iter() + .map(|i| AdminAllowInstanceForm { + instance_id: i.id, + admin_person_id: person.id, + reason: None, + }) + .collect(); + + for f in &forms { + AdminAllowInstance::allow(pool, &f).await?; + } + + let allows = Instance::allowlist(pool).await?; + + assert_eq!(3, allows.len()); + assert_eq!(instances, allows); + + // Now test clearing them + for f in forms { + AdminAllowInstance::unallow(pool, f.instance_id).await?; + } + let allows = Instance::allowlist(pool).await?; + assert_eq!(0, allows.len()); + + Instance::delete_all(pool).await?; + + Ok(()) + } +}