diff --git a/crates/apub/src/api/mod.rs b/crates/apub/src/api/mod.rs index 75ec51043..580be3228 100644 --- a/crates/apub/src/api/mod.rs +++ b/crates/apub/src/api/mod.rs @@ -12,8 +12,6 @@ pub mod read_community; pub mod read_person; pub mod resolve_object; pub mod search; -#[cfg(test)] -pub(crate) mod test; pub mod user_settings_backup; /// Returns default listing type, depending if the query is for frontpage or community. diff --git a/crates/apub/src/api/resolve_object.rs b/crates/apub/src/api/resolve_object.rs index d0b8a9769..d9d50e69e 100644 --- a/crates/apub/src/api/resolve_object.rs +++ b/crates/apub/src/api/resolve_object.rs @@ -72,7 +72,7 @@ async fn convert_response( #[cfg(test)] mod tests { - use crate::api::{resolve_object::resolve_object, test::TestUser}; + use crate::api::resolve_object::resolve_object; use actix_web::web::Query; use lemmy_api_common::{context::LemmyContext, site::ResolveObject}; use lemmy_db_schema::{ @@ -85,6 +85,7 @@ mod tests { }, traits::Crud, }; + use lemmy_db_views::structs::LocalUserView; use lemmy_utils::{error::LemmyResult, LemmyErrorType}; use serial_test::serial; @@ -95,14 +96,12 @@ mod tests { let context = LemmyContext::init_test_context().await; let pool = &mut context.pool(); - let creator = TestUser::default().create(pool).await?; - let regular_user = TestUser::default().create(pool).await?; - let admin_user = TestUser { - admin: true, - ..Default::default() - } - .create(pool) - .await?; + let name = "test_local_user_name"; + let bio = "test_local_user_bio"; + + let creator = LocalUserView::create_test_user(pool, name, bio, false).await?; + let regular_user = LocalUserView::create_test_user(pool, name, bio, false).await?; + let admin_user = LocalUserView::create_test_user(pool, name, bio, true).await?; let instance_id = creator.person.instance_id; let site_form = SiteInsertForm::new("test site".to_string(), instance_id); diff --git a/crates/apub/src/api/test.rs b/crates/apub/src/api/test.rs deleted file mode 100644 index 55ce6bab3..000000000 --- a/crates/apub/src/api/test.rs +++ /dev/null @@ -1,44 +0,0 @@ -use lemmy_db_schema::{ - source::{ - instance::Instance, - local_user::{LocalUser, LocalUserInsertForm}, - person::{Person, PersonInsertForm}, - }, - traits::Crud, - utils::DbPool, -}; -use lemmy_db_views::structs::LocalUserView; -use lemmy_utils::error::LemmyResult; - -#[derive(Default)] -pub struct TestUser { - pub name: Option<&'static str>, - pub bio: Option<&'static str>, - pub admin: bool, -} - -impl TestUser { - pub async fn create(self, pool: &mut DbPool<'_>) -> LemmyResult { - let instance_id = Instance::read_or_create(pool, "example.com".to_string()) - .await? - .id; - let name = self - .name - .map_or_else(|| uuid::Uuid::new_v4().to_string(), ToString::to_string); - - let person_form = PersonInsertForm { - display_name: Some(name.clone()), - bio: self.bio.map(ToString::to_string), - ..PersonInsertForm::test_form(instance_id, &name) - }; - let person = Person::create(pool, &person_form).await?; - - let user_form = match self.admin { - true => LocalUserInsertForm::test_form_admin(person.id), - false => LocalUserInsertForm::test_form(person.id), - }; - let local_user = LocalUser::create(pool, &user_form, vec![]).await?; - - Ok(LocalUserView::read(pool, local_user.id).await?) - } -} diff --git a/crates/apub/src/api/user_settings_backup.rs b/crates/apub/src/api/user_settings_backup.rs index 50017df9e..2e075c202 100644 --- a/crates/apub/src/api/user_settings_backup.rs +++ b/crates/apub/src/api/user_settings_backup.rs @@ -314,10 +314,7 @@ where #[cfg(test)] #[expect(clippy::indexing_slicing)] pub(crate) mod tests { - use crate::api::{ - test::TestUser, - user_settings_backup::{export_settings, import_settings}, - }; + use crate::api::user_settings_backup::{export_settings, import_settings}; use actix_web::web::Json; use lemmy_api_common::context::LemmyContext; use lemmy_db_schema::{ @@ -341,13 +338,7 @@ pub(crate) mod tests { let context = LemmyContext::init_test_context().await; let pool = &mut context.pool(); - let export_user = TestUser { - name: "hanna".into(), - bio: "my bio".into(), - ..Default::default() - } - .create(pool) - .await?; + let export_user = LocalUserView::create_test_user(pool, "hanna", "my bio", false).await?; let community_form = CommunityInsertForm::new( export_user.person.instance_id, @@ -365,12 +356,8 @@ pub(crate) mod tests { let backup = export_settings(export_user.clone(), context.reset_request_count()).await?; - let import_user = TestUser { - name: "charles".into(), - ..Default::default() - } - .create(pool) - .await?; + let import_user = + LocalUserView::create_test_user(pool, "charles", "charles bio", false).await?; import_settings(backup, import_user.clone(), context.reset_request_count()).await?; @@ -400,12 +387,7 @@ pub(crate) mod tests { let context = LemmyContext::init_test_context().await; let pool = &mut context.pool(); - let export_user = TestUser { - bio: "my bio".into(), - ..Default::default() - } - .create(pool) - .await?; + let export_user = LocalUserView::create_test_user(pool, "harry", "harry bio", false).await?; let mut backup = export_settings(export_user.clone(), context.reset_request_count()).await?; @@ -420,7 +402,7 @@ pub(crate) mod tests { backup.saved_comments.push("http://example4.com".parse()?); } - let import_user = TestUser::default().create(pool).await?; + let import_user = LocalUserView::create_test_user(pool, "sally", "sally bio", false).await?; let imported = import_settings(backup, import_user.clone(), context.reset_request_count()).await; @@ -441,12 +423,7 @@ pub(crate) mod tests { let context = LemmyContext::init_test_context().await; let pool = &mut context.pool(); - let import_user = TestUser { - bio: "my bio".into(), - ..Default::default() - } - .create(pool) - .await?; + let import_user = LocalUserView::create_test_user(pool, "larry", "larry bio", false).await?; let backup = serde_json::from_str("{\"bot_account\": true, \"settings\": {\"theme\": \"my_theme\"}}")?; diff --git a/crates/apub/src/fetcher/markdown_links.rs b/crates/apub/src/fetcher/markdown_links.rs index 009b333c0..d83aae515 100644 --- a/crates/apub/src/fetcher/markdown_links.rs +++ b/crates/apub/src/fetcher/markdown_links.rs @@ -104,7 +104,6 @@ async fn format_actor_url( #[cfg(test)] mod tests { use super::*; - use crate::api::test::TestUser; use lemmy_db_schema::{ source::{ community::{Community, CommunityInsertForm}, @@ -112,6 +111,7 @@ mod tests { }, traits::Crud, }; + use lemmy_db_views::structs::LocalUserView; use pretty_assertions::assert_eq; use serial_test::serial; @@ -130,7 +130,8 @@ mod tests { ), ) .await?; - let user = TestUser::default().create(&mut context.pool()).await?; + let user = + LocalUserView::create_test_user(&mut context.pool(), "garda", "garda bio", false).await?; // insert a remote post which is already fetched let post_form = PostInsertForm { diff --git a/crates/db_views/src/local_user_view.rs b/crates/db_views/src/local_user_view.rs index b8ae14d5e..8d55b96fe 100644 --- a/crates/db_views/src/local_user_view.rs +++ b/crates/db_views/src/local_user_view.rs @@ -5,6 +5,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::{LocalUserId, OAuthProviderId, PersonId}, schema::{local_user, local_user_vote_display_mode, oauth_account, person, person_aggregates}, + source::{ + instance::Instance, + local_user::{LocalUser, LocalUserInsertForm}, + person::{Person, PersonInsertForm}, + }, + traits::Crud, utils::{ functions::{coalesce, lower}, DbConn, @@ -134,6 +140,31 @@ impl LocalUserView { pub async fn list_admins_with_emails(pool: &mut DbPool<'_>) -> Result, Error> { queries().list(pool, ListMode::AdminsWithEmails).await } + + pub async fn create_test_user( + pool: &mut DbPool<'_>, + name: &str, + bio: &str, + admin: bool, + ) -> Result { + let instance_id = Instance::read_or_create(pool, "example.com".to_string()) + .await? + .id; + let person_form = PersonInsertForm { + display_name: Some(name.to_owned()), + bio: Some(bio.to_owned()), + ..PersonInsertForm::test_form(instance_id, name) + }; + let person = Person::create(pool, &person_form).await?; + + let user_form = match admin { + true => LocalUserInsertForm::test_form_admin(person.id), + false => LocalUserInsertForm::test_form(person.id), + }; + let local_user = LocalUser::create(pool, &user_form, vec![]).await?; + + LocalUserView::read(pool, local_user.id).await + } } impl FromRequest for LocalUserView {