1 jest.setTimeout(120000);
16 searchForBetaCommunity,
29 import { PostView } from 'lemmy-js-client';
31 beforeAll(async () => {
33 await followBeta(alpha);
34 await followBeta(gamma);
35 await followBeta(delta);
36 await followBeta(epsilon);
40 afterAll(async () => {
41 await unfollowRemotes(alpha);
42 await unfollowRemotes(gamma);
43 await unfollowRemotes(delta);
44 await unfollowRemotes(epsilon);
47 function assertPostFederation(postOne: PostView, postTwo: PostView) {
48 expect(postOne.post.ap_id).toBe(postTwo.post.ap_id);
49 expect(postOne.post.name).toBe(postTwo.post.name);
50 expect(postOne.post.body).toBe(postTwo.post.body);
51 expect(postOne.post.url).toBe(postTwo.post.url);
52 expect(postOne.post.nsfw).toBe(postTwo.post.nsfw);
53 expect(postOne.post.embed_title).toBe(postTwo.post.embed_title);
54 expect(postOne.post.embed_description).toBe(postTwo.post.embed_description);
55 expect(postOne.post.embed_html).toBe(postTwo.post.embed_html);
56 expect(postOne.post.published).toBe(postTwo.post.published);
57 expect(postOne.community.actor_id).toBe(postTwo.community.actor_id);
58 expect(postOne.post.locked).toBe(postTwo.post.locked);
59 expect(postOne.post.removed).toBe(postTwo.post.removed);
60 expect(postOne.post.deleted).toBe(postTwo.post.deleted);
63 test('Create a post', async () => {
64 let search = await searchForBetaCommunity(alpha);
66 let postRes = await createPost(alpha, search.communities[0].community.id);
67 expect(postRes.post_view.post).toBeDefined();
68 expect(postRes.post_view.community.local).toBe(false);
69 expect(postRes.post_view.creator.local).toBe(true);
70 expect(postRes.post_view.counts.score).toBe(1);
73 // Make sure that post is liked on beta
74 let searchBeta = await searchPost(beta, postRes.post_view.post);
75 let betaPost = searchBeta.posts[0];
77 expect(betaPost).toBeDefined();
78 expect(betaPost.community.local).toBe(true);
79 expect(betaPost.creator.local).toBe(false);
80 expect(betaPost.counts.score).toBe(1);
81 assertPostFederation(betaPost, postRes.post_view);
83 // Delta only follows beta, so it should not see an alpha ap_id
84 let searchDelta = await searchPost(delta, postRes.post_view.post);
85 expect(searchDelta.posts[0]).toBeUndefined();
87 // Epsilon has alpha blocked, it should not see the alpha post
88 let searchEpsilon = await searchPost(epsilon, postRes.post_view.post);
89 expect(searchEpsilon.posts[0]).toBeUndefined();
92 test('Create a post in a non-existent community', async () => {
93 let postRes = await createPost(alpha, -2);
94 expect(postRes).toStrictEqual({ error: 'couldnt_create_post' });
97 test('Unlike a post', async () => {
98 let search = await searchForBetaCommunity(alpha);
99 let postRes = await createPost(alpha, search.communities[0].community.id);
101 let unlike = await likePost(alpha, 0, postRes.post_view.post);
102 expect(unlike.post_view.counts.score).toBe(0);
105 // Try to unlike it again, make sure it stays at 0
106 let unlike2 = await likePost(alpha, 0, postRes.post_view.post);
107 expect(unlike2.post_view.counts.score).toBe(0);
110 // Make sure that post is unliked on beta
111 let searchBeta = await searchPost(beta, postRes.post_view.post);
112 let betaPost = searchBeta.posts[0];
114 expect(betaPost).toBeDefined();
115 expect(betaPost.community.local).toBe(true);
116 expect(betaPost.creator.local).toBe(false);
117 expect(betaPost.counts.score).toBe(0);
118 assertPostFederation(betaPost, postRes.post_view);
121 test('Update a post', async () => {
122 let search = await searchForBetaCommunity(alpha);
123 let postRes = await createPost(alpha, search.communities[0].community.id);
126 let updatedName = 'A jest test federated post, updated';
127 let updatedPost = await editPost(alpha, postRes.post_view.post);
128 expect(updatedPost.post_view.post.name).toBe(updatedName);
129 expect(updatedPost.post_view.community.local).toBe(false);
130 expect(updatedPost.post_view.creator.local).toBe(true);
133 // Make sure that post is updated on beta
134 let searchBeta = await searchPost(beta, postRes.post_view.post);
135 let betaPost = searchBeta.posts[0];
136 expect(betaPost.community.local).toBe(true);
137 expect(betaPost.creator.local).toBe(false);
138 expect(betaPost.post.name).toBe(updatedName);
139 assertPostFederation(betaPost, updatedPost.post_view);
142 // Make sure lemmy beta cannot update the post
143 let updatedPostBeta = await editPost(beta, betaPost.post);
144 expect(updatedPostBeta).toStrictEqual({ error: 'no_post_edit_allowed' });
147 test('Sticky a post', async () => {
148 let search = await searchForBetaCommunity(alpha);
149 let postRes = await createPost(alpha, search.communities[0].community.id);
152 let stickiedPostRes = await stickyPost(alpha, true, postRes.post_view.post);
153 expect(stickiedPostRes.post_view.post.stickied).toBe(true);
156 // Make sure that post is stickied on beta
157 let searchBeta = await searchPost(beta, postRes.post_view.post);
158 let betaPost = searchBeta.posts[0];
159 expect(betaPost.community.local).toBe(true);
160 expect(betaPost.creator.local).toBe(false);
161 expect(betaPost.post.stickied).toBe(true);
164 let unstickiedPost = await stickyPost(alpha, false, postRes.post_view.post);
165 expect(unstickiedPost.post_view.post.stickied).toBe(false);
168 // Make sure that post is unstickied on beta
169 let searchBeta2 = await searchPost(beta, postRes.post_view.post);
170 let betaPost2 = searchBeta2.posts[0];
171 expect(betaPost2.community.local).toBe(true);
172 expect(betaPost2.creator.local).toBe(false);
173 expect(betaPost2.post.stickied).toBe(false);
175 // Make sure that gamma cannot sticky the post on beta
176 let searchGamma = await searchPost(gamma, postRes.post_view.post);
177 let gammaPost = searchGamma.posts[0];
178 let gammaTrySticky = await stickyPost(gamma, true, gammaPost.post);
180 let searchBeta3 = await searchPost(beta, postRes.post_view.post);
181 let betaPost3 = searchBeta3.posts[0];
182 expect(gammaTrySticky.post_view.post.stickied).toBe(true);
183 expect(betaPost3.post.stickied).toBe(false);
186 test('Lock a post', async () => {
187 let search = await searchForBetaCommunity(alpha);
189 let postRes = await createPost(alpha, search.communities[0].community.id);
193 let lockedPostRes = await lockPost(alpha, true, postRes.post_view.post);
194 expect(lockedPostRes.post_view.post.locked).toBe(true);
197 // Make sure that post is locked on beta
198 let searchBeta = await searchPostLocal(beta, postRes.post_view.post);
199 let betaPost1 = searchBeta.posts[0];
200 expect(betaPost1.post.locked).toBe(true);
203 // Try to make a new comment there, on alpha
204 let comment: any = await createComment(alpha, postRes.post_view.post.id);
205 expect(comment['error']).toBe('locked');
209 let unlockedPost = await lockPost(alpha, false, postRes.post_view.post);
210 expect(unlockedPost.post_view.post.locked).toBe(false);
213 // Make sure that post is unlocked on beta
214 let searchBeta2 = await searchPost(beta, postRes.post_view.post);
215 let betaPost2 = searchBeta2.posts[0];
216 expect(betaPost2.community.local).toBe(true);
217 expect(betaPost2.creator.local).toBe(false);
218 expect(betaPost2.post.locked).toBe(false);
220 // Try to create a new comment, on beta
221 let commentBeta = await createComment(beta, betaPost2.post.id);
222 expect(commentBeta).toBeDefined();
225 test('Delete a post', async () => {
226 let search = await searchForBetaCommunity(alpha);
227 let postRes = await createPost(alpha, search.communities[0].community.id);
230 let deletedPost = await deletePost(alpha, true, postRes.post_view.post);
231 expect(deletedPost.post_view.post.deleted).toBe(true);
234 // Make sure lemmy beta sees post is deleted
235 let searchBeta = await searchPost(beta, postRes.post_view.post);
236 let betaPost = searchBeta.posts[0];
237 // This will be undefined because of the tombstone
238 expect(betaPost).toBeUndefined();
242 let undeletedPost = await deletePost(alpha, false, postRes.post_view.post);
243 expect(undeletedPost.post_view.post.deleted).toBe(false);
246 // Make sure lemmy beta sees post is undeleted
247 let searchBeta2 = await searchPost(beta, postRes.post_view.post);
248 let betaPost2 = searchBeta2.posts[0];
249 expect(betaPost2.post.deleted).toBe(false);
250 assertPostFederation(betaPost2, undeletedPost.post_view);
252 // Make sure lemmy beta cannot delete the post
253 let deletedPostBeta = await deletePost(beta, true, betaPost2.post);
254 expect(deletedPostBeta).toStrictEqual({ error: 'no_post_edit_allowed' });
257 test('Remove a post from admin and community on different instance', async () => {
258 let search = await searchForBetaCommunity(alpha);
259 let postRes = await createPost(alpha, search.communities[0].community.id);
262 let removedPost = await removePost(alpha, true, postRes.post_view.post);
263 expect(removedPost.post_view.post.removed).toBe(true);
266 // Make sure lemmy beta sees post is NOT removed
267 let searchBeta = await searchPost(beta, postRes.post_view.post);
268 let betaPost = searchBeta.posts[0];
269 expect(betaPost.post.removed).toBe(false);
273 let undeletedPost = await removePost(alpha, false, postRes.post_view.post);
274 expect(undeletedPost.post_view.post.removed).toBe(false);
277 // Make sure lemmy beta sees post is undeleted
278 let searchBeta2 = await searchPost(beta, postRes.post_view.post);
279 let betaPost2 = searchBeta2.posts[0];
280 expect(betaPost2.post.removed).toBe(false);
281 assertPostFederation(betaPost2, undeletedPost.post_view);
284 test('Remove a post from admin and community on same instance', async () => {
285 let search = await searchForBetaCommunity(alpha);
286 let postRes = await createPost(alpha, search.communities[0].community.id);
289 // Get the id for beta
290 let searchBeta = await searchPost(beta, postRes.post_view.post);
291 let betaPost = searchBeta.posts[0];
294 // The beta admin removes it (the community lives on beta)
295 let removePostRes = await removePost(beta, true, betaPost.post);
296 expect(removePostRes.post_view.post.removed).toBe(true);
299 // Make sure lemmy alpha sees post is removed
300 let alphaPost = await getPost(alpha, postRes.post_view.post.id);
301 expect(alphaPost.post_view.post.removed).toBe(true);
302 assertPostFederation(alphaPost.post_view, removePostRes.post_view);
306 let undeletedPost = await removePost(beta, false, betaPost.post);
307 expect(undeletedPost.post_view.post.removed).toBe(false);
310 // Make sure lemmy alpha sees post is undeleted
311 let alphaPost2 = await getPost(alpha, postRes.post_view.post.id);
313 expect(alphaPost2.post_view.post.removed).toBe(false);
314 assertPostFederation(alphaPost2.post_view, undeletedPost.post_view);
317 test('Search for a post', async () => {
318 let search = await searchForBetaCommunity(alpha);
320 let postRes = await createPost(alpha, search.communities[0].community.id);
322 let searchBeta = await searchPost(beta, postRes.post_view.post);
324 expect(searchBeta.posts[0].post.name).toBeDefined();
327 test('A and G subscribe to B (center) A posts, it gets announced to G', async () => {
328 let search = await searchForBetaCommunity(alpha);
329 let postRes = await createPost(alpha, search.communities[0].community.id);
332 let search2 = await searchPost(gamma, postRes.post_view.post);
333 expect(search2.posts[0].post.name).toBeDefined();
336 test('Enforce site ban for federated user', async () => {
337 let alphaShortname = `@lemmy_alpha@lemmy-alpha:8541`;
338 let userSearch = await searchForUser(beta, alphaShortname);
339 let alphaUser = userSearch.users[0];
340 expect(alphaUser).toBeDefined();
343 // ban alpha from beta site
344 let banAlpha = await banUserFromSite(beta, alphaUser.user.id, true);
345 expect(banAlpha.banned).toBe(true);
348 // Alpha makes post on beta
349 let search = await searchForBetaCommunity(alpha);
351 let postRes = await createPost(alpha, search.communities[0].community.id);
352 expect(postRes.post_view.post).toBeDefined();
353 expect(postRes.post_view.community.local).toBe(false);
354 expect(postRes.post_view.creator.local).toBe(true);
355 expect(postRes.post_view.counts.score).toBe(1);
358 // Make sure that post doesn't make it to beta
359 let searchBeta = await searchPostLocal(beta, postRes.post_view.post);
360 let betaPost = searchBeta.posts[0];
361 expect(betaPost).toBeUndefined();
365 let unBanAlpha = await banUserFromSite(beta, alphaUser.user.id, false);
366 expect(unBanAlpha.banned).toBe(false);
369 test('Enforce community ban for federated user', async () => {
370 let alphaShortname = `@lemmy_alpha@lemmy-alpha:8541`;
371 let userSearch = await searchForUser(beta, alphaShortname);
372 let alphaUser = userSearch.users[0];
373 expect(alphaUser).toBeDefined();
376 // ban alpha from beta site
377 await banUserFromCommunity(beta, alphaUser.user.id, 2, false);
378 let banAlpha = await banUserFromCommunity(beta, alphaUser.user.id, 2, true);
379 expect(banAlpha.banned).toBe(true);
382 // Alpha makes post on beta
383 let search = await searchForBetaCommunity(alpha);
385 let postRes = await createPost(alpha, search.communities[0].community.id);
386 expect(postRes.post_view.post).toBeDefined();
387 expect(postRes.post_view.community.local).toBe(false);
388 expect(postRes.post_view.creator.local).toBe(true);
389 expect(postRes.post_view.counts.score).toBe(1);
392 // Make sure that post doesn't make it to beta community
393 let searchBeta = await searchPostLocal(beta, postRes.post_view.post);
394 let betaPost = searchBeta.posts[0];
395 expect(betaPost).toBeUndefined();
398 let unBanAlpha = await banUserFromCommunity(
404 expect(unBanAlpha.banned).toBe(false);