Adding unit tests for post and comment report views

cleanup_reporting_api
Dessalines 2021-09-17 13:40:21 -04:00
parent 79dd2dd93f
commit f5b0f77863
10 changed files with 554 additions and 31 deletions

View File

@ -1,4 +1,4 @@
use lemmy_db_schema::{CommentId, CommunityId, LocalUserId, PostId};
use lemmy_db_schema::{CommentId, CommentReportId, CommunityId, LocalUserId, PostId};
use lemmy_db_views::{comment_report_view::CommentReportView, comment_view::CommentView};
use serde::{Deserialize, Serialize};
@ -93,7 +93,7 @@ pub struct CommentReportResponse {
#[derive(Deserialize)]
pub struct ResolveCommentReport {
pub report_id: i32,
pub report_id: CommentReportId,
pub resolved: bool,
pub auth: String,
}

View File

@ -1,4 +1,4 @@
use lemmy_db_schema::{CommunityId, PostId};
use lemmy_db_schema::{CommunityId, PostId, PostReportId};
use lemmy_db_views::{
comment_view::CommentView,
post_report_view::PostReportView,
@ -126,7 +126,7 @@ pub struct PostReportResponse {
#[derive(Deserialize)]
pub struct ResolvePostReport {
pub report_id: i32,
pub report_id: PostReportId,
pub resolved: bool,
pub auth: String,
}

View File

@ -131,13 +131,22 @@ pub trait Readable {
pub trait Reportable {
type Form;
type IdType;
fn report(conn: &PgConnection, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
fn resolve(conn: &PgConnection, report_id: i32, resolver_id: PersonId) -> Result<usize, Error>
fn resolve(
conn: &PgConnection,
report_id: Self::IdType,
resolver_id: PersonId,
) -> Result<usize, Error>
where
Self: Sized;
fn unresolve(conn: &PgConnection, report_id: i32, resolver_id: PersonId) -> Result<usize, Error>
fn unresolve(
conn: &PgConnection,
report_id: Self::IdType,
resolver_id: PersonId,
) -> Result<usize, Error>
where
Self: Sized;
}

View File

@ -3,11 +3,13 @@ use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{
naive_now,
source::comment_report::{CommentReport, CommentReportForm},
CommentReportId,
PersonId,
};
impl Reportable for CommentReport {
type Form = CommentReportForm;
type IdType = CommentReportId;
/// creates a comment report and returns it
///
/// * `conn` - the postgres connection
@ -26,7 +28,7 @@ impl Reportable for CommentReport {
/// * `by_resolver_id` - the id of the user resolving the report
fn resolve(
conn: &PgConnection,
report_id: i32,
report_id: Self::IdType,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
use lemmy_db_schema::schema::comment_report::dsl::*;
@ -46,7 +48,7 @@ impl Reportable for CommentReport {
/// * `by_resolver_id` - the id of the user unresolving the report
fn unresolve(
conn: &PgConnection,
report_id: i32,
report_id: Self::IdType,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
use lemmy_db_schema::schema::comment_report::dsl::*;

View File

@ -1,9 +1,11 @@
use crate::Reportable;
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{naive_now, source::post_report::*, PersonId};
use lemmy_db_schema::{naive_now, source::post_report::*, PersonId, PostReportId};
impl Reportable for PostReport {
type Form = PostReportForm;
type IdType = PostReportId;
/// creates a post report and returns it
///
/// * `conn` - the postgres connection
@ -22,7 +24,7 @@ impl Reportable for PostReport {
/// * `by_resolver_id` - the id of the user resolving the report
fn resolve(
conn: &PgConnection,
report_id: i32,
report_id: Self::IdType,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
use lemmy_db_schema::schema::post_report::dsl::*;
@ -42,7 +44,7 @@ impl Reportable for PostReport {
/// * `by_resolver_id` - the id of the user unresolving the report
fn unresolve(
conn: &PgConnection,
report_id: i32,
report_id: Self::IdType,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
use lemmy_db_schema::schema::post_report::dsl::*;

View File

@ -73,6 +73,12 @@ pub struct PersonBlockId(i32);
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
pub struct CommunityBlockId(i32);
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
pub struct CommentReportId(i32);
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
pub struct PostReportId(i32);
#[repr(transparent)]
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug, AsExpression, FromSqlRow)]
#[sql_type = "Text"]

View File

@ -1,4 +1,10 @@
use crate::{schema::comment_report, source::comment::Comment, CommentId, PersonId};
use crate::{
schema::comment_report,
source::comment::Comment,
CommentId,
CommentReportId,
PersonId,
};
use serde::{Deserialize, Serialize};
#[derive(
@ -7,7 +13,7 @@ use serde::{Deserialize, Serialize};
#[belongs_to(Comment)]
#[table_name = "comment_report"]
pub struct CommentReport {
pub id: i32,
pub id: CommentReportId,
pub creator_id: PersonId,
pub comment_id: CommentId,
pub original_comment_text: String,

View File

@ -1,4 +1,4 @@
use crate::{schema::post_report, source::post::Post, DbUrl, PersonId, PostId};
use crate::{schema::post_report, source::post::Post, DbUrl, PersonId, PostId, PostReportId};
use serde::{Deserialize, Serialize};
#[derive(
@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
#[belongs_to(Post)]
#[table_name = "post_report"]
pub struct PostReport {
pub id: i32,
pub id: PostReportId,
pub creator_id: PersonId,
pub post_id: PostId,
pub original_post_name: String,

View File

@ -18,6 +18,7 @@ use lemmy_db_schema::{
person::{Person, PersonAlias1, PersonAlias2, PersonSafe, PersonSafeAlias1, PersonSafeAlias2},
post::Post,
},
CommentReportId,
CommunityId,
PersonId,
};
@ -48,7 +49,7 @@ impl CommentReportView {
/// returns the CommentReportView for the provided report_id
///
/// * `report_id` - the report id to obtain
pub fn read(conn: &PgConnection, report_id: i32) -> Result<Self, Error> {
pub fn read(conn: &PgConnection, report_id: CommentReportId) -> Result<Self, Error> {
let (comment_report, comment, post, community, creator, comment_creator, resolver) =
comment_report::table
.find(report_id)
@ -123,7 +124,7 @@ pub struct CommentReportQueryBuilder<'a> {
community_id: Option<CommunityId>,
page: Option<i64>,
limit: Option<i64>,
resolved: Option<bool>,
resolved: bool,
}
impl<'a> CommentReportQueryBuilder<'a> {
@ -134,7 +135,7 @@ impl<'a> CommentReportQueryBuilder<'a> {
community_id: None,
page: None,
limit: None,
resolved: Some(false),
resolved: false,
}
}
@ -153,8 +154,8 @@ impl<'a> CommentReportQueryBuilder<'a> {
self
}
pub fn resolved<T: MaybeOptional<bool>>(mut self, resolved: T) -> Self {
self.resolved = resolved.get_optional();
pub fn resolved(mut self, resolved: bool) -> Self {
self.resolved = resolved;
self
}
@ -191,9 +192,7 @@ impl<'a> CommentReportQueryBuilder<'a> {
query = query.filter(post::community_id.eq(community_id));
}
if let Some(resolved_flag) = self.resolved {
query = query.filter(comment_report::resolved.eq(resolved_flag));
}
query = query.filter(comment_report::resolved.eq(self.resolved));
let (limit, offset) = limit_and_offset(self.page, self.limit);
@ -224,3 +223,255 @@ impl ViewToVec for CommentReportView {
.collect::<Vec<Self>>()
}
}
#[cfg(test)]
mod tests {
use crate::comment_report_view::{CommentReportQueryBuilder, CommentReportView};
use lemmy_db_queries::{establish_unpooled_connection, Crud, Joinable, Reportable};
use lemmy_db_schema::source::{comment::*, comment_report::*, community::*, person::*, post::*};
use serial_test::serial;
#[test]
#[serial]
fn test_crud() {
let conn = establish_unpooled_connection();
let new_person = PersonForm {
name: "timmy_crv".into(),
..PersonForm::default()
};
let inserted_timmy = Person::create(&conn, &new_person).unwrap();
let new_person_2 = PersonForm {
name: "sara_crv".into(),
..PersonForm::default()
};
let inserted_sara = Person::create(&conn, &new_person_2).unwrap();
// Add a third person, since new ppl can only report something once.
let new_person_3 = PersonForm {
name: "jessica_crv".into(),
..PersonForm::default()
};
let inserted_jessica = Person::create(&conn, &new_person_3).unwrap();
let new_community = CommunityForm {
name: "test community crv".to_string(),
title: "nada".to_owned(),
..CommunityForm::default()
};
let inserted_community = Community::create(&conn, &new_community).unwrap();
// Make timmy a mod
let timmy_moderator_form = CommunityModeratorForm {
community_id: inserted_community.id,
person_id: inserted_timmy.id,
};
let _inserted_moderator = CommunityModerator::join(&conn, &timmy_moderator_form).unwrap();
let new_post = PostForm {
name: "A test post crv".into(),
creator_id: inserted_timmy.id,
community_id: inserted_community.id,
..PostForm::default()
};
let inserted_post = Post::create(&conn, &new_post).unwrap();
let comment_form = CommentForm {
content: "A test comment 32".into(),
creator_id: inserted_timmy.id,
post_id: inserted_post.id,
..CommentForm::default()
};
let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
// sara reports
let sara_report_form = CommentReportForm {
creator_id: inserted_sara.id,
comment_id: inserted_comment.id,
original_comment_text: "this was it at time of creation".into(),
reason: "from sara".into(),
};
let inserted_sara_report = CommentReport::report(&conn, &sara_report_form).unwrap();
// jessica reports
let jessica_report_form = CommentReportForm {
creator_id: inserted_jessica.id,
comment_id: inserted_comment.id,
original_comment_text: "this was it at time of creation".into(),
reason: "from jessica".into(),
};
let inserted_jessica_report = CommentReport::report(&conn, &jessica_report_form).unwrap();
let read_jessica_report_view =
CommentReportView::read(&conn, inserted_jessica_report.id).unwrap();
let expected_jessica_report_view = CommentReportView {
comment_report: inserted_jessica_report.to_owned(),
comment: inserted_comment,
post: inserted_post,
community: CommunitySafe {
id: inserted_community.id,
name: inserted_community.name,
icon: None,
removed: false,
deleted: false,
nsfw: false,
actor_id: inserted_community.actor_id.to_owned(),
local: true,
title: inserted_community.title,
description: None,
updated: None,
banner: None,
published: inserted_community.published,
},
creator: PersonSafe {
id: inserted_jessica.id,
name: inserted_jessica.name,
display_name: None,
published: inserted_jessica.published,
avatar: None,
actor_id: inserted_jessica.actor_id.to_owned(),
local: true,
banned: false,
deleted: false,
admin: false,
bot_account: false,
bio: None,
banner: None,
updated: None,
inbox_url: inserted_jessica.inbox_url.to_owned(),
shared_inbox_url: None,
matrix_user_id: None,
},
comment_creator: PersonSafeAlias1 {
id: inserted_timmy.id,
name: inserted_timmy.name.to_owned(),
display_name: None,
published: inserted_timmy.published,
avatar: None,
actor_id: inserted_timmy.actor_id.to_owned(),
local: true,
banned: false,
deleted: false,
admin: false,
bot_account: false,
bio: None,
banner: None,
updated: None,
inbox_url: inserted_timmy.inbox_url.to_owned(),
shared_inbox_url: None,
matrix_user_id: None,
},
resolver: None,
};
assert_eq!(read_jessica_report_view, expected_jessica_report_view);
let mut expected_sara_report_view = expected_jessica_report_view.clone();
expected_sara_report_view.comment_report = inserted_sara_report;
expected_sara_report_view.creator = PersonSafe {
id: inserted_sara.id,
name: inserted_sara.name,
display_name: None,
published: inserted_sara.published,
avatar: None,
actor_id: inserted_sara.actor_id.to_owned(),
local: true,
banned: false,
deleted: false,
admin: false,
bot_account: false,
bio: None,
banner: None,
updated: None,
inbox_url: inserted_sara.inbox_url.to_owned(),
shared_inbox_url: None,
matrix_user_id: None,
};
// Do a batch read of timmys reports
let reports = CommentReportQueryBuilder::create(&conn, inserted_timmy.id)
.list()
.unwrap();
assert_eq!(
reports,
[
expected_sara_report_view.to_owned(),
expected_jessica_report_view.to_owned()
]
);
// Make sure the counts are correct
let report_count = CommentReportView::get_report_count(&conn, inserted_timmy.id, None).unwrap();
assert_eq!(2, report_count);
// Try to resolve the report
CommentReport::resolve(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
let read_jessica_report_view_after_resolve =
CommentReportView::read(&conn, inserted_jessica_report.id).unwrap();
let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view.clone();
expected_jessica_report_view_after_resolve
.comment_report
.resolved = true;
expected_jessica_report_view_after_resolve
.comment_report
.resolver_id = Some(inserted_timmy.id);
expected_jessica_report_view_after_resolve
.comment_report
.updated = read_jessica_report_view_after_resolve
.comment_report
.updated;
expected_jessica_report_view_after_resolve.resolver = Some(PersonSafeAlias2 {
id: inserted_timmy.id,
name: inserted_timmy.name.to_owned(),
display_name: None,
published: inserted_timmy.published,
avatar: None,
actor_id: inserted_timmy.actor_id.to_owned(),
local: true,
banned: false,
deleted: false,
admin: false,
bot_account: false,
bio: None,
banner: None,
updated: None,
inbox_url: inserted_timmy.inbox_url.to_owned(),
shared_inbox_url: None,
matrix_user_id: None,
});
assert_eq!(
read_jessica_report_view_after_resolve,
expected_jessica_report_view_after_resolve
);
// Do a batch read of timmys reports
// It should only show saras, which is unresolved
let reports_after_resolve = CommentReportQueryBuilder::create(&conn, inserted_timmy.id)
.list()
.unwrap();
assert_eq!(reports_after_resolve[0], expected_sara_report_view);
// Make sure the counts are correct
let report_count_after_resolved =
CommentReportView::get_report_count(&conn, inserted_timmy.id, None).unwrap();
assert_eq!(1, report_count_after_resolved);
Person::delete(&conn, inserted_timmy.id).unwrap();
Person::delete(&conn, inserted_sara.id).unwrap();
Person::delete(&conn, inserted_jessica.id).unwrap();
Community::delete(&conn, inserted_community.id).unwrap();
}
}

View File

@ -18,6 +18,7 @@ use lemmy_db_schema::{
},
CommunityId,
PersonId,
PostReportId,
};
use serde::Serialize;
@ -44,7 +45,7 @@ impl PostReportView {
/// returns the PostReportView for the provided report_id
///
/// * `report_id` - the report id to obtain
pub fn read(conn: &PgConnection, report_id: i32) -> Result<Self, Error> {
pub fn read(conn: &PgConnection, report_id: PostReportId) -> Result<Self, Error> {
let (post_report, post, community, creator, post_creator, resolver) = post_report::table
.find(report_id)
.inner_join(post::table)
@ -112,7 +113,7 @@ pub struct PostReportQueryBuilder<'a> {
community_id: Option<CommunityId>,
page: Option<i64>,
limit: Option<i64>,
resolved: Option<bool>,
resolved: bool,
}
impl<'a> PostReportQueryBuilder<'a> {
@ -123,7 +124,7 @@ impl<'a> PostReportQueryBuilder<'a> {
community_id: None,
page: None,
limit: None,
resolved: Some(false),
resolved: false,
}
}
@ -142,8 +143,8 @@ impl<'a> PostReportQueryBuilder<'a> {
self
}
pub fn resolved<T: MaybeOptional<bool>>(mut self, resolved: T) -> Self {
self.resolved = resolved.get_optional();
pub fn resolved(mut self, resolved: bool) -> Self {
self.resolved = resolved;
self
}
@ -178,9 +179,7 @@ impl<'a> PostReportQueryBuilder<'a> {
query = query.filter(post::community_id.eq(community_id));
}
if let Some(resolved_flag) = self.resolved {
query = query.filter(post_report::resolved.eq(resolved_flag));
}
query = query.filter(post_report::resolved.eq(self.resolved));
let (limit, offset) = limit_and_offset(self.page, self.limit);
@ -210,3 +209,251 @@ impl ViewToVec for PostReportView {
.collect::<Vec<Self>>()
}
}
#[cfg(test)]
mod tests {
use crate::post_report_view::{PostReportQueryBuilder, PostReportView};
use lemmy_db_queries::{establish_unpooled_connection, Crud, Joinable, Reportable};
use lemmy_db_schema::source::{
community::*,
person::*,
post::*,
post_report::{PostReport, PostReportForm},
};
use serial_test::serial;
#[test]
#[serial]
fn test_crud() {
let conn = establish_unpooled_connection();
let new_person = PersonForm {
name: "timmy_prv".into(),
..PersonForm::default()
};
let inserted_timmy = Person::create(&conn, &new_person).unwrap();
let new_person_2 = PersonForm {
name: "sara_prv".into(),
..PersonForm::default()
};
let inserted_sara = Person::create(&conn, &new_person_2).unwrap();
// Add a third person, since new ppl can only report something once.
let new_person_3 = PersonForm {
name: "jessica_prv".into(),
..PersonForm::default()
};
let inserted_jessica = Person::create(&conn, &new_person_3).unwrap();
let new_community = CommunityForm {
name: "test community prv".to_string(),
title: "nada".to_owned(),
..CommunityForm::default()
};
let inserted_community = Community::create(&conn, &new_community).unwrap();
// Make timmy a mod
let timmy_moderator_form = CommunityModeratorForm {
community_id: inserted_community.id,
person_id: inserted_timmy.id,
};
let _inserted_moderator = CommunityModerator::join(&conn, &timmy_moderator_form).unwrap();
let new_post = PostForm {
name: "A test post crv".into(),
creator_id: inserted_timmy.id,
community_id: inserted_community.id,
..PostForm::default()
};
let inserted_post = Post::create(&conn, &new_post).unwrap();
// sara reports
let sara_report_form = PostReportForm {
creator_id: inserted_sara.id,
post_id: inserted_post.id,
original_post_name: "Orig post".into(),
original_post_url: None,
original_post_body: None,
reason: "from sara".into(),
};
let inserted_sara_report = PostReport::report(&conn, &sara_report_form).unwrap();
// jessica reports
let jessica_report_form = PostReportForm {
creator_id: inserted_jessica.id,
post_id: inserted_post.id,
original_post_name: "Orig post".into(),
original_post_url: None,
original_post_body: None,
reason: "from jessica".into(),
};
let inserted_jessica_report = PostReport::report(&conn, &jessica_report_form).unwrap();
let read_jessica_report_view = PostReportView::read(&conn, inserted_jessica_report.id).unwrap();
let expected_jessica_report_view = PostReportView {
post_report: inserted_jessica_report.to_owned(),
post: inserted_post,
community: CommunitySafe {
id: inserted_community.id,
name: inserted_community.name,
icon: None,
removed: false,
deleted: false,
nsfw: false,
actor_id: inserted_community.actor_id.to_owned(),
local: true,
title: inserted_community.title,
description: None,
updated: None,
banner: None,
published: inserted_community.published,
},
creator: PersonSafe {
id: inserted_jessica.id,
name: inserted_jessica.name,
display_name: None,
published: inserted_jessica.published,
avatar: None,
actor_id: inserted_jessica.actor_id.to_owned(),
local: true,
banned: false,
deleted: false,
admin: false,
bot_account: false,
bio: None,
banner: None,
updated: None,
inbox_url: inserted_jessica.inbox_url.to_owned(),
shared_inbox_url: None,
matrix_user_id: None,
},
post_creator: PersonSafeAlias1 {
id: inserted_timmy.id,
name: inserted_timmy.name.to_owned(),
display_name: None,
published: inserted_timmy.published,
avatar: None,
actor_id: inserted_timmy.actor_id.to_owned(),
local: true,
banned: false,
deleted: false,
admin: false,
bot_account: false,
bio: None,
banner: None,
updated: None,
inbox_url: inserted_timmy.inbox_url.to_owned(),
shared_inbox_url: None,
matrix_user_id: None,
},
resolver: None,
};
assert_eq!(read_jessica_report_view, expected_jessica_report_view);
let mut expected_sara_report_view = expected_jessica_report_view.clone();
expected_sara_report_view.post_report = inserted_sara_report;
expected_sara_report_view.creator = PersonSafe {
id: inserted_sara.id,
name: inserted_sara.name,
display_name: None,
published: inserted_sara.published,
avatar: None,
actor_id: inserted_sara.actor_id.to_owned(),
local: true,
banned: false,
deleted: false,
admin: false,
bot_account: false,
bio: None,
banner: None,
updated: None,
inbox_url: inserted_sara.inbox_url.to_owned(),
shared_inbox_url: None,
matrix_user_id: None,
};
// Do a batch read of timmys reports
let reports = PostReportQueryBuilder::create(&conn, inserted_timmy.id)
.list()
.unwrap();
assert_eq!(
reports,
[
expected_sara_report_view.to_owned(),
expected_jessica_report_view.to_owned()
]
);
// Make sure the counts are correct
let report_count = PostReportView::get_report_count(&conn, inserted_timmy.id, None).unwrap();
assert_eq!(2, report_count);
// Try to resolve the report
PostReport::resolve(&conn, inserted_jessica_report.id, inserted_timmy.id).unwrap();
let read_jessica_report_view_after_resolve =
PostReportView::read(&conn, inserted_jessica_report.id).unwrap();
let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view.clone();
expected_jessica_report_view_after_resolve
.post_report
.resolved = true;
expected_jessica_report_view_after_resolve
.post_report
.resolver_id = Some(inserted_timmy.id);
expected_jessica_report_view_after_resolve
.post_report
.updated = read_jessica_report_view_after_resolve.post_report.updated;
expected_jessica_report_view_after_resolve.resolver = Some(PersonSafeAlias2 {
id: inserted_timmy.id,
name: inserted_timmy.name.to_owned(),
display_name: None,
published: inserted_timmy.published,
avatar: None,
actor_id: inserted_timmy.actor_id.to_owned(),
local: true,
banned: false,
deleted: false,
admin: false,
bot_account: false,
bio: None,
banner: None,
updated: None,
inbox_url: inserted_timmy.inbox_url.to_owned(),
shared_inbox_url: None,
matrix_user_id: None,
});
assert_eq!(
read_jessica_report_view_after_resolve,
expected_jessica_report_view_after_resolve
);
// Do a batch read of timmys reports
// It should only show saras, which is unresolved
let reports_after_resolve = PostReportQueryBuilder::create(&conn, inserted_timmy.id)
.list()
.unwrap();
assert_eq!(reports_after_resolve[0], expected_sara_report_view);
// Make sure the counts are correct
let report_count_after_resolved =
PostReportView::get_report_count(&conn, inserted_timmy.id, None).unwrap();
assert_eq!(1, report_count_after_resolved);
Person::delete(&conn, inserted_timmy.id).unwrap();
Person::delete(&conn, inserted_sara.id).unwrap();
Person::delete(&conn, inserted_jessica.id).unwrap();
Community::delete(&conn, inserted_community.id).unwrap();
}
}