mirror of
https://github.com/LemmyNet/lemmy.git
synced 2025-02-27 16:16:36 +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 () => {
|
||||
let alphaShortname = `@lemmy_alpha@lemmy-alpha:8541`;
|
||||
let alphaPerson = (await resolvePerson(beta, alphaShortname)).person;
|
||||
let alphaPerson = (await resolvePerson(alpha, alphaShortname)).person;
|
||||
expect(alphaPerson).toBeDefined();
|
||||
|
||||
// ban alpha from beta site
|
||||
let banAlpha = await banPersonFromSite(beta, alphaPerson.person.id, true);
|
||||
let postRes1 = await createPost(alpha, betaCommunity.community.id);
|
||||
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);
|
||||
|
||||
// Alpha makes post on beta
|
||||
let postRes = await createPost(alpha, betaCommunity.community.id);
|
||||
expect(postRes.post_view.post).toBeDefined();
|
||||
expect(postRes.post_view.community.local).toBe(false);
|
||||
expect(postRes.post_view.creator.local).toBe(true);
|
||||
expect(postRes.post_view.counts.score).toBe(1);
|
||||
// alpha ban should be federated to beta
|
||||
let alphaUserOnBeta1 = await resolvePerson(beta, alphaPerson.person.actor_id);
|
||||
console.debug(alphaUserOnBeta1);
|
||||
expect(alphaUserOnBeta1.person.person.banned).toBe(true);
|
||||
|
||||
// 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
|
||||
let searchBeta = await searchPostLocal(beta, postRes.post_view.post);
|
||||
let betaPost = searchBeta.posts[0];
|
||||
expect(betaPost).toBeUndefined();
|
||||
let searchBeta = await searchPostLocal(beta, postRes2.post_view.post);
|
||||
expect(searchBeta.posts[0]).toBeUndefined();
|
||||
|
||||
// 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);
|
||||
|
||||
let alphaUserOnBeta2 = await resolvePerson(beta, alphaPerson.person.actor_id)
|
||||
expect(alphaUserOnBeta2.person.person.banned).toBe(false);
|
||||
});
|
||||
|
||||
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;
|
||||
expect(alphaPerson).toBeDefined();
|
||||
|
||||
// ban alpha from beta site
|
||||
await banPersonFromCommunity(beta, alphaPerson.person.id, 2, false);
|
||||
let banAlpha = await banPersonFromCommunity(beta, alphaPerson.person.id, 2, true);
|
||||
// make a post in beta, it goes through
|
||||
let postRes1 = await createPost(alpha, betaCommunity.community.id);
|
||||
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);
|
||||
|
||||
// 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
|
||||
let postRes = await createPost(alpha, betaCommunity.community.id);
|
||||
expect(postRes.post_view).toBeUndefined();
|
||||
let postRes2 = await createPost(alpha, betaCommunity.community.id);
|
||||
expect(postRes2.post_view).toBeUndefined();
|
||||
|
||||
// Unban alpha
|
||||
let unBanAlpha = await banPersonFromCommunity(
|
||||
beta,
|
||||
alphaPerson.person.id,
|
||||
2,
|
||||
false,
|
||||
false
|
||||
);
|
||||
expect(unBanAlpha.banned).toBe(false);
|
||||
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);
|
||||
let postRes3 = await createPost(alpha, betaCommunity.community.id);
|
||||
expect(postRes3.post_view.post).toBeDefined();
|
||||
expect(postRes3.post_view.community.local).toBe(false);
|
||||
expect(postRes3.post_view.creator.local).toBe(true);
|
||||
expect(postRes3.post_view.counts.score).toBe(1);
|
||||
|
||||
// Make sure that post makes it to beta community
|
||||
let searchBeta = await searchPostLocal(beta, postRes2.post_view.post);
|
||||
let betaPost = searchBeta.posts[0];
|
||||
expect(betaPost).toBeDefined();
|
||||
let searchBeta2 = await searchPostLocal(beta, postRes3.post_view.post);
|
||||
expect(searchBeta2.posts[0]).toBeDefined();
|
||||
});
|
||||
|
||||
test('Report a post', async () => {
|
||||
|
|
|
@ -66,23 +66,23 @@ export interface API {
|
|||
}
|
||||
|
||||
export let alpha: API = {
|
||||
client: new LemmyHttp('http://localhost:8541'),
|
||||
client: new LemmyHttp('http://127.0.0.1:8541'),
|
||||
};
|
||||
|
||||
export let beta: API = {
|
||||
client: new LemmyHttp('http://localhost:8551'),
|
||||
client: new LemmyHttp('http://127.0.0.1:8551'),
|
||||
};
|
||||
|
||||
export let gamma: API = {
|
||||
client: new LemmyHttp('http://localhost:8561'),
|
||||
client: new LemmyHttp('http://127.0.0.1:8561'),
|
||||
};
|
||||
|
||||
export let delta: API = {
|
||||
client: new LemmyHttp('http://localhost:8571'),
|
||||
client: new LemmyHttp('http://127.0.0.1:8571'),
|
||||
};
|
||||
|
||||
export let epsilon: API = {
|
||||
client: new LemmyHttp('http://localhost:8581'),
|
||||
client: new LemmyHttp('http://127.0.0.1:8581'),
|
||||
};
|
||||
|
||||
const password = 'lemmylemmy'
|
||||
|
@ -289,13 +289,14 @@ export async function resolvePerson(
|
|||
export async function banPersonFromSite(
|
||||
api: API,
|
||||
person_id: number,
|
||||
ban: boolean
|
||||
ban: boolean,
|
||||
remove_data: boolean,
|
||||
): Promise<BanPersonResponse> {
|
||||
// Make sure lemmy-beta/c/main is cached on lemmy_alpha
|
||||
let form: BanPerson = {
|
||||
person_id,
|
||||
ban,
|
||||
remove_data: false,
|
||||
remove_data,
|
||||
auth: api.auth,
|
||||
};
|
||||
return api.client.banPerson(form);
|
||||
|
@ -305,13 +306,13 @@ export async function banPersonFromCommunity(
|
|||
api: API,
|
||||
person_id: number,
|
||||
community_id: number,
|
||||
remove_data: boolean,
|
||||
ban: boolean
|
||||
): Promise<BanFromCommunityResponse> {
|
||||
// Make sure lemmy-beta/c/main is cached on lemmy_alpha
|
||||
let form: BanFromCommunity = {
|
||||
person_id,
|
||||
community_id,
|
||||
remove_data: false,
|
||||
remove_data,
|
||||
ban,
|
||||
auth: api.auth,
|
||||
};
|
||||
|
|
|
@ -8,6 +8,7 @@ use lemmy_api_common::{
|
|||
community::*,
|
||||
get_local_user_view_from_jwt,
|
||||
is_mod_or_admin,
|
||||
remove_user_data_in_community,
|
||||
};
|
||||
use lemmy_apub::{
|
||||
activities::block::SiteOrCommunity,
|
||||
|
@ -20,7 +21,6 @@ use lemmy_apub::{
|
|||
};
|
||||
use lemmy_db_schema::{
|
||||
source::{
|
||||
comment::Comment,
|
||||
community::{
|
||||
Community,
|
||||
CommunityFollower,
|
||||
|
@ -40,11 +40,9 @@ use lemmy_db_schema::{
|
|||
ModTransferCommunityForm,
|
||||
},
|
||||
person::Person,
|
||||
post::Post,
|
||||
},
|
||||
traits::{Bannable, Blockable, Crud, Followable, Joinable},
|
||||
};
|
||||
use lemmy_db_views::comment_view::CommentQueryBuilder;
|
||||
use lemmy_db_views_actor::{
|
||||
community_moderator_view::CommunityModeratorView,
|
||||
community_view::CommunityView,
|
||||
|
@ -280,30 +278,7 @@ impl Perform for BanFromCommunity {
|
|||
|
||||
// Remove/Restore their data if that's desired
|
||||
if remove_data {
|
||||
// Posts
|
||||
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??;
|
||||
}
|
||||
remove_user_data_in_community(community_id, banned_person_id, context.pool()).await?;
|
||||
}
|
||||
|
||||
// Mod tables
|
||||
|
|
|
@ -23,7 +23,10 @@ use lemmy_db_schema::{
|
|||
traits::{ApubActor, Crud, Readable},
|
||||
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::{
|
||||
community_moderator_view::CommunityModeratorView,
|
||||
community_person_ban_view::CommunityPersonBanView,
|
||||
|
@ -593,3 +596,36 @@ pub async fn remove_user_data(banned_person_id: PersonId, pool: &DbPool) -> Resu
|
|||
|
||||
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 inbox_url = Some(generate_site_inbox_url(&actor_id)?);
|
||||
let keypair = generate_actor_keypair()?;
|
||||
let site_form = SiteForm {
|
||||
name: data.name.to_owned(),
|
||||
|
@ -74,8 +75,9 @@ impl PerformCrud for CreateSite {
|
|||
open_registration: data.open_registration,
|
||||
enable_nsfw: data.enable_nsfw,
|
||||
community_creation_admin_only: data.community_creation_admin_only,
|
||||
actor_id: Some(actor_id),
|
||||
last_refreshed_at: Some(naive_now()),
|
||||
inbox_url: Some(generate_site_inbox_url(&actor_id)?),
|
||||
inbox_url,
|
||||
private_key: Some(Some(keypair.private_key)),
|
||||
public_key: Some(keypair.public_key),
|
||||
..SiteForm::default()
|
||||
|
|
|
@ -16,7 +16,7 @@ use crate::{
|
|||
use activitystreams_kinds::{activity::BlockType, public};
|
||||
use anyhow::anyhow;
|
||||
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::{
|
||||
data::Data,
|
||||
object_id::ObjectId,
|
||||
|
@ -148,11 +148,11 @@ impl ActivityHandler for BlockUser {
|
|||
.object
|
||||
.dereference(context, context.client(), request_counter)
|
||||
.await?;
|
||||
match self
|
||||
let target = self
|
||||
.target
|
||||
.dereference(context, context.client(), request_counter)
|
||||
.await?
|
||||
{
|
||||
.await?;
|
||||
match target {
|
||||
SiteOrCommunity::Site(_site) => {
|
||||
let blocked_person = blocking(context.pool(), move |conn| {
|
||||
Person::ban_person(conn, blocked_person.id, true, expires)
|
||||
|
@ -196,7 +196,7 @@ impl ActivityHandler for BlockUser {
|
|||
.ok();
|
||||
|
||||
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
|
||||
|
|
|
@ -29,7 +29,6 @@ pub fn config(cfg: &mut web::ServiceConfig, settings: &Settings) {
|
|||
cfg
|
||||
.route("/", web::get().to(get_apub_site_http))
|
||||
.route("/site_outbox", web::get().to(get_apub_site_outbox))
|
||||
.route("/site_inbox", web::get().to(get_apub_site_inbox))
|
||||
.route(
|
||||
"/c/{community_name}",
|
||||
web::get().to(get_apub_community_http),
|
||||
|
@ -61,7 +60,8 @@ pub fn config(cfg: &mut web::ServiceConfig, settings: &Settings) {
|
|||
.guard(InboxRequestGuard)
|
||||
.route("/c/{community_name}/inbox", web::post().to(community_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(
|
||||
request: HttpRequest,
|
||||
payload: Payload,
|
||||
_path: web::Path<String>,
|
||||
context: web::Data<LemmyContext>,
|
||||
) -> Result<HttpResponse, LemmyError> {
|
||||
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> {
|
||||
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> {
|
||||
|
|
Loading…
Reference in a new issue