]> Untitled Git - lemmy.git/blob - api_tests/src/post.spec.ts
Adding unfollows.
[lemmy.git] / api_tests / src / post.spec.ts
1 jest.setTimeout(120000);
2 import {
3   alpha,
4   beta,
5   gamma,
6   delta,
7   epsilon,
8   setupLogins,
9   createPost,
10   updatePost,
11   stickyPost,
12   lockPost,
13   searchPost,
14   likePost,
15   followBeta,
16   searchForBetaCommunity,
17   createComment,
18   deletePost,
19   removePost,
20   getPost,
21   unfollowRemotes,
22   searchForUser,
23   banUserFromSite,
24   searchPostLocal,
25   banUserFromCommunity,
26 } from './shared';
27 import {
28   Post,
29   Community,
30 } from 'lemmy-js-client';
31
32 let betaCommunity: Community;
33
34 beforeAll(async () => {
35   await setupLogins();
36   let search = await searchForBetaCommunity(alpha);
37   betaCommunity = search.communities[0];
38   await unfollows();
39 });
40
41 afterAll(async () => {
42   await unfollows();
43 });
44
45 async function unfollows() {
46   await unfollowRemotes(alpha);
47   await unfollowRemotes(gamma);
48   await unfollowRemotes(delta);
49   await unfollowRemotes(epsilon);
50 }
51
52 function assertPostFederation(
53   postOne: Post,
54   postTwo: Post) {
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);
68 }
69
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);
76
77   // Make sure that post is liked on beta
78   let searchBeta = await searchPost(beta, postRes.post);
79   let betaPost = searchBeta.posts[0];
80
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);
86
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();
90
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();
94 });
95
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' });
99 });
100
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);
105
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);
109
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);
118 });
119
120 test('Update a post', async () => {
121   let postRes = await createPost(alpha, betaCommunity.id);
122
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);
128
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);
136
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' });
140 });
141
142 test('Sticky a post', async () => {
143   let postRes = await createPost(alpha, betaCommunity.id);
144
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);
153
154   // Unsticky a post
155   let unstickiedPost = await stickyPost(alpha, false, postRes.post);
156   expect(unstickiedPost.post.stickied).toBe(false);
157
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);
164
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);
173 });
174
175 test('Lock a post', async () => {
176   let postRes = await createPost(alpha, betaCommunity.id);
177
178   // Lock the post
179   let lockedPostRes = await lockPost(alpha, true, postRes.post);
180   expect(lockedPostRes.post.locked).toBe(true);
181
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);
186
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');
190
191   // Unlock a post
192   let unlockedPost = await lockPost(alpha, false, postRes.post);
193   expect(unlockedPost.post.locked).toBe(false);
194
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);
201
202   // Try to create a new comment, on beta
203   let commentBeta = await createComment(beta, betaPost2.id);
204   expect(commentBeta).toBeDefined();
205 });
206
207 test('Delete a post', async () => {
208   let postRes = await createPost(alpha, betaCommunity.id);
209   expect(postRes.post).toBeDefined();
210
211   let deletedPost = await deletePost(alpha, true, postRes.post);
212   expect(deletedPost.post.deleted).toBe(true);
213
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();
219
220   // Undelete
221   let undeletedPost = await deletePost(alpha, false, postRes.post);
222   expect(undeletedPost.post.deleted).toBe(false);
223
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);
229
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' });
233 });
234
235 test('Remove a post from admin and community on different instance', async () => {
236   let postRes = await createPost(alpha, betaCommunity.id);
237
238   let removedPost = await removePost(alpha, true, postRes.post);
239   expect(removedPost.post.removed).toBe(true);
240
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);
245
246   // Undelete
247   let undeletedPost = await removePost(alpha, false, postRes.post);
248   expect(undeletedPost.post.removed).toBe(false);
249
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);
255 });
256
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();
261
262   // Get the id for beta
263   let searchBeta = await searchPostLocal(beta, postRes.post);
264   let betaPost = searchBeta.posts[0];
265   expect(betaPost).toBeDefined();
266
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);
270
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);
274   assertPostFederation(alphaPost.post, removePostRes.post);
275
276   // Undelete
277   let undeletedPost = await removePost(beta, false, betaPost);
278   expect(undeletedPost.post.removed).toBe(false);
279
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);
285 });
286
287 test('Search for a post', async () => {
288   await unfollowRemotes(alpha);
289   let postRes = await createPost(alpha, betaCommunity.id);
290   expect(postRes.post).toBeDefined();
291
292   let searchBeta = await searchPost(beta, postRes.post);
293
294   expect(searchBeta.posts[0].name).toBeDefined();
295 });
296
297 test('A and G subscribe to B (center) A posts, it gets announced to G', async () => {
298   await followBeta(alpha);
299   await followBeta(gamma);
300   let postRes = await createPost(alpha, betaCommunity.id);
301   expect(postRes.post).toBeDefined();
302
303   let search2 = await searchPostLocal(gamma, postRes.post);
304   expect(search2.posts[0].name).toBeDefined();
305   await unfollowRemotes(alpha);
306   await unfollowRemotes(gamma);
307 });
308
309 test('Enforce site ban for federated user', async () => {
310
311   let alphaShortname = `@lemmy_alpha@lemmy-alpha:8541`;
312   let userSearch = await searchForUser(beta, alphaShortname);
313   let alphaUser = userSearch.users[0];
314   expect(alphaUser).toBeDefined();
315
316   // ban alpha from beta site
317   let banAlpha = await banUserFromSite(beta, alphaUser.id, true);
318   expect(banAlpha.banned).toBe(true);
319
320   // Alpha makes post on beta
321   let postRes = await createPost(alpha, betaCommunity.id);
322   expect(postRes.post).toBeDefined();
323   expect(postRes.post.community_local).toBe(false);
324   expect(postRes.post.creator_local).toBe(true);
325   expect(postRes.post.score).toBe(1);
326
327   // Make sure that post doesn't make it to beta
328   let searchBeta = await searchPostLocal(beta, postRes.post);
329   let betaPost = searchBeta.posts[0];
330   expect(betaPost).toBeUndefined();
331
332   // Unban alpha
333   let unBanAlpha = await banUserFromSite(beta, alphaUser.id, false);
334   expect(unBanAlpha.banned).toBe(false);
335 });
336
337 test('Enforce community ban for federated user', async () => {
338   let alphaShortname = `@lemmy_alpha@lemmy-alpha:8541`;
339   let userSearch = await searchForUser(beta, alphaShortname);
340   let alphaUser = userSearch.users[0];
341   expect(alphaUser).toBeDefined();
342
343   // ban alpha from beta site
344   await banUserFromCommunity(beta, alphaUser.id, 2, false);
345   let banAlpha = await banUserFromCommunity(beta, alphaUser.id, 2, true);
346   expect(banAlpha.banned).toBe(true);
347
348   // Alpha makes post on beta
349   let postRes = await createPost(alpha, betaCommunity.id);
350   expect(postRes.post).toBeDefined();
351   expect(postRes.post.community_local).toBe(false);
352   expect(postRes.post.creator_local).toBe(true);
353   expect(postRes.post.score).toBe(1);
354
355   // Make sure that post doesn't make it to beta community
356   let searchBeta = await searchPostLocal(beta, postRes.post);
357   let betaPost = searchBeta.posts[0];
358   expect(betaPost).toBeUndefined();
359
360   // Unban alpha
361   let unBanAlpha = await banUserFromCommunity(beta, alphaUser.id, 2, false);
362   expect(unBanAlpha.banned).toBe(false);
363 });