1 jest.setTimeout(120000);
16 searchForBetaCommunity,
30 } from 'lemmy-js-client';
32 let betaCommunity: Community;
34 beforeAll(async () => {
36 let search = await searchForBetaCommunity(alpha);
37 betaCommunity = search.communities[0];
40 afterAll(async () => {
41 await unfollowRemotes(alpha);
42 await unfollowRemotes(gamma);
43 await unfollowRemotes(delta);
44 await unfollowRemotes(epsilon);
47 function assertPostFederation(
50 expect(postOne.ap_id).toBe(postTwo.ap_id);
51 expect(postOne.name).toBe(postTwo.name);
52 expect(postOne.body).toBe(postTwo.body);
53 expect(postOne.url).toBe(postTwo.url);
54 expect(postOne.nsfw).toBe(postTwo.nsfw);
55 expect(postOne.embed_title).toBe(postTwo.embed_title);
56 expect(postOne.embed_description).toBe(postTwo.embed_description);
57 expect(postOne.embed_html).toBe(postTwo.embed_html);
58 expect(postOne.published).toBe(postTwo.published);
59 expect(postOne.community_actor_id).toBe(postTwo.community_actor_id);
60 expect(postOne.locked).toBe(postTwo.locked);
61 expect(postOne.removed).toBe(postTwo.removed);
62 expect(postOne.deleted).toBe(postTwo.deleted);
65 test('Create a post', async () => {
66 let postRes = await createPost(alpha, betaCommunity.id);
67 expect(postRes.post).toBeDefined();
68 expect(postRes.post.community_local).toBe(false);
69 expect(postRes.post.creator_local).toBe(true);
70 expect(postRes.post.score).toBe(1);
72 // Make sure that post is liked on beta
73 let searchBeta = await searchPost(beta, postRes.post);
74 let betaPost = searchBeta.posts[0];
76 expect(betaPost).toBeDefined();
77 expect(betaPost.community_local).toBe(true);
78 expect(betaPost.creator_local).toBe(false);
79 expect(betaPost.score).toBe(1);
80 assertPostFederation(betaPost, postRes.post);
82 // Delta only follows beta, so it should not see an alpha ap_id
83 let searchDelta = await searchPost(delta, postRes.post);
84 expect(searchDelta.posts[0]).toBeUndefined();
86 // Epsilon has alpha blocked, it should not see the alpha post
87 let searchEpsilon = await searchPost(epsilon, postRes.post);
88 expect(searchEpsilon.posts[0]).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_create_post' });
96 test('Unlike a post', async () => {
97 let postRes = await createPost(alpha, betaCommunity.id);
98 let unlike = await likePost(alpha, 0, postRes.post);
99 expect(unlike.post.score).toBe(0);
101 // Try to unlike it again, make sure it stays at 0
102 let unlike2 = await likePost(alpha, 0, postRes.post);
103 expect(unlike2.post.score).toBe(0);
105 // Make sure that post is unliked on beta
106 let searchBeta = await searchPost(beta, postRes.post);
107 let betaPost = searchBeta.posts[0];
108 expect(betaPost).toBeDefined();
109 expect(betaPost.community_local).toBe(true);
110 expect(betaPost.creator_local).toBe(false);
111 expect(betaPost.score).toBe(0);
112 assertPostFederation(betaPost, postRes.post);
115 test('Update a post', async () => {
116 let postRes = await createPost(alpha, betaCommunity.id);
118 let updatedName = 'A jest test federated post, updated';
119 let updatedPost = await updatePost(alpha, postRes.post);
120 expect(updatedPost.post.name).toBe(updatedName);
121 expect(updatedPost.post.community_local).toBe(false);
122 expect(updatedPost.post.creator_local).toBe(true);
124 // Make sure that post is updated on beta
125 let searchBeta = await searchPost(beta, postRes.post);
126 let betaPost = searchBeta.posts[0];
127 expect(betaPost.community_local).toBe(true);
128 expect(betaPost.creator_local).toBe(false);
129 expect(betaPost.name).toBe(updatedName);
130 assertPostFederation(betaPost, updatedPost.post);
132 // Make sure lemmy beta cannot update the post
133 let updatedPostBeta = await updatePost(beta, betaPost);
134 expect(updatedPostBeta).toStrictEqual({ error: 'no_post_edit_allowed' });
137 test('Sticky a post', async () => {
138 let postRes = await createPost(alpha, betaCommunity.id);
140 let stickiedPostRes = await stickyPost(alpha, true, postRes.post);
141 expect(stickiedPostRes.post.stickied).toBe(true);
142 // Make sure that post is stickied on beta
143 let searchBeta = await searchPost(beta, postRes.post);
144 let betaPost = searchBeta.posts[0];
145 expect(betaPost.community_local).toBe(true);
146 expect(betaPost.creator_local).toBe(false);
147 expect(betaPost.stickied).toBe(true);
150 let unstickiedPost = await stickyPost(alpha, false, postRes.post);
151 expect(unstickiedPost.post.stickied).toBe(false);
153 // Make sure that post is unstickied on beta
154 let searchBeta2 = await searchPost(beta, postRes.post);
155 let betaPost2 = searchBeta2.posts[0];
156 expect(betaPost2.community_local).toBe(true);
157 expect(betaPost2.creator_local).toBe(false);
158 expect(betaPost2.stickied).toBe(false);
160 // Make sure that gamma cannot sticky the post on beta
161 let searchGamma = await searchPost(gamma, postRes.post);
162 let gammaPost = searchGamma.posts[0];
163 let gammaTrySticky = await stickyPost(gamma, true, gammaPost);
164 let searchBeta3 = await searchPost(beta, postRes.post);
165 let betaPost3 = searchBeta3.posts[0];
166 expect(gammaTrySticky.post.stickied).toBe(true);
167 expect(betaPost3.stickied).toBe(false);
170 test('Lock a post', async () => {
171 let postRes = await createPost(alpha, betaCommunity.id);
174 let lockedPostRes = await lockPost(alpha, true, postRes.post);
175 expect(lockedPostRes.post.locked).toBe(true);
177 // Make sure that post is locked on beta
178 let searchBeta = await searchPostLocal(beta, postRes.post);
179 let betaPost1 = searchBeta.posts[0];
180 expect(betaPost1.locked).toBe(true);
182 // Try to make a new comment there, on alpha
183 let comment = await createComment(alpha, postRes.post.id);
184 expect(comment['error']).toBe('locked');
187 let unlockedPost = await lockPost(alpha, false, postRes.post);
188 expect(unlockedPost.post.locked).toBe(false);
190 // Make sure that post is unlocked on beta
191 let searchBeta2 = await searchPost(beta, postRes.post);
192 let betaPost2 = searchBeta2.posts[0];
193 expect(betaPost2.community_local).toBe(true);
194 expect(betaPost2.creator_local).toBe(false);
195 expect(betaPost2.locked).toBe(false);
197 // Try to create a new comment, on beta
198 let commentBeta = await createComment(beta, betaPost2.id);
199 expect(commentBeta).toBeDefined();
202 test('Delete a post', async () => {
203 let postRes = await createPost(alpha, betaCommunity.id);
204 expect(postRes.post).toBeDefined();
206 let deletedPost = await deletePost(alpha, true, postRes.post);
207 expect(deletedPost.post.deleted).toBe(true);
209 // Make sure lemmy beta sees post is deleted
210 let searchBeta = await searchPost(beta, postRes.post);
211 let betaPost = searchBeta.posts[0];
212 // This will be undefined because of the tombstone
213 expect(betaPost).toBeUndefined();
216 let undeletedPost = await deletePost(alpha, false, postRes.post);
217 expect(undeletedPost.post.deleted).toBe(false);
219 // Make sure lemmy beta sees post is undeleted
220 let searchBeta2 = await searchPost(beta, postRes.post);
221 let betaPost2 = searchBeta2.posts[0];
222 expect(betaPost2.deleted).toBe(false);
223 assertPostFederation(betaPost2, undeletedPost.post);
225 // Make sure lemmy beta cannot delete the post
226 let deletedPostBeta = await deletePost(beta, true, betaPost2);
227 expect(deletedPostBeta).toStrictEqual({ error: 'no_post_edit_allowed' });
230 test('Remove a post from admin and community on different instance', async () => {
231 let postRes = await createPost(alpha, betaCommunity.id);
233 let removedPost = await removePost(alpha, true, postRes.post);
234 expect(removedPost.post.removed).toBe(true);
236 // Make sure lemmy beta sees post is NOT removed
237 let searchBeta = await searchPost(beta, postRes.post);
238 let betaPost = searchBeta.posts[0];
239 expect(betaPost.removed).toBe(false);
242 let undeletedPost = await removePost(alpha, false, postRes.post);
243 expect(undeletedPost.post.removed).toBe(false);
245 // Make sure lemmy beta sees post is undeleted
246 let searchBeta2 = await searchPost(beta, postRes.post);
247 let betaPost2 = searchBeta2.posts[0];
248 expect(betaPost2.removed).toBe(false);
249 assertPostFederation(betaPost2, undeletedPost.post);
252 test('Remove a post from admin and community on same instance', async () => {
253 await followBeta(alpha);
254 let postRes = await createPost(alpha, betaCommunity.id);
256 // Get the id for beta
257 let searchBeta = await searchPost(beta, postRes.post);
258 let betaPost = searchBeta.posts[0];
260 // The beta admin removes it (the community lives on beta)
261 let removePostRes = await removePost(beta, true, betaPost);
262 expect(removePostRes.post.removed).toBe(true);
264 // Make sure lemmy alpha sees post is removed
265 let alphaPost = await getPost(alpha, postRes.post.id);
266 expect(alphaPost.post.removed).toBe(true);
267 assertPostFederation(alphaPost.post, removePostRes.post);
270 let undeletedPost = await removePost(beta, false, betaPost);
271 expect(undeletedPost.post.removed).toBe(false);
273 // Make sure lemmy alpha sees post is undeleted
274 let alphaPost2 = await getPost(alpha, postRes.post.id);
275 expect(alphaPost2.post.removed).toBe(false);
276 assertPostFederation(alphaPost2.post, undeletedPost.post);
277 await unfollowRemotes(alpha);
280 test('Search for a post', async () => {
281 let postRes = await createPost(alpha, betaCommunity.id);
282 expect(postRes.post).toBeDefined();
284 let searchBeta = await searchPost(beta, postRes.post);
286 expect(searchBeta.posts[0].name).toBeDefined();
289 test('A and G subscribe to B (center) A posts, it gets announced to G', async () => {
290 let postRes = await createPost(alpha, betaCommunity.id);
292 let search2 = await searchPost(gamma, postRes.post);
293 expect(search2.posts[0].name).toBeDefined();
296 test('Enforce site ban for federated user', async () => {
298 let alphaShortname = `@lemmy_alpha@lemmy-alpha:8541`;
299 let userSearch = await searchForUser(beta, alphaShortname);
300 let alphaUser = userSearch.users[0];
301 expect(alphaUser).toBeDefined();
303 // ban alpha from beta site
304 let banAlpha = await banUserFromSite(beta, alphaUser.id, true);
305 expect(banAlpha.banned).toBe(true);
307 // Alpha makes post on beta
308 let postRes = await createPost(alpha, betaCommunity.id);
309 expect(postRes.post).toBeDefined();
310 expect(postRes.post.community_local).toBe(false);
311 expect(postRes.post.creator_local).toBe(true);
312 expect(postRes.post.score).toBe(1);
314 // Make sure that post doesn't make it to beta
315 let searchBeta = await searchPostLocal(beta, postRes.post);
316 let betaPost = searchBeta.posts[0];
317 expect(betaPost).toBeUndefined();
320 let unBanAlpha = await banUserFromSite(beta, alphaUser.id, false);
321 expect(unBanAlpha.banned).toBe(false);
324 test('Enforce community ban for federated user', async () => {
325 let alphaShortname = `@lemmy_alpha@lemmy-alpha:8541`;
326 let userSearch = await searchForUser(beta, alphaShortname);
327 let alphaUser = userSearch.users[0];
328 expect(alphaUser).toBeDefined();
330 // ban alpha from beta site
331 await banUserFromCommunity(beta, alphaUser.id, 2, false);
332 let banAlpha = await banUserFromCommunity(beta, alphaUser.id, 2, true);
333 expect(banAlpha.banned).toBe(true);
335 // Alpha makes post on beta
336 let postRes = await createPost(alpha, betaCommunity.id);
337 expect(postRes.post).toBeDefined();
338 expect(postRes.post.community_local).toBe(false);
339 expect(postRes.post.creator_local).toBe(true);
340 expect(postRes.post.score).toBe(1);
342 // Make sure that post doesn't make it to beta community
343 let searchBeta = await searchPostLocal(beta, postRes.post);
344 let betaPost = searchBeta.posts[0];
345 expect(betaPost).toBeUndefined();
348 let unBanAlpha = await banUserFromCommunity(beta, alphaUser.id, 2, false);
349 expect(unBanAlpha.banned).toBe(false);