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];
41 afterAll(async () => {
45 async function unfollows() {
46 await unfollowRemotes(alpha);
47 await unfollowRemotes(gamma);
48 await unfollowRemotes(delta);
49 await unfollowRemotes(epsilon);
52 function assertPostFederation(
55 expect(postOne.ap_id).toBe(postTwo.ap_id);
56 expect(postOne.name).toBe(postTwo.name);
57 expect(postOne.body).toBe(postTwo.body);
58 expect(postOne.url).toBe(postTwo.url);
59 expect(postOne.nsfw).toBe(postTwo.nsfw);
60 expect(postOne.embed_title).toBe(postTwo.embed_title);
61 expect(postOne.embed_description).toBe(postTwo.embed_description);
62 expect(postOne.embed_html).toBe(postTwo.embed_html);
63 expect(postOne.published).toBe(postTwo.published);
64 expect(postOne.community_actor_id).toBe(postTwo.community_actor_id);
65 expect(postOne.locked).toBe(postTwo.locked);
66 expect(postOne.removed).toBe(postTwo.removed);
67 expect(postOne.deleted).toBe(postTwo.deleted);
70 test('Create a post', async () => {
71 let postRes = await createPost(alpha, betaCommunity.id);
72 expect(postRes.post).toBeDefined();
73 expect(postRes.post.community_local).toBe(false);
74 expect(postRes.post.creator_local).toBe(true);
75 expect(postRes.post.score).toBe(1);
77 // Make sure that post is liked on beta
78 let searchBeta = await searchPost(beta, postRes.post);
79 let betaPost = searchBeta.posts[0];
81 expect(betaPost).toBeDefined();
82 expect(betaPost.community_local).toBe(true);
83 expect(betaPost.creator_local).toBe(false);
84 expect(betaPost.score).toBe(1);
85 assertPostFederation(betaPost, postRes.post);
87 // Delta only follows beta, so it should not see an alpha ap_id
88 let searchDelta = await searchPost(delta, postRes.post);
89 expect(searchDelta.posts[0]).toBeUndefined();
91 // Epsilon has alpha blocked, it should not see the alpha post
92 let searchEpsilon = await searchPost(epsilon, postRes.post);
93 expect(searchEpsilon.posts[0]).toBeUndefined();
96 test('Create a post in a non-existent community', async () => {
97 let postRes = await createPost(alpha, -2);
98 expect(postRes).toStrictEqual({ error: 'couldnt_create_post' });
101 test('Unlike a post', async () => {
102 let postRes = await createPost(alpha, betaCommunity.id);
103 let unlike = await likePost(alpha, 0, postRes.post);
104 expect(unlike.post.score).toBe(0);
106 // Try to unlike it again, make sure it stays at 0
107 let unlike2 = await likePost(alpha, 0, postRes.post);
108 expect(unlike2.post.score).toBe(0);
110 // Make sure that post is unliked on beta
111 let searchBeta = await searchPost(beta, postRes.post);
112 let betaPost = searchBeta.posts[0];
113 expect(betaPost).toBeDefined();
114 expect(betaPost.community_local).toBe(true);
115 expect(betaPost.creator_local).toBe(false);
116 expect(betaPost.score).toBe(0);
117 assertPostFederation(betaPost, postRes.post);
120 test('Update a post', async () => {
121 let postRes = await createPost(alpha, betaCommunity.id);
123 let updatedName = 'A jest test federated post, updated';
124 let updatedPost = await updatePost(alpha, postRes.post);
125 expect(updatedPost.post.name).toBe(updatedName);
126 expect(updatedPost.post.community_local).toBe(false);
127 expect(updatedPost.post.creator_local).toBe(true);
129 // Make sure that post is updated on beta
130 let searchBeta = await searchPost(beta, postRes.post);
131 let betaPost = searchBeta.posts[0];
132 expect(betaPost.community_local).toBe(true);
133 expect(betaPost.creator_local).toBe(false);
134 expect(betaPost.name).toBe(updatedName);
135 assertPostFederation(betaPost, updatedPost.post);
137 // Make sure lemmy beta cannot update the post
138 let updatedPostBeta = await updatePost(beta, betaPost);
139 expect(updatedPostBeta).toStrictEqual({ error: 'no_post_edit_allowed' });
142 test('Sticky a post', async () => {
143 let postRes = await createPost(alpha, betaCommunity.id);
145 let stickiedPostRes = await stickyPost(alpha, true, postRes.post);
146 expect(stickiedPostRes.post.stickied).toBe(true);
147 // Make sure that post is stickied on beta
148 let searchBeta = await searchPost(beta, postRes.post);
149 let betaPost = searchBeta.posts[0];
150 expect(betaPost.community_local).toBe(true);
151 expect(betaPost.creator_local).toBe(false);
152 expect(betaPost.stickied).toBe(true);
155 let unstickiedPost = await stickyPost(alpha, false, postRes.post);
156 expect(unstickiedPost.post.stickied).toBe(false);
158 // Make sure that post is unstickied on beta
159 let searchBeta2 = await searchPost(beta, postRes.post);
160 let betaPost2 = searchBeta2.posts[0];
161 expect(betaPost2.community_local).toBe(true);
162 expect(betaPost2.creator_local).toBe(false);
163 expect(betaPost2.stickied).toBe(false);
165 // Make sure that gamma cannot sticky the post on beta
166 let searchGamma = await searchPost(gamma, postRes.post);
167 let gammaPost = searchGamma.posts[0];
168 let gammaTrySticky = await stickyPost(gamma, true, gammaPost);
169 let searchBeta3 = await searchPost(beta, postRes.post);
170 let betaPost3 = searchBeta3.posts[0];
171 expect(gammaTrySticky.post.stickied).toBe(true);
172 expect(betaPost3.stickied).toBe(false);
175 test('Lock a post', async () => {
176 let postRes = await createPost(alpha, betaCommunity.id);
179 let lockedPostRes = await lockPost(alpha, true, postRes.post);
180 expect(lockedPostRes.post.locked).toBe(true);
182 // Make sure that post is locked on beta
183 let searchBeta = await searchPostLocal(beta, postRes.post);
184 let betaPost1 = searchBeta.posts[0];
185 expect(betaPost1.locked).toBe(true);
187 // Try to make a new comment there, on alpha
188 let comment = await createComment(alpha, postRes.post.id);
189 expect(comment['error']).toBe('locked');
192 let unlockedPost = await lockPost(alpha, false, postRes.post);
193 expect(unlockedPost.post.locked).toBe(false);
195 // Make sure that post is unlocked on beta
196 let searchBeta2 = await searchPost(beta, postRes.post);
197 let betaPost2 = searchBeta2.posts[0];
198 expect(betaPost2.community_local).toBe(true);
199 expect(betaPost2.creator_local).toBe(false);
200 expect(betaPost2.locked).toBe(false);
202 // Try to create a new comment, on beta
203 let commentBeta = await createComment(beta, betaPost2.id);
204 expect(commentBeta).toBeDefined();
207 test('Delete a post', async () => {
208 let postRes = await createPost(alpha, betaCommunity.id);
209 expect(postRes.post).toBeDefined();
211 let deletedPost = await deletePost(alpha, true, postRes.post);
212 expect(deletedPost.post.deleted).toBe(true);
214 // Make sure lemmy beta sees post is deleted
215 let searchBeta = await searchPost(beta, postRes.post);
216 let betaPost = searchBeta.posts[0];
217 // This will be undefined because of the tombstone
218 expect(betaPost).toBeUndefined();
221 let undeletedPost = await deletePost(alpha, false, postRes.post);
222 expect(undeletedPost.post.deleted).toBe(false);
224 // Make sure lemmy beta sees post is undeleted
225 let searchBeta2 = await searchPost(beta, postRes.post);
226 let betaPost2 = searchBeta2.posts[0];
227 expect(betaPost2.deleted).toBe(false);
228 assertPostFederation(betaPost2, undeletedPost.post);
230 // Make sure lemmy beta cannot delete the post
231 let deletedPostBeta = await deletePost(beta, true, betaPost2);
232 expect(deletedPostBeta).toStrictEqual({ error: 'no_post_edit_allowed' });
235 test('Remove a post from admin and community on different instance', async () => {
236 let postRes = await createPost(alpha, betaCommunity.id);
238 let removedPost = await removePost(alpha, true, postRes.post);
239 expect(removedPost.post.removed).toBe(true);
241 // Make sure lemmy beta sees post is NOT removed
242 let searchBeta = await searchPost(beta, postRes.post);
243 let betaPost = searchBeta.posts[0];
244 expect(betaPost.removed).toBe(false);
247 let undeletedPost = await removePost(alpha, false, postRes.post);
248 expect(undeletedPost.post.removed).toBe(false);
250 // Make sure lemmy beta sees post is undeleted
251 let searchBeta2 = await searchPost(beta, postRes.post);
252 let betaPost2 = searchBeta2.posts[0];
253 expect(betaPost2.removed).toBe(false);
254 assertPostFederation(betaPost2, undeletedPost.post);
257 test('Remove a post from admin and community on same instance', async () => {
258 await followBeta(alpha);
259 let postRes = await createPost(alpha, betaCommunity.id);
260 expect(postRes.post).toBeDefined();
262 // Get the id for beta
263 let searchBeta = await searchPostLocal(beta, postRes.post);
264 let betaPost = searchBeta.posts[0];
265 expect(betaPost).toBeDefined();
267 // The beta admin removes it (the community lives on beta)
268 let removePostRes = await removePost(beta, true, betaPost);
269 expect(removePostRes.post.removed).toBe(true);
271 // Make sure lemmy alpha sees post is removed
272 let alphaPost = await getPost(alpha, postRes.post.id);
273 // expect(alphaPost.post.removed).toBe(true); // TODO this shouldn't be commented
274 assertPostFederation(alphaPost.post, removePostRes.post);
277 let undeletedPost = await removePost(beta, false, betaPost);
278 expect(undeletedPost.post.removed).toBe(false);
280 // Make sure lemmy alpha sees post is undeleted
281 let alphaPost2 = await getPost(alpha, postRes.post.id);
282 expect(alphaPost2.post.removed).toBe(false);
283 assertPostFederation(alphaPost2.post, undeletedPost.post);
284 await unfollowRemotes(alpha);
287 test('Search for a post', async () => {
288 await unfollowRemotes(alpha);
289 let postRes = await createPost(alpha, betaCommunity.id);
290 expect(postRes.post).toBeDefined();
292 let searchBeta = await searchPost(beta, postRes.post);
294 expect(searchBeta.posts[0].name).toBeDefined();
297 test('A and G subscribe to B (center) A posts, it gets announced to G', async () => {
298 let postRes = await createPost(alpha, betaCommunity.id);
299 expect(postRes.post).toBeDefined();
301 let search2 = await searchPost(gamma, postRes.post);
302 expect(search2.posts[0].name).toBeDefined();
305 test('Enforce site ban for federated user', async () => {
307 let alphaShortname = `@lemmy_alpha@lemmy-alpha:8541`;
308 let userSearch = await searchForUser(beta, alphaShortname);
309 let alphaUser = userSearch.users[0];
310 expect(alphaUser).toBeDefined();
312 // ban alpha from beta site
313 let banAlpha = await banUserFromSite(beta, alphaUser.id, true);
314 expect(banAlpha.banned).toBe(true);
316 // Alpha makes post on beta
317 let postRes = await createPost(alpha, betaCommunity.id);
318 expect(postRes.post).toBeDefined();
319 expect(postRes.post.community_local).toBe(false);
320 expect(postRes.post.creator_local).toBe(true);
321 expect(postRes.post.score).toBe(1);
323 // Make sure that post doesn't make it to beta
324 let searchBeta = await searchPostLocal(beta, postRes.post);
325 let betaPost = searchBeta.posts[0];
326 expect(betaPost).toBeUndefined();
329 let unBanAlpha = await banUserFromSite(beta, alphaUser.id, false);
330 expect(unBanAlpha.banned).toBe(false);
333 test('Enforce community ban for federated user', async () => {
334 let alphaShortname = `@lemmy_alpha@lemmy-alpha:8541`;
335 let userSearch = await searchForUser(beta, alphaShortname);
336 let alphaUser = userSearch.users[0];
337 expect(alphaUser).toBeDefined();
339 // ban alpha from beta site
340 await banUserFromCommunity(beta, alphaUser.id, 2, false);
341 let banAlpha = await banUserFromCommunity(beta, alphaUser.id, 2, true);
342 expect(banAlpha.banned).toBe(true);
344 // Alpha makes post on beta
345 let postRes = await createPost(alpha, betaCommunity.id);
346 expect(postRes.post).toBeDefined();
347 expect(postRes.post.community_local).toBe(false);
348 expect(postRes.post.creator_local).toBe(true);
349 expect(postRes.post.score).toBe(1);
351 // Make sure that post doesn't make it to beta community
352 let searchBeta = await searchPostLocal(beta, postRes.post);
353 let betaPost = searchBeta.posts[0];
354 expect(betaPost).toBeUndefined();
357 let unBanAlpha = await banUserFromCommunity(beta, alphaUser.id, 2, false);
358 expect(unBanAlpha.banned).toBe(false);