1 jest.setTimeout(120000);
2 import { PostView, CommunityView } from "lemmy-js-client";
28 banPersonFromCommunity,
39 let betaCommunity: CommunityView | undefined;
41 beforeAll(async () => {
43 betaCommunity = (await resolveBetaCommunity(alpha)).community;
44 expect(betaCommunity).toBeDefined();
48 afterAll(async () => {
52 function assertPostFederation(postOne?: PostView, postTwo?: PostView) {
53 expect(postOne?.post.ap_id).toBe(postTwo?.post.ap_id);
54 expect(postOne?.post.name).toBe(postTwo?.post.name);
55 expect(postOne?.post.body).toBe(postTwo?.post.body);
56 // TODO url clears arent working
57 // expect(postOne?.post.url).toBe(postTwo?.post.url);
58 expect(postOne?.post.nsfw).toBe(postTwo?.post.nsfw);
59 expect(postOne?.post.embed_title).toBe(postTwo?.post.embed_title);
60 expect(postOne?.post.embed_description).toBe(postTwo?.post.embed_description);
61 expect(postOne?.post.embed_video_url).toBe(postTwo?.post.embed_video_url);
62 expect(postOne?.post.published).toBe(postTwo?.post.published);
63 expect(postOne?.community.actor_id).toBe(postTwo?.community.actor_id);
64 expect(postOne?.post.locked).toBe(postTwo?.post.locked);
65 expect(postOne?.post.removed).toBe(postTwo?.post.removed);
66 expect(postOne?.post.deleted).toBe(postTwo?.post.deleted);
69 test("Create a post", async () => {
71 throw "Missing beta community";
74 let postRes = await createPost(alpha, betaCommunity.community.id);
75 expect(postRes.post_view.post).toBeDefined();
76 expect(postRes.post_view.community.local).toBe(false);
77 expect(postRes.post_view.creator.local).toBe(true);
78 expect(postRes.post_view.counts.score).toBe(1);
80 // Make sure that post is liked on beta
81 let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
83 expect(betaPost).toBeDefined();
84 expect(betaPost?.community.local).toBe(true);
85 expect(betaPost?.creator.local).toBe(false);
86 expect(betaPost?.counts.score).toBe(1);
87 assertPostFederation(betaPost, postRes.post_view);
89 // Delta only follows beta, so it should not see an alpha ap_id
90 let deltaPost = (await resolvePost(delta, postRes.post_view.post)).post;
91 expect(deltaPost).toBeUndefined();
93 // Epsilon has alpha blocked, it should not see the alpha post
94 let epsilonPost = (await resolvePost(epsilon, postRes.post_view.post)).post;
95 expect(epsilonPost).toBeUndefined();
98 test("Create a post in a non-existent community", async () => {
99 let postRes = (await createPost(alpha, -2)) as any;
100 expect(postRes.error).toBe("couldnt_find_community");
103 test("Unlike a post", async () => {
104 if (!betaCommunity) {
105 throw "Missing beta community";
107 let postRes = await createPost(alpha, betaCommunity.community.id);
108 let unlike = await likePost(alpha, 0, postRes.post_view.post);
109 expect(unlike.post_view.counts.score).toBe(0);
111 // Try to unlike it again, make sure it stays at 0
112 let unlike2 = await likePost(alpha, 0, postRes.post_view.post);
113 expect(unlike2.post_view.counts.score).toBe(0);
115 // Make sure that post is unliked on beta
116 let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
117 expect(betaPost).toBeDefined();
118 expect(betaPost?.community.local).toBe(true);
119 expect(betaPost?.creator.local).toBe(false);
120 expect(betaPost?.counts.score).toBe(0);
121 assertPostFederation(betaPost, postRes.post_view);
124 test("Update a post", async () => {
125 if (!betaCommunity) {
126 throw "Missing beta community";
128 let postRes = await createPost(alpha, betaCommunity.community.id);
130 let updatedName = "A jest test federated post, updated";
131 let updatedPost = await editPost(alpha, postRes.post_view.post);
132 expect(updatedPost.post_view.post.name).toBe(updatedName);
133 expect(updatedPost.post_view.community.local).toBe(false);
134 expect(updatedPost.post_view.creator.local).toBe(true);
136 // Make sure that post is updated on beta
137 let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
139 throw "Missing beta post";
141 expect(betaPost.community.local).toBe(true);
142 expect(betaPost.creator.local).toBe(false);
143 expect(betaPost.post.name).toBe(updatedName);
144 assertPostFederation(betaPost, updatedPost.post_view);
146 // Make sure lemmy beta cannot update the post
147 let updatedPostBeta = (await editPost(beta, betaPost.post)) as any;
148 expect(updatedPostBeta.error).toBe("no_post_edit_allowed");
151 test("Sticky a post", async () => {
152 if (!betaCommunity) {
153 throw "Missing beta community";
155 let postRes = await createPost(alpha, betaCommunity.community.id);
157 let betaPost1 = (await resolvePost(beta, postRes.post_view.post)).post;
159 throw "Missing beta post1";
161 let stickiedPostRes = await featurePost(beta, true, betaPost1.post);
162 expect(stickiedPostRes.post_view.post.featured_community).toBe(true);
164 // Make sure that post is stickied on beta
165 let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
166 expect(betaPost?.community.local).toBe(true);
167 expect(betaPost?.creator.local).toBe(false);
168 expect(betaPost?.post.featured_community).toBe(true);
171 let unstickiedPost = await featurePost(beta, false, betaPost1.post);
172 expect(unstickiedPost.post_view.post.featured_community).toBe(false);
174 // Make sure that post is unstickied on beta
175 let betaPost2 = (await resolvePost(beta, postRes.post_view.post)).post;
176 expect(betaPost2?.community.local).toBe(true);
177 expect(betaPost2?.creator.local).toBe(false);
178 expect(betaPost2?.post.featured_community).toBe(false);
180 // Make sure that gamma cannot sticky the post on beta
181 let gammaPost = (await resolvePost(gamma, postRes.post_view.post)).post;
183 throw "Missing gamma post";
185 let gammaTrySticky = await featurePost(gamma, true, gammaPost.post);
186 let betaPost3 = (await resolvePost(beta, postRes.post_view.post)).post;
187 expect(gammaTrySticky.post_view.post.featured_community).toBe(true);
188 expect(betaPost3?.post.featured_community).toBe(false);
191 test("Lock a post", async () => {
192 if (!betaCommunity) {
193 throw "Missing beta community";
195 await followCommunity(alpha, true, betaCommunity.community.id);
196 let postRes = await createPost(alpha, betaCommunity.community.id);
199 let betaPost1 = (await resolvePost(beta, postRes.post_view.post)).post;
201 throw "Missing beta post1";
203 let lockedPostRes = await lockPost(beta, true, betaPost1.post);
204 expect(lockedPostRes.post_view.post.locked).toBe(true);
206 // Make sure that post is locked on alpha
207 let searchAlpha = await searchPostLocal(alpha, postRes.post_view.post);
208 let alphaPost1 = searchAlpha.posts[0];
209 expect(alphaPost1.post.locked).toBe(true);
211 // Try to make a new comment there, on alpha
212 let comment: any = await createComment(alpha, alphaPost1.post.id);
213 expect(comment["error"]).toBe("locked");
216 let unlockedPost = await lockPost(beta, false, betaPost1.post);
217 expect(unlockedPost.post_view.post.locked).toBe(false);
219 // Make sure that post is unlocked on alpha
220 let searchAlpha2 = await searchPostLocal(alpha, postRes.post_view.post);
221 let alphaPost2 = searchAlpha2.posts[0];
222 expect(alphaPost2.community.local).toBe(false);
223 expect(alphaPost2.creator.local).toBe(true);
224 expect(alphaPost2.post.locked).toBe(false);
226 // Try to create a new comment, on alpha
227 let commentAlpha = await createComment(alpha, alphaPost1.post.id);
228 expect(commentAlpha).toBeDefined();
231 test("Delete a post", async () => {
232 if (!betaCommunity) {
233 throw "Missing beta community";
236 let postRes = await createPost(alpha, betaCommunity.community.id);
237 expect(postRes.post_view.post).toBeDefined();
239 let deletedPost = await deletePost(alpha, true, postRes.post_view.post);
240 expect(deletedPost.post_view.post.deleted).toBe(true);
241 expect(deletedPost.post_view.post.name).toBe(postRes.post_view.post.name);
243 // Make sure lemmy beta sees post is deleted
244 let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
245 // This will be undefined because of the tombstone
246 expect(betaPost).toBeUndefined();
249 let undeletedPost = await deletePost(alpha, false, postRes.post_view.post);
250 expect(undeletedPost.post_view.post.deleted).toBe(false);
252 // Make sure lemmy beta sees post is undeleted
253 let betaPost2 = (await resolvePost(beta, postRes.post_view.post)).post;
255 throw "Missing beta post 2";
257 expect(betaPost2.post.deleted).toBe(false);
258 assertPostFederation(betaPost2, undeletedPost.post_view);
260 // Make sure lemmy beta cannot delete the post
261 let deletedPostBeta = (await deletePost(beta, true, betaPost2.post)) as any;
262 expect(deletedPostBeta.error).toStrictEqual("no_post_edit_allowed");
265 test("Remove a post from admin and community on different instance", async () => {
266 if (!betaCommunity) {
267 throw "Missing beta community";
270 let gammaCommunity = (
271 await resolveCommunity(gamma, betaCommunity.community.actor_id)
272 ).community?.community;
273 if (!gammaCommunity) {
274 throw "Missing gamma community";
276 let postRes = await createPost(gamma, gammaCommunity.id);
278 let alphaPost = (await resolvePost(alpha, postRes.post_view.post)).post;
280 throw "Missing alpha post";
282 let removedPost = await removePost(alpha, true, alphaPost.post);
283 expect(removedPost.post_view.post.removed).toBe(true);
284 expect(removedPost.post_view.post.name).toBe(postRes.post_view.post.name);
286 // Make sure lemmy beta sees post is NOT removed
287 let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
289 throw "Missing beta post";
291 expect(betaPost.post.removed).toBe(false);
294 let undeletedPost = await removePost(alpha, false, alphaPost.post);
295 expect(undeletedPost.post_view.post.removed).toBe(false);
297 // Make sure lemmy beta sees post is undeleted
298 let betaPost2 = (await resolvePost(beta, postRes.post_view.post)).post;
299 expect(betaPost2?.post.removed).toBe(false);
300 assertPostFederation(betaPost2, undeletedPost.post_view);
303 test("Remove a post from admin and community on same instance", async () => {
304 if (!betaCommunity) {
305 throw "Missing beta community";
307 await followBeta(alpha);
308 let postRes = await createPost(alpha, betaCommunity.community.id);
309 expect(postRes.post_view.post).toBeDefined();
311 // Get the id for beta
312 let searchBeta = await searchPostLocal(beta, postRes.post_view.post);
313 let betaPost = searchBeta.posts[0];
314 expect(betaPost).toBeDefined();
316 // The beta admin removes it (the community lives on beta)
317 let removePostRes = await removePost(beta, true, betaPost.post);
318 expect(removePostRes.post_view.post.removed).toBe(true);
320 // Make sure lemmy alpha sees post is removed
321 // let alphaPost = await getPost(alpha, postRes.post_view.post.id);
322 // expect(alphaPost.post_view.post.removed).toBe(true); // TODO this shouldn't be commented
323 // assertPostFederation(alphaPost.post_view, removePostRes.post_view);
326 let undeletedPost = await removePost(beta, false, betaPost.post);
327 expect(undeletedPost.post_view.post.removed).toBe(false);
329 // Make sure lemmy alpha sees post is undeleted
330 let alphaPost2 = await getPost(alpha, postRes.post_view.post.id);
331 expect(alphaPost2.post_view.post.removed).toBe(false);
332 assertPostFederation(alphaPost2.post_view, undeletedPost.post_view);
333 await unfollowRemotes(alpha);
336 test("Search for a post", async () => {
337 if (!betaCommunity) {
338 throw "Missing beta community";
340 await unfollowRemotes(alpha);
341 let postRes = await createPost(alpha, betaCommunity.community.id);
342 expect(postRes.post_view.post).toBeDefined();
344 let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
345 expect(betaPost?.post.name).toBeDefined();
348 test("Enforce site ban for federated user", async () => {
349 if (!betaCommunity) {
350 throw "Missing beta community";
352 // create a test user
353 let alphaUserJwt = await registerUser(alpha);
354 expect(alphaUserJwt).toBeDefined();
355 let alpha_user: API = {
356 client: alpha.client,
357 auth: alphaUserJwt.jwt ?? "",
359 let alphaUserActorId = (await getSite(alpha_user)).my_user?.local_user_view
361 if (!alphaUserActorId) {
362 throw "Missing alpha user actor id";
364 expect(alphaUserActorId).toBeDefined();
365 let alphaPerson = (await resolvePerson(alpha_user, alphaUserActorId)).person;
367 throw "Missing alpha person";
369 expect(alphaPerson).toBeDefined();
371 // alpha makes post in beta community, it federates to beta instance
372 let postRes1 = await createPost(alpha_user, betaCommunity.community.id);
373 let searchBeta1 = await searchPostLocal(beta, postRes1.post_view.post);
374 expect(searchBeta1.posts[0]).toBeDefined();
376 // ban alpha from its instance
377 let banAlpha = await banPersonFromSite(
379 alphaPerson.person.id,
383 expect(banAlpha.banned).toBe(true);
385 // alpha ban should be federated to beta
386 let alphaUserOnBeta1 = await resolvePerson(beta, alphaUserActorId);
387 expect(alphaUserOnBeta1.person?.person.banned).toBe(true);
389 // existing alpha post should be removed on beta
390 let searchBeta2 = await searchPostLocal(beta, postRes1.post_view.post);
391 expect(searchBeta2.posts[0]).toBeUndefined();
394 let unBanAlpha = await banPersonFromSite(
396 alphaPerson.person.id,
400 expect(unBanAlpha.banned).toBe(false);
402 // alpha makes new post in beta community, it federates
403 let postRes2 = await createPost(alpha_user, betaCommunity.community.id);
404 let searchBeta3 = await searchPostLocal(beta, postRes2.post_view.post);
405 expect(searchBeta3.posts[0]).toBeDefined();
407 let alphaUserOnBeta2 = await resolvePerson(beta, alphaUserActorId);
408 expect(alphaUserOnBeta2.person?.person.banned).toBe(false);
411 test("Enforce community ban for federated user", async () => {
412 if (!betaCommunity) {
413 throw "Missing beta community";
415 let alphaShortname = `@lemmy_alpha@lemmy-alpha:8541`;
416 let alphaPerson = (await resolvePerson(beta, alphaShortname)).person;
418 throw "Missing alpha person";
420 expect(alphaPerson).toBeDefined();
422 // make a post in beta, it goes through
423 let postRes1 = await createPost(alpha, betaCommunity.community.id);
424 let searchBeta1 = await searchPostLocal(beta, postRes1.post_view.post);
425 expect(searchBeta1.posts[0]).toBeDefined();
427 // ban alpha from beta community
428 let banAlpha = await banPersonFromCommunity(
430 alphaPerson.person.id,
435 expect(banAlpha.banned).toBe(true);
437 // ensure that the post by alpha got removed
438 let searchAlpha1 = await searchPostLocal(alpha, postRes1.post_view.post);
439 expect(searchAlpha1.posts[0]).toBeUndefined();
441 // Alpha tries to make post on beta, but it fails because of ban
442 let postRes2 = await createPost(alpha, betaCommunity.community.id);
443 expect(postRes2.post_view).toBeUndefined();
446 let unBanAlpha = await banPersonFromCommunity(
448 alphaPerson.person.id,
453 expect(unBanAlpha.banned).toBe(false);
454 let postRes3 = await createPost(alpha, betaCommunity.community.id);
455 expect(postRes3.post_view.post).toBeDefined();
456 expect(postRes3.post_view.community.local).toBe(false);
457 expect(postRes3.post_view.creator.local).toBe(true);
458 expect(postRes3.post_view.counts.score).toBe(1);
460 // Make sure that post makes it to beta community
461 let searchBeta2 = await searchPostLocal(beta, postRes3.post_view.post);
462 expect(searchBeta2.posts[0]).toBeDefined();
465 test("A and G subscribe to B (center) A posts, it gets announced to G", async () => {
466 if (!betaCommunity) {
467 throw "Missing beta community";
469 let postRes = await createPost(alpha, betaCommunity.community.id);
470 expect(postRes.post_view.post).toBeDefined();
472 let betaPost = (await resolvePost(gamma, postRes.post_view.post)).post;
473 expect(betaPost?.post.name).toBeDefined();
476 test("Report a post", async () => {
477 // Note, this is a different one from the setup
478 let betaCommunity = (await resolveBetaCommunity(beta)).community;
479 if (!betaCommunity) {
480 throw "Missing beta community";
482 let postRes = await createPost(beta, betaCommunity.community.id);
483 expect(postRes.post_view.post).toBeDefined();
485 let alphaPost = (await resolvePost(alpha, postRes.post_view.post)).post;
487 throw "Missing alpha post";
490 await reportPost(alpha, alphaPost.post.id, randomString(10))
491 ).post_report_view.post_report;
493 let betaReport = (await listPostReports(beta)).post_reports[0].post_report;
494 expect(betaReport).toBeDefined();
495 expect(betaReport.resolved).toBe(false);
496 expect(betaReport.original_post_name).toBe(alphaReport.original_post_name);
497 expect(betaReport.original_post_url).toBe(alphaReport.original_post_url);
498 expect(betaReport.original_post_body).toBe(alphaReport.original_post_body);
499 expect(betaReport.reason).toBe(alphaReport.reason);