mirror of
https://github.com/LemmyNet/lemmy.git
synced 2025-02-28 00:27:10 +00:00
start adding api tests
This commit is contained in:
parent
760027a5fc
commit
d206e8acfb
9 changed files with 111 additions and 73 deletions
|
@ -298,28 +298,43 @@ test('A and G subscribe to B (center) A posts, it gets announced to G', async ()
|
||||||
|
|
||||||
test('Enforce site ban for federated user', async () => {
|
test('Enforce site ban for federated user', async () => {
|
||||||
let alphaShortname = `@lemmy_alpha@lemmy-alpha:8541`;
|
let alphaShortname = `@lemmy_alpha@lemmy-alpha:8541`;
|
||||||
let alphaPerson = (await resolvePerson(beta, alphaShortname)).person;
|
let alphaPerson = (await resolvePerson(alpha, alphaShortname)).person;
|
||||||
expect(alphaPerson).toBeDefined();
|
expect(alphaPerson).toBeDefined();
|
||||||
|
|
||||||
// ban alpha from beta site
|
let postRes1 = await createPost(alpha, betaCommunity.community.id);
|
||||||
let banAlpha = await banPersonFromSite(beta, alphaPerson.person.id, true);
|
let searchBeta1 = await searchPostLocal(beta, postRes1.post_view.post);
|
||||||
|
expect(searchBeta1.posts[0]).toBeDefined();
|
||||||
|
|
||||||
|
// ban alpha from site
|
||||||
|
let banAlpha = await banPersonFromSite(alpha, alphaPerson.person.id, true, true);
|
||||||
expect(banAlpha.banned).toBe(true);
|
expect(banAlpha.banned).toBe(true);
|
||||||
|
|
||||||
// Alpha makes post on beta
|
// alpha ban should be federated to beta
|
||||||
let postRes = await createPost(alpha, betaCommunity.community.id);
|
let alphaUserOnBeta1 = await resolvePerson(beta, alphaPerson.person.actor_id);
|
||||||
expect(postRes.post_view.post).toBeDefined();
|
console.debug(alphaUserOnBeta1);
|
||||||
expect(postRes.post_view.community.local).toBe(false);
|
expect(alphaUserOnBeta1.person.person.banned).toBe(true);
|
||||||
expect(postRes.post_view.creator.local).toBe(true);
|
|
||||||
expect(postRes.post_view.counts.score).toBe(1);
|
// post should be removed
|
||||||
|
let searchBeta2 = await searchPostLocal(beta, postRes1.post_view.post);
|
||||||
|
expect(searchBeta2.posts[0]).toBeUndefined();
|
||||||
|
|
||||||
|
// Alpha makes new post on beta
|
||||||
|
let postRes2 = await createPost(alpha, betaCommunity.community.id);
|
||||||
|
expect(postRes2.post_view.post).toBeDefined();
|
||||||
|
expect(postRes2.post_view.community.local).toBe(false);
|
||||||
|
expect(postRes2.post_view.creator.local).toBe(true);
|
||||||
|
expect(postRes2.post_view.counts.score).toBe(1);
|
||||||
|
|
||||||
// Make sure that post doesn't make it to beta
|
// Make sure that post doesn't make it to beta
|
||||||
let searchBeta = await searchPostLocal(beta, postRes.post_view.post);
|
let searchBeta = await searchPostLocal(beta, postRes2.post_view.post);
|
||||||
let betaPost = searchBeta.posts[0];
|
expect(searchBeta.posts[0]).toBeUndefined();
|
||||||
expect(betaPost).toBeUndefined();
|
|
||||||
|
|
||||||
// Unban alpha
|
// Unban alpha
|
||||||
let unBanAlpha = await banPersonFromSite(beta, alphaPerson.person.id, false);
|
let unBanAlpha = await banPersonFromSite(alpha, alphaPerson.person.id, false, false);
|
||||||
expect(unBanAlpha.banned).toBe(false);
|
expect(unBanAlpha.banned).toBe(false);
|
||||||
|
|
||||||
|
let alphaUserOnBeta2 = await resolvePerson(beta, alphaPerson.person.actor_id)
|
||||||
|
expect(alphaUserOnBeta2.person.person.banned).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Enforce community ban for federated user', async () => {
|
test('Enforce community ban for federated user', async () => {
|
||||||
|
@ -327,33 +342,41 @@ test('Enforce community ban for federated user', async () => {
|
||||||
let alphaPerson = (await resolvePerson(beta, alphaShortname)).person;
|
let alphaPerson = (await resolvePerson(beta, alphaShortname)).person;
|
||||||
expect(alphaPerson).toBeDefined();
|
expect(alphaPerson).toBeDefined();
|
||||||
|
|
||||||
// ban alpha from beta site
|
// make a post in beta, it goes through
|
||||||
await banPersonFromCommunity(beta, alphaPerson.person.id, 2, false);
|
let postRes1 = await createPost(alpha, betaCommunity.community.id);
|
||||||
let banAlpha = await banPersonFromCommunity(beta, alphaPerson.person.id, 2, true);
|
let searchBeta1 = await searchPostLocal(beta, postRes1.post_view.post);
|
||||||
|
expect(searchBeta1.posts[0]).toBeDefined();
|
||||||
|
|
||||||
|
// ban alpha from beta community
|
||||||
|
let banAlpha = await banPersonFromCommunity(beta, alphaPerson.person.id, 2, true, true);
|
||||||
expect(banAlpha.banned).toBe(true);
|
expect(banAlpha.banned).toBe(true);
|
||||||
|
|
||||||
|
// ensure that the post by alpha got removed
|
||||||
|
let searchAlpha1 = await searchPostLocal(alpha, postRes1.post_view.post);
|
||||||
|
expect(searchAlpha1.posts[0]).toBeUndefined();
|
||||||
|
|
||||||
// Alpha tries to make post on beta, but it fails because of ban
|
// Alpha tries to make post on beta, but it fails because of ban
|
||||||
let postRes = await createPost(alpha, betaCommunity.community.id);
|
let postRes2 = await createPost(alpha, betaCommunity.community.id);
|
||||||
expect(postRes.post_view).toBeUndefined();
|
expect(postRes2.post_view).toBeUndefined();
|
||||||
|
|
||||||
// Unban alpha
|
// Unban alpha
|
||||||
let unBanAlpha = await banPersonFromCommunity(
|
let unBanAlpha = await banPersonFromCommunity(
|
||||||
beta,
|
beta,
|
||||||
alphaPerson.person.id,
|
alphaPerson.person.id,
|
||||||
2,
|
2,
|
||||||
|
false,
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
expect(unBanAlpha.banned).toBe(false);
|
expect(unBanAlpha.banned).toBe(false);
|
||||||
let postRes2 = await createPost(alpha, betaCommunity.community.id);
|
let postRes3 = await createPost(alpha, betaCommunity.community.id);
|
||||||
expect(postRes2.post_view.post).toBeDefined();
|
expect(postRes3.post_view.post).toBeDefined();
|
||||||
expect(postRes2.post_view.community.local).toBe(false);
|
expect(postRes3.post_view.community.local).toBe(false);
|
||||||
expect(postRes2.post_view.creator.local).toBe(true);
|
expect(postRes3.post_view.creator.local).toBe(true);
|
||||||
expect(postRes2.post_view.counts.score).toBe(1);
|
expect(postRes3.post_view.counts.score).toBe(1);
|
||||||
|
|
||||||
// Make sure that post makes it to beta community
|
// Make sure that post makes it to beta community
|
||||||
let searchBeta = await searchPostLocal(beta, postRes2.post_view.post);
|
let searchBeta2 = await searchPostLocal(beta, postRes3.post_view.post);
|
||||||
let betaPost = searchBeta.posts[0];
|
expect(searchBeta2.posts[0]).toBeDefined();
|
||||||
expect(betaPost).toBeDefined();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Report a post', async () => {
|
test('Report a post', async () => {
|
||||||
|
|
|
@ -66,23 +66,23 @@ export interface API {
|
||||||
}
|
}
|
||||||
|
|
||||||
export let alpha: API = {
|
export let alpha: API = {
|
||||||
client: new LemmyHttp('http://localhost:8541'),
|
client: new LemmyHttp('http://127.0.0.1:8541'),
|
||||||
};
|
};
|
||||||
|
|
||||||
export let beta: API = {
|
export let beta: API = {
|
||||||
client: new LemmyHttp('http://localhost:8551'),
|
client: new LemmyHttp('http://127.0.0.1:8551'),
|
||||||
};
|
};
|
||||||
|
|
||||||
export let gamma: API = {
|
export let gamma: API = {
|
||||||
client: new LemmyHttp('http://localhost:8561'),
|
client: new LemmyHttp('http://127.0.0.1:8561'),
|
||||||
};
|
};
|
||||||
|
|
||||||
export let delta: API = {
|
export let delta: API = {
|
||||||
client: new LemmyHttp('http://localhost:8571'),
|
client: new LemmyHttp('http://127.0.0.1:8571'),
|
||||||
};
|
};
|
||||||
|
|
||||||
export let epsilon: API = {
|
export let epsilon: API = {
|
||||||
client: new LemmyHttp('http://localhost:8581'),
|
client: new LemmyHttp('http://127.0.0.1:8581'),
|
||||||
};
|
};
|
||||||
|
|
||||||
const password = 'lemmylemmy'
|
const password = 'lemmylemmy'
|
||||||
|
@ -289,13 +289,14 @@ export async function resolvePerson(
|
||||||
export async function banPersonFromSite(
|
export async function banPersonFromSite(
|
||||||
api: API,
|
api: API,
|
||||||
person_id: number,
|
person_id: number,
|
||||||
ban: boolean
|
ban: boolean,
|
||||||
|
remove_data: boolean,
|
||||||
): Promise<BanPersonResponse> {
|
): Promise<BanPersonResponse> {
|
||||||
// Make sure lemmy-beta/c/main is cached on lemmy_alpha
|
// Make sure lemmy-beta/c/main is cached on lemmy_alpha
|
||||||
let form: BanPerson = {
|
let form: BanPerson = {
|
||||||
person_id,
|
person_id,
|
||||||
ban,
|
ban,
|
||||||
remove_data: false,
|
remove_data,
|
||||||
auth: api.auth,
|
auth: api.auth,
|
||||||
};
|
};
|
||||||
return api.client.banPerson(form);
|
return api.client.banPerson(form);
|
||||||
|
@ -305,13 +306,13 @@ export async function banPersonFromCommunity(
|
||||||
api: API,
|
api: API,
|
||||||
person_id: number,
|
person_id: number,
|
||||||
community_id: number,
|
community_id: number,
|
||||||
|
remove_data: boolean,
|
||||||
ban: boolean
|
ban: boolean
|
||||||
): Promise<BanFromCommunityResponse> {
|
): Promise<BanFromCommunityResponse> {
|
||||||
// Make sure lemmy-beta/c/main is cached on lemmy_alpha
|
|
||||||
let form: BanFromCommunity = {
|
let form: BanFromCommunity = {
|
||||||
person_id,
|
person_id,
|
||||||
community_id,
|
community_id,
|
||||||
remove_data: false,
|
remove_data,
|
||||||
ban,
|
ban,
|
||||||
auth: api.auth,
|
auth: api.auth,
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,6 +8,7 @@ use lemmy_api_common::{
|
||||||
community::*,
|
community::*,
|
||||||
get_local_user_view_from_jwt,
|
get_local_user_view_from_jwt,
|
||||||
is_mod_or_admin,
|
is_mod_or_admin,
|
||||||
|
remove_user_data_in_community,
|
||||||
};
|
};
|
||||||
use lemmy_apub::{
|
use lemmy_apub::{
|
||||||
activities::block::SiteOrCommunity,
|
activities::block::SiteOrCommunity,
|
||||||
|
@ -20,7 +21,6 @@ use lemmy_apub::{
|
||||||
};
|
};
|
||||||
use lemmy_db_schema::{
|
use lemmy_db_schema::{
|
||||||
source::{
|
source::{
|
||||||
comment::Comment,
|
|
||||||
community::{
|
community::{
|
||||||
Community,
|
Community,
|
||||||
CommunityFollower,
|
CommunityFollower,
|
||||||
|
@ -40,11 +40,9 @@ use lemmy_db_schema::{
|
||||||
ModTransferCommunityForm,
|
ModTransferCommunityForm,
|
||||||
},
|
},
|
||||||
person::Person,
|
person::Person,
|
||||||
post::Post,
|
|
||||||
},
|
},
|
||||||
traits::{Bannable, Blockable, Crud, Followable, Joinable},
|
traits::{Bannable, Blockable, Crud, Followable, Joinable},
|
||||||
};
|
};
|
||||||
use lemmy_db_views::comment_view::CommentQueryBuilder;
|
|
||||||
use lemmy_db_views_actor::{
|
use lemmy_db_views_actor::{
|
||||||
community_moderator_view::CommunityModeratorView,
|
community_moderator_view::CommunityModeratorView,
|
||||||
community_view::CommunityView,
|
community_view::CommunityView,
|
||||||
|
@ -280,30 +278,7 @@ impl Perform for BanFromCommunity {
|
||||||
|
|
||||||
// Remove/Restore their data if that's desired
|
// Remove/Restore their data if that's desired
|
||||||
if remove_data {
|
if remove_data {
|
||||||
// Posts
|
remove_user_data_in_community(community_id, banned_person_id, context.pool()).await?;
|
||||||
blocking(context.pool(), move |conn: &'_ _| {
|
|
||||||
Post::update_removed_for_creator(conn, banned_person_id, Some(community_id), true)
|
|
||||||
})
|
|
||||||
.await??;
|
|
||||||
|
|
||||||
// Comments
|
|
||||||
// TODO Diesel doesn't allow updates with joins, so this has to be a loop
|
|
||||||
let comments = blocking(context.pool(), move |conn| {
|
|
||||||
CommentQueryBuilder::create(conn)
|
|
||||||
.creator_id(banned_person_id)
|
|
||||||
.community_id(community_id)
|
|
||||||
.limit(std::i64::MAX)
|
|
||||||
.list()
|
|
||||||
})
|
|
||||||
.await??;
|
|
||||||
|
|
||||||
for comment_view in &comments {
|
|
||||||
let comment_id = comment_view.comment.id;
|
|
||||||
blocking(context.pool(), move |conn: &'_ _| {
|
|
||||||
Comment::update_removed(conn, comment_id, true)
|
|
||||||
})
|
|
||||||
.await??;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mod tables
|
// Mod tables
|
||||||
|
|
|
@ -23,7 +23,10 @@ use lemmy_db_schema::{
|
||||||
traits::{ApubActor, Crud, Readable},
|
traits::{ApubActor, Crud, Readable},
|
||||||
DbPool,
|
DbPool,
|
||||||
};
|
};
|
||||||
use lemmy_db_views::local_user_view::{LocalUserSettingsView, LocalUserView};
|
use lemmy_db_views::{
|
||||||
|
comment_view::CommentQueryBuilder,
|
||||||
|
local_user_view::{LocalUserSettingsView, LocalUserView},
|
||||||
|
};
|
||||||
use lemmy_db_views_actor::{
|
use lemmy_db_views_actor::{
|
||||||
community_moderator_view::CommunityModeratorView,
|
community_moderator_view::CommunityModeratorView,
|
||||||
community_person_ban_view::CommunityPersonBanView,
|
community_person_ban_view::CommunityPersonBanView,
|
||||||
|
@ -593,3 +596,36 @@ pub async fn remove_user_data(banned_person_id: PersonId, pool: &DbPool) -> Resu
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn remove_user_data_in_community(
|
||||||
|
community_id: CommunityId,
|
||||||
|
banned_person_id: PersonId,
|
||||||
|
pool: &DbPool,
|
||||||
|
) -> Result<(), LemmyError> {
|
||||||
|
// Posts
|
||||||
|
blocking(pool, move |conn| {
|
||||||
|
Post::update_removed_for_creator(conn, banned_person_id, Some(community_id), true)
|
||||||
|
})
|
||||||
|
.await??;
|
||||||
|
|
||||||
|
// Comments
|
||||||
|
// TODO Diesel doesn't allow updates with joins, so this has to be a loop
|
||||||
|
let comments = blocking(pool, move |conn| {
|
||||||
|
CommentQueryBuilder::create(conn)
|
||||||
|
.creator_id(banned_person_id)
|
||||||
|
.community_id(community_id)
|
||||||
|
.limit(std::i64::MAX)
|
||||||
|
.list()
|
||||||
|
})
|
||||||
|
.await??;
|
||||||
|
|
||||||
|
for comment_view in &comments {
|
||||||
|
let comment_id = comment_view.comment.id;
|
||||||
|
blocking(pool, move |conn| {
|
||||||
|
Comment::update_removed(conn, comment_id, true)
|
||||||
|
})
|
||||||
|
.await??;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
|
@ -63,6 +63,7 @@ impl PerformCrud for CreateSite {
|
||||||
}
|
}
|
||||||
|
|
||||||
let actor_id: DbUrl = Url::parse(&Settings::get().get_protocol_and_hostname())?.into();
|
let actor_id: DbUrl = Url::parse(&Settings::get().get_protocol_and_hostname())?.into();
|
||||||
|
let inbox_url = Some(generate_site_inbox_url(&actor_id)?);
|
||||||
let keypair = generate_actor_keypair()?;
|
let keypair = generate_actor_keypair()?;
|
||||||
let site_form = SiteForm {
|
let site_form = SiteForm {
|
||||||
name: data.name.to_owned(),
|
name: data.name.to_owned(),
|
||||||
|
@ -74,8 +75,9 @@ impl PerformCrud for CreateSite {
|
||||||
open_registration: data.open_registration,
|
open_registration: data.open_registration,
|
||||||
enable_nsfw: data.enable_nsfw,
|
enable_nsfw: data.enable_nsfw,
|
||||||
community_creation_admin_only: data.community_creation_admin_only,
|
community_creation_admin_only: data.community_creation_admin_only,
|
||||||
|
actor_id: Some(actor_id),
|
||||||
last_refreshed_at: Some(naive_now()),
|
last_refreshed_at: Some(naive_now()),
|
||||||
inbox_url: Some(generate_site_inbox_url(&actor_id)?),
|
inbox_url,
|
||||||
private_key: Some(Some(keypair.private_key)),
|
private_key: Some(Some(keypair.private_key)),
|
||||||
public_key: Some(keypair.public_key),
|
public_key: Some(keypair.public_key),
|
||||||
..SiteForm::default()
|
..SiteForm::default()
|
||||||
|
|
|
@ -16,7 +16,7 @@ use crate::{
|
||||||
use activitystreams_kinds::{activity::BlockType, public};
|
use activitystreams_kinds::{activity::BlockType, public};
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use chrono::NaiveDateTime;
|
use chrono::NaiveDateTime;
|
||||||
use lemmy_api_common::{blocking, remove_user_data};
|
use lemmy_api_common::{blocking, remove_user_data, remove_user_data_in_community};
|
||||||
use lemmy_apub_lib::{
|
use lemmy_apub_lib::{
|
||||||
data::Data,
|
data::Data,
|
||||||
object_id::ObjectId,
|
object_id::ObjectId,
|
||||||
|
@ -148,11 +148,11 @@ impl ActivityHandler for BlockUser {
|
||||||
.object
|
.object
|
||||||
.dereference(context, context.client(), request_counter)
|
.dereference(context, context.client(), request_counter)
|
||||||
.await?;
|
.await?;
|
||||||
match self
|
let target = self
|
||||||
.target
|
.target
|
||||||
.dereference(context, context.client(), request_counter)
|
.dereference(context, context.client(), request_counter)
|
||||||
.await?
|
.await?;
|
||||||
{
|
match target {
|
||||||
SiteOrCommunity::Site(_site) => {
|
SiteOrCommunity::Site(_site) => {
|
||||||
let blocked_person = blocking(context.pool(), move |conn| {
|
let blocked_person = blocking(context.pool(), move |conn| {
|
||||||
Person::ban_person(conn, blocked_person.id, true, expires)
|
Person::ban_person(conn, blocked_person.id, true, expires)
|
||||||
|
@ -196,7 +196,7 @@ impl ActivityHandler for BlockUser {
|
||||||
.ok();
|
.ok();
|
||||||
|
|
||||||
if self.remove_data.unwrap_or(false) {
|
if self.remove_data.unwrap_or(false) {
|
||||||
todo!()
|
remove_user_data_in_community(community.id, blocked_person.id, context.pool()).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// write to mod log
|
// write to mod log
|
||||||
|
|
|
@ -29,7 +29,6 @@ pub fn config(cfg: &mut web::ServiceConfig, settings: &Settings) {
|
||||||
cfg
|
cfg
|
||||||
.route("/", web::get().to(get_apub_site_http))
|
.route("/", web::get().to(get_apub_site_http))
|
||||||
.route("/site_outbox", web::get().to(get_apub_site_outbox))
|
.route("/site_outbox", web::get().to(get_apub_site_outbox))
|
||||||
.route("/site_inbox", web::get().to(get_apub_site_inbox))
|
|
||||||
.route(
|
.route(
|
||||||
"/c/{community_name}",
|
"/c/{community_name}",
|
||||||
web::get().to(get_apub_community_http),
|
web::get().to(get_apub_community_http),
|
||||||
|
@ -61,7 +60,8 @@ pub fn config(cfg: &mut web::ServiceConfig, settings: &Settings) {
|
||||||
.guard(InboxRequestGuard)
|
.guard(InboxRequestGuard)
|
||||||
.route("/c/{community_name}/inbox", web::post().to(community_inbox))
|
.route("/c/{community_name}/inbox", web::post().to(community_inbox))
|
||||||
.route("/u/{user_name}/inbox", web::post().to(person_inbox))
|
.route("/u/{user_name}/inbox", web::post().to(person_inbox))
|
||||||
.route("/inbox", web::post().to(shared_inbox)),
|
.route("/inbox", web::post().to(shared_inbox))
|
||||||
|
.route("/site_inbox", web::post().to(get_apub_site_inbox)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,6 @@ pub(crate) async fn get_apub_site_outbox() -> Result<HttpResponse, LemmyError> {
|
||||||
pub async fn get_apub_site_inbox(
|
pub async fn get_apub_site_inbox(
|
||||||
request: HttpRequest,
|
request: HttpRequest,
|
||||||
payload: Payload,
|
payload: Payload,
|
||||||
_path: web::Path<String>,
|
|
||||||
context: web::Data<LemmyContext>,
|
context: web::Data<LemmyContext>,
|
||||||
) -> Result<HttpResponse, LemmyError> {
|
) -> Result<HttpResponse, LemmyError> {
|
||||||
let unparsed = payload_to_string(payload).await?;
|
let unparsed = payload_to_string(payload).await?;
|
||||||
|
|
|
@ -165,7 +165,9 @@ pub fn generate_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_site_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
|
pub fn generate_site_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
|
||||||
Ok(Url::parse(&format!("{}/site_inbox", actor_id))?.into())
|
let mut actor_id: Url = actor_id.clone().into();
|
||||||
|
actor_id.set_path("site_inbox");
|
||||||
|
Ok(actor_id.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_shared_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, LemmyError> {
|
pub fn generate_shared_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, LemmyError> {
|
||||||
|
|
Loading…
Reference in a new issue