2 schema::federation_allowlist,
4 federation_allowlist::{FederationAllowList, FederationAllowListForm},
7 utils::{get_conn, DbPool},
9 use diesel::{dsl::insert_into, result::Error};
10 use diesel_async::{AsyncPgConnection, RunQueryDsl};
12 impl FederationAllowList {
13 pub async fn replace(pool: &DbPool, list_opt: Option<Vec<String>>) -> Result<(), Error> {
14 let conn = &mut get_conn(pool).await?;
19 if let Some(list) = list_opt {
20 Self::clear(conn).await?;
23 // Upsert all of these as instances
24 let instance = Instance::create_conn(conn, &domain).await?;
26 let form = FederationAllowListForm {
27 instance_id: instance.id,
30 insert_into(federation_allowlist::table)
32 .get_result::<Self>(conn)
44 async fn clear(conn: &mut AsyncPgConnection) -> Result<usize, Error> {
45 diesel::delete(federation_allowlist::table)
53 source::{federation_allowlist::FederationAllowList, instance::Instance},
54 utils::build_db_pool_for_tests,
56 use serial_test::serial;
60 async fn test_allowlist_insert_and_clear() {
61 let pool = &build_db_pool_for_tests().await;
62 let allowed = Some(vec![
63 "tld1.xyz".to_string(),
64 "tld2.xyz".to_string(),
65 "tld3.xyz".to_string(),
68 FederationAllowList::replace(pool, allowed).await.unwrap();
70 let allows = Instance::allowlist(pool).await.unwrap();
72 assert_eq!(3, allows.len());
75 "tld1.xyz".to_string(),
76 "tld2.xyz".to_string(),
77 "tld3.xyz".to_string()
82 // Now test clearing them via Some(empty vec)
83 let clear_allows = Some(Vec::new());
85 FederationAllowList::replace(pool, clear_allows)
88 let allows = Instance::allowlist(pool).await.unwrap();
90 assert_eq!(0, allows.len());
92 Instance::delete_all(pool).await.unwrap();