1 jest.setTimeout(120000);
26 banPersonFromCommunity,
35 import { PostView, CommunityView } from 'lemmy-js-client';
37 let betaCommunity: CommunityView;
39 beforeAll(async () => {
41 betaCommunity = (await resolveBetaCommunity(alpha)).community;
42 expect(betaCommunity).toBeDefined();
46 afterAll(async () => {
50 function assertPostFederation(postOne: PostView, postTwo: PostView) {
51 expect(postOne.post.ap_id).toBe(postTwo.post.ap_id);
52 expect(postOne.post.name).toBe(postTwo.post.name);
53 expect(postOne.post.body).toBe(postTwo.post.body);
54 expect(postOne.post.url).toBe(postTwo.post.url);
55 expect(postOne.post.nsfw).toBe(postTwo.post.nsfw);
56 expect(postOne.post.embed_title).toBe(postTwo.post.embed_title);
57 expect(postOne.post.embed_description).toBe(postTwo.post.embed_description);
58 expect(postOne.post.embed_html).toBe(postTwo.post.embed_html);
59 expect(postOne.post.published).toBe(postTwo.post.published);
60 expect(postOne.community.actor_id).toBe(postTwo.community.actor_id);
61 expect(postOne.post.locked).toBe(postTwo.post.locked);
62 expect(postOne.post.removed).toBe(postTwo.post.removed);
63 expect(postOne.post.deleted).toBe(postTwo.post.deleted);
66 test('Create a post', async () => {
67 let postRes = await createPost(alpha, betaCommunity.community.id);
68 expect(postRes.post_view.post).toBeDefined();
69 expect(postRes.post_view.community.local).toBe(false);
70 expect(postRes.post_view.creator.local).toBe(true);
71 expect(postRes.post_view.counts.score).toBe(1);
73 // Make sure that post is liked on beta
74 let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
76 expect(betaPost).toBeDefined();
77 expect(betaPost.community.local).toBe(true);
78 expect(betaPost.creator.local).toBe(false);
79 expect(betaPost.counts.score).toBe(1);
80 assertPostFederation(betaPost, postRes.post_view);
82 // Delta only follows beta, so it should not see an alpha ap_id
83 let deltaPost = (await resolvePost(delta, postRes.post_view.post)).post;
84 expect(deltaPost).toBeUndefined();
86 // Epsilon has alpha blocked, it should not see the alpha post
87 let epsilonPost = (await resolvePost(epsilon, postRes.post_view.post)).post;
88 expect(epsilonPost).toBeUndefined();
91 test('Create a post in a non-existent community', async () => {
92 let postRes = await createPost(alpha, -2);
93 expect(postRes).toStrictEqual({ error: 'couldnt_find_community' });
96 test('Unlike a post', async () => {
97 let postRes = await createPost(alpha, betaCommunity.community.id);
98 let unlike = await likePost(alpha, 0, postRes.post_view.post);
99 expect(unlike.post_view.counts.score).toBe(0);
101 // Try to unlike it again, make sure it stays at 0
102 let unlike2 = await likePost(alpha, 0, postRes.post_view.post);
103 expect(unlike2.post_view.counts.score).toBe(0);
105 // Make sure that post is unliked on beta
106 let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
107 expect(betaPost).toBeDefined();
108 expect(betaPost.community.local).toBe(true);
109 expect(betaPost.creator.local).toBe(false);
110 expect(betaPost.counts.score).toBe(0);
111 assertPostFederation(betaPost, postRes.post_view);
114 test('Update a post', async () => {
115 let postRes = await createPost(alpha, betaCommunity.community.id);
117 let updatedName = 'A jest test federated post, updated';
118 let updatedPost = await editPost(alpha, postRes.post_view.post);
119 expect(updatedPost.post_view.post.name).toBe(updatedName);
120 expect(updatedPost.post_view.community.local).toBe(false);
121 expect(updatedPost.post_view.creator.local).toBe(true);
123 // Make sure that post is updated on beta
124 let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
125 expect(betaPost.community.local).toBe(true);
126 expect(betaPost.creator.local).toBe(false);
127 expect(betaPost.post.name).toBe(updatedName);
128 assertPostFederation(betaPost, updatedPost.post_view);
130 // Make sure lemmy beta cannot update the post
131 let updatedPostBeta = await editPost(beta, betaPost.post);
132 expect(updatedPostBeta).toStrictEqual({ error: 'no_post_edit_allowed' });
135 test('Sticky a post', async () => {
136 let postRes = await createPost(alpha, betaCommunity.community.id);
138 let betaPost1 = (await resolvePost(beta, postRes.post_view.post)).post;
139 let stickiedPostRes = await stickyPost(beta, true, betaPost1.post);
140 expect(stickiedPostRes.post_view.post.stickied).toBe(true);
142 // Make sure that post is stickied on beta
143 let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
144 expect(betaPost.community.local).toBe(true);
145 expect(betaPost.creator.local).toBe(false);
146 expect(betaPost.post.stickied).toBe(true);
149 let unstickiedPost = await stickyPost(beta, false, betaPost1.post);
150 expect(unstickiedPost.post_view.post.stickied).toBe(false);
152 // Make sure that post is unstickied on beta
153 let betaPost2 = (await resolvePost(beta, postRes.post_view.post)).post;
154 expect(betaPost2.community.local).toBe(true);
155 expect(betaPost2.creator.local).toBe(false);
156 expect(betaPost2.post.stickied).toBe(false);
158 // Make sure that gamma cannot sticky the post on beta
159 let gammaPost = (await resolvePost(gamma, postRes.post_view.post)).post;
160 let gammaTrySticky = await stickyPost(gamma, true, gammaPost.post);
161 let betaPost3 = (await resolvePost(beta, postRes.post_view.post)).post;
162 expect(gammaTrySticky.post_view.post.stickied).toBe(true);
163 expect(betaPost3.post.stickied).toBe(false);
166 test('Lock a post', async () => {
167 await followCommunity(alpha, true, betaCommunity.community.id);
168 let postRes = await createPost(alpha, betaCommunity.community.id);
171 let betaPost1 = (await resolvePost(beta, postRes.post_view.post)).post;
172 let lockedPostRes = await lockPost(beta, true, betaPost1.post);
173 expect(lockedPostRes.post_view.post.locked).toBe(true);
175 // Make sure that post is locked on alpha
176 let searchAlpha = await searchPostLocal(alpha, postRes.post_view.post);
177 let alphaPost1 = searchAlpha.posts[0];
178 expect(alphaPost1.post.locked).toBe(true);
180 // Try to make a new comment there, on alpha
181 let comment: any = await createComment(alpha, alphaPost1.post.id);
182 expect(comment['error']).toBe('locked');
185 let unlockedPost = await lockPost(beta, false, betaPost1.post);
186 expect(unlockedPost.post_view.post.locked).toBe(false);
188 // Make sure that post is unlocked on alpha
189 let searchAlpha2 = await searchPostLocal(alpha, postRes.post_view.post);
190 let alphaPost2 = searchAlpha2.posts[0];
191 expect(alphaPost2.community.local).toBe(false);
192 expect(alphaPost2.creator.local).toBe(true);
193 expect(alphaPost2.post.locked).toBe(false);
195 // Try to create a new comment, on alpha
196 let commentAlpha = await createComment(alpha, alphaPost1.post.id);
197 expect(commentAlpha).toBeDefined();
200 test('Delete a post', async () => {
201 let postRes = await createPost(alpha, betaCommunity.community.id);
202 expect(postRes.post_view.post).toBeDefined();
204 let deletedPost = await deletePost(alpha, true, postRes.post_view.post);
205 expect(deletedPost.post_view.post.deleted).toBe(true);
206 expect(deletedPost.post_view.post.name).toBe(postRes.post_view.post.name);
208 // Make sure lemmy beta sees post is deleted
209 let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
210 // This will be undefined because of the tombstone
211 expect(betaPost).toBeUndefined();
214 let undeletedPost = await deletePost(alpha, false, postRes.post_view.post);
215 expect(undeletedPost.post_view.post.deleted).toBe(false);
217 // Make sure lemmy beta sees post is undeleted
218 let betaPost2 = (await resolvePost(beta, postRes.post_view.post)).post;
219 expect(betaPost2.post.deleted).toBe(false);
220 assertPostFederation(betaPost2, undeletedPost.post_view);
222 // Make sure lemmy beta cannot delete the post
223 let deletedPostBeta = await deletePost(beta, true, betaPost2.post);
224 expect(deletedPostBeta).toStrictEqual({ error: 'no_post_edit_allowed' });
227 test('Remove a post from admin and community on different instance', async () => {
228 let postRes = await createPost(gamma, betaCommunity.community.id);
230 let alphaPost = (await resolvePost(alpha, postRes.post_view.post)).post;
231 let removedPost = await removePost(alpha, true, alphaPost.post);
232 expect(removedPost.post_view.post.removed).toBe(true);
233 expect(removedPost.post_view.post.name).toBe(postRes.post_view.post.name);
235 // Make sure lemmy beta sees post is NOT removed
236 let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
237 expect(betaPost.post.removed).toBe(false);
240 let undeletedPost = await removePost(alpha, false, alphaPost.post);
241 expect(undeletedPost.post_view.post.removed).toBe(false);
243 // Make sure lemmy beta sees post is undeleted
244 let betaPost2 = (await resolvePost(beta, postRes.post_view.post)).post;
245 expect(betaPost2.post.removed).toBe(false);
246 assertPostFederation(betaPost2, undeletedPost.post_view);
249 test('Remove a post from admin and community on same instance', async () => {
250 await followBeta(alpha);
251 let postRes = await createPost(alpha, betaCommunity.community.id);
252 expect(postRes.post_view.post).toBeDefined();
254 // Get the id for beta
255 let searchBeta = await searchPostLocal(beta, postRes.post_view.post);
256 let betaPost = searchBeta.posts[0];
257 expect(betaPost).toBeDefined();
259 // The beta admin removes it (the community lives on beta)
260 let removePostRes = await removePost(beta, true, betaPost.post);
261 expect(removePostRes.post_view.post.removed).toBe(true);
263 // Make sure lemmy alpha sees post is removed
264 let alphaPost = await getPost(alpha, postRes.post_view.post.id);
265 // expect(alphaPost.post_view.post.removed).toBe(true); // TODO this shouldn't be commented
266 // assertPostFederation(alphaPost.post_view, removePostRes.post_view);
269 let undeletedPost = await removePost(beta, false, betaPost.post);
270 expect(undeletedPost.post_view.post.removed).toBe(false);
272 // Make sure lemmy alpha sees post is undeleted
273 let alphaPost2 = await getPost(alpha, postRes.post_view.post.id);
274 expect(alphaPost2.post_view.post.removed).toBe(false);
275 assertPostFederation(alphaPost2.post_view, undeletedPost.post_view);
276 await unfollowRemotes(alpha);
279 test('Search for a post', async () => {
280 await unfollowRemotes(alpha);
281 let postRes = await createPost(alpha, betaCommunity.community.id);
282 expect(postRes.post_view.post).toBeDefined();
284 let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
286 expect(betaPost.post.name).toBeDefined();
289 test('Enforce site ban for federated user', async () => {
290 // create a test user
291 let alphaUserJwt = await registerUser(alpha);
292 expect(alphaUserJwt).toBeDefined();
293 let alpha_user: API = {
294 client: alpha.client,
295 auth: alphaUserJwt.jwt,
297 let alphaUserActorId = (await getSite(alpha_user)).my_user.local_user_view.person.actor_id;
298 expect(alphaUserActorId).toBeDefined();
299 let alphaPerson = (await resolvePerson(alpha_user, alphaUserActorId)).person;
300 expect(alphaPerson).toBeDefined();
302 // alpha makes post in beta community, it federates to beta instance
303 let postRes1 = await createPost(alpha_user, betaCommunity.community.id);
304 let searchBeta1 = await searchPostLocal(beta, postRes1.post_view.post);
305 expect(searchBeta1.posts[0]).toBeDefined();
307 // ban alpha from its instance
308 let banAlpha = await banPersonFromSite(alpha, alphaPerson.person.id, true, true);
309 expect(banAlpha.banned).toBe(true);
311 // alpha ban should be federated to beta
312 let alphaUserOnBeta1 = await resolvePerson(beta, alphaUserActorId);
313 expect(alphaUserOnBeta1.person.person.banned).toBe(true);
315 // existing alpha post should be removed on beta
316 let searchBeta2 = await searchPostLocal(beta, postRes1.post_view.post);
317 expect(searchBeta2.posts[0]).toBeUndefined();
320 let unBanAlpha = await banPersonFromSite(alpha, alphaPerson.person.id, false, false);
321 expect(unBanAlpha.banned).toBe(false);
323 // alpha makes new post in beta community, it federates
324 let postRes2 = await createPost(alpha_user, betaCommunity.community.id);
325 let searchBeta3 = await searchPostLocal(beta, postRes2.post_view.post);
326 expect(searchBeta3.posts[0]).toBeDefined();
328 let alphaUserOnBeta2 = await resolvePerson(beta, alphaUserActorId)
329 expect(alphaUserOnBeta2.person.person.banned).toBe(false);
332 test('Enforce community ban for federated user', async () => {
333 let alphaShortname = `@lemmy_alpha@lemmy-alpha:8541`;
334 let alphaPerson = (await resolvePerson(beta, alphaShortname)).person;
335 expect(alphaPerson).toBeDefined();
337 // make a post in beta, it goes through
338 let postRes1 = await createPost(alpha, betaCommunity.community.id);
339 let searchBeta1 = await searchPostLocal(beta, postRes1.post_view.post);
340 expect(searchBeta1.posts[0]).toBeDefined();
342 // ban alpha from beta community
343 let banAlpha = await banPersonFromCommunity(beta, alphaPerson.person.id, 2, true, true);
344 expect(banAlpha.banned).toBe(true);
346 // ensure that the post by alpha got removed
347 let searchAlpha1 = await searchPostLocal(alpha, postRes1.post_view.post);
348 expect(searchAlpha1.posts[0]).toBeUndefined();
350 // Alpha tries to make post on beta, but it fails because of ban
351 let postRes2 = await createPost(alpha, betaCommunity.community.id);
352 expect(postRes2.post_view).toBeUndefined();
355 let unBanAlpha = await banPersonFromCommunity(
357 alphaPerson.person.id,
362 expect(unBanAlpha.banned).toBe(false);
363 let postRes3 = await createPost(alpha, betaCommunity.community.id);
364 expect(postRes3.post_view.post).toBeDefined();
365 expect(postRes3.post_view.community.local).toBe(false);
366 expect(postRes3.post_view.creator.local).toBe(true);
367 expect(postRes3.post_view.counts.score).toBe(1);
369 // Make sure that post makes it to beta community
370 let searchBeta2 = await searchPostLocal(beta, postRes3.post_view.post);
371 expect(searchBeta2.posts[0]).toBeDefined();
375 test('A and G subscribe to B (center) A posts, it gets announced to G', async () => {
376 let postRes = await createPost(alpha, betaCommunity.community.id);
377 expect(postRes.post_view.post).toBeDefined();
379 let betaPost = (await resolvePost(gamma, postRes.post_view.post)).post;
380 expect(betaPost.post.name).toBeDefined();
383 test('Report a post', async () => {
384 let betaCommunity = (await resolveBetaCommunity(beta)).community;
385 let postRes = await createPost(beta, betaCommunity.community.id);
386 expect(postRes.post_view.post).toBeDefined();
388 let alphaPost = (await resolvePost(alpha, postRes.post_view.post)).post;
389 let alphaReport = (await reportPost(alpha, alphaPost.post.id, randomString(10)))
390 .post_report_view.post_report;
392 let betaReport = (await listPostReports(beta)).post_reports[0].post_report;
393 expect(betaReport).toBeDefined();
394 expect(betaReport.resolved).toBe(false);
395 expect(betaReport.original_post_name).toBe(alphaReport.original_post_name);
396 expect(betaReport.original_post_url).toBe(alphaReport.original_post_url);
397 expect(betaReport.original_post_body).toBe(alphaReport.original_post_body);
398 expect(betaReport.reason).toBe(alphaReport.reason);