]> Untitled Git - lemmy.git/blob - api_tests/src/post.spec.ts
Update prettier to 3.0.0 (#3509)
[lemmy.git] / api_tests / src / post.spec.ts
1 jest.setTimeout(120000);
2
3 import { CommunityView } from "lemmy-js-client/dist/types/CommunityView";
4 import {
5   alpha,
6   beta,
7   gamma,
8   delta,
9   epsilon,
10   setupLogins,
11   createPost,
12   editPost,
13   featurePost,
14   lockPost,
15   resolvePost,
16   likePost,
17   followBeta,
18   resolveBetaCommunity,
19   createComment,
20   deletePost,
21   removePost,
22   getPost,
23   unfollowRemotes,
24   resolvePerson,
25   banPersonFromSite,
26   searchPostLocal,
27   followCommunity,
28   banPersonFromCommunity,
29   reportPost,
30   listPostReports,
31   randomString,
32   registerUser,
33   API,
34   getSite,
35   unfollows,
36   resolveCommunity,
37 } from "./shared";
38 import { PostView } from "lemmy-js-client/dist/types/PostView";
39
40 let betaCommunity: CommunityView | undefined;
41
42 beforeAll(async () => {
43   await setupLogins();
44   betaCommunity = (await resolveBetaCommunity(alpha)).community;
45   expect(betaCommunity).toBeDefined();
46   await unfollows();
47 });
48
49 afterAll(async () => {
50   await unfollows();
51 });
52
53 function assertPostFederation(postOne?: PostView, postTwo?: PostView) {
54   expect(postOne?.post.ap_id).toBe(postTwo?.post.ap_id);
55   expect(postOne?.post.name).toBe(postTwo?.post.name);
56   expect(postOne?.post.body).toBe(postTwo?.post.body);
57   // TODO url clears arent working
58   // expect(postOne?.post.url).toBe(postTwo?.post.url);
59   expect(postOne?.post.nsfw).toBe(postTwo?.post.nsfw);
60   expect(postOne?.post.embed_title).toBe(postTwo?.post.embed_title);
61   expect(postOne?.post.embed_description).toBe(postTwo?.post.embed_description);
62   expect(postOne?.post.embed_video_url).toBe(postTwo?.post.embed_video_url);
63   expect(postOne?.post.published).toBe(postTwo?.post.published);
64   expect(postOne?.community.actor_id).toBe(postTwo?.community.actor_id);
65   expect(postOne?.post.locked).toBe(postTwo?.post.locked);
66   expect(postOne?.post.removed).toBe(postTwo?.post.removed);
67   expect(postOne?.post.deleted).toBe(postTwo?.post.deleted);
68 }
69
70 test("Create a post", async () => {
71   if (!betaCommunity) {
72     throw "Missing beta community";
73   }
74
75   let postRes = await createPost(alpha, betaCommunity.community.id);
76   expect(postRes.post_view.post).toBeDefined();
77   expect(postRes.post_view.community.local).toBe(false);
78   expect(postRes.post_view.creator.local).toBe(true);
79   expect(postRes.post_view.counts.score).toBe(1);
80
81   // Make sure that post is liked on beta
82   let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
83
84   expect(betaPost).toBeDefined();
85   expect(betaPost?.community.local).toBe(true);
86   expect(betaPost?.creator.local).toBe(false);
87   expect(betaPost?.counts.score).toBe(1);
88   assertPostFederation(betaPost, postRes.post_view);
89
90   // Delta only follows beta, so it should not see an alpha ap_id
91   let deltaPost = (await resolvePost(delta, postRes.post_view.post)).post;
92   expect(deltaPost).toBeUndefined();
93
94   // Epsilon has alpha blocked, it should not see the alpha post
95   let epsilonPost = (await resolvePost(epsilon, postRes.post_view.post)).post;
96   expect(epsilonPost).toBeUndefined();
97 });
98
99 test("Create a post in a non-existent community", async () => {
100   let postRes = (await createPost(alpha, -2)) as any;
101   expect(postRes.error).toBe("couldnt_find_community");
102 });
103
104 test("Unlike a post", async () => {
105   if (!betaCommunity) {
106     throw "Missing beta community";
107   }
108   let postRes = await createPost(alpha, betaCommunity.community.id);
109   let unlike = await likePost(alpha, 0, postRes.post_view.post);
110   expect(unlike.post_view.counts.score).toBe(0);
111
112   // Try to unlike it again, make sure it stays at 0
113   let unlike2 = await likePost(alpha, 0, postRes.post_view.post);
114   expect(unlike2.post_view.counts.score).toBe(0);
115
116   // Make sure that post is unliked on beta
117   let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
118   expect(betaPost).toBeDefined();
119   expect(betaPost?.community.local).toBe(true);
120   expect(betaPost?.creator.local).toBe(false);
121   expect(betaPost?.counts.score).toBe(0);
122   assertPostFederation(betaPost, postRes.post_view);
123 });
124
125 test("Update a post", async () => {
126   if (!betaCommunity) {
127     throw "Missing beta community";
128   }
129   let postRes = await createPost(alpha, betaCommunity.community.id);
130
131   let updatedName = "A jest test federated post, updated";
132   let updatedPost = await editPost(alpha, postRes.post_view.post);
133   expect(updatedPost.post_view.post.name).toBe(updatedName);
134   expect(updatedPost.post_view.community.local).toBe(false);
135   expect(updatedPost.post_view.creator.local).toBe(true);
136
137   // Make sure that post is updated on beta
138   let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
139   if (!betaPost) {
140     throw "Missing beta post";
141   }
142   expect(betaPost.community.local).toBe(true);
143   expect(betaPost.creator.local).toBe(false);
144   expect(betaPost.post.name).toBe(updatedName);
145   assertPostFederation(betaPost, updatedPost.post_view);
146
147   // Make sure lemmy beta cannot update the post
148   let updatedPostBeta = (await editPost(beta, betaPost.post)) as any;
149   expect(updatedPostBeta.error).toBe("no_post_edit_allowed");
150 });
151
152 test("Sticky a post", async () => {
153   if (!betaCommunity) {
154     throw "Missing beta community";
155   }
156   let postRes = await createPost(alpha, betaCommunity.community.id);
157
158   let betaPost1 = (await resolvePost(beta, postRes.post_view.post)).post;
159   if (!betaPost1) {
160     throw "Missing beta post1";
161   }
162   let stickiedPostRes = await featurePost(beta, true, betaPost1.post);
163   expect(stickiedPostRes.post_view.post.featured_community).toBe(true);
164
165   // Make sure that post is stickied on beta
166   let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
167   expect(betaPost?.community.local).toBe(true);
168   expect(betaPost?.creator.local).toBe(false);
169   expect(betaPost?.post.featured_community).toBe(true);
170
171   // Unsticky a post
172   let unstickiedPost = await featurePost(beta, false, betaPost1.post);
173   expect(unstickiedPost.post_view.post.featured_community).toBe(false);
174
175   // Make sure that post is unstickied on beta
176   let betaPost2 = (await resolvePost(beta, postRes.post_view.post)).post;
177   expect(betaPost2?.community.local).toBe(true);
178   expect(betaPost2?.creator.local).toBe(false);
179   expect(betaPost2?.post.featured_community).toBe(false);
180
181   // Make sure that gamma cannot sticky the post on beta
182   let gammaPost = (await resolvePost(gamma, postRes.post_view.post)).post;
183   if (!gammaPost) {
184     throw "Missing gamma post";
185   }
186   let gammaTrySticky = await featurePost(gamma, true, gammaPost.post);
187   let betaPost3 = (await resolvePost(beta, postRes.post_view.post)).post;
188   expect(gammaTrySticky.post_view.post.featured_community).toBe(true);
189   expect(betaPost3?.post.featured_community).toBe(false);
190 });
191
192 test("Lock a post", async () => {
193   if (!betaCommunity) {
194     throw "Missing beta community";
195   }
196   await followCommunity(alpha, true, betaCommunity.community.id);
197   let postRes = await createPost(alpha, betaCommunity.community.id);
198
199   // Lock the post
200   let betaPost1 = (await resolvePost(beta, postRes.post_view.post)).post;
201   if (!betaPost1) {
202     throw "Missing beta post1";
203   }
204   let lockedPostRes = await lockPost(beta, true, betaPost1.post);
205   expect(lockedPostRes.post_view.post.locked).toBe(true);
206
207   // Make sure that post is locked on alpha
208   let searchAlpha = await searchPostLocal(alpha, postRes.post_view.post);
209   let alphaPost1 = searchAlpha.posts[0];
210   expect(alphaPost1.post.locked).toBe(true);
211
212   // Try to make a new comment there, on alpha
213   let comment: any = await createComment(alpha, alphaPost1.post.id);
214   expect(comment["error"]).toBe("locked");
215
216   // Unlock a post
217   let unlockedPost = await lockPost(beta, false, betaPost1.post);
218   expect(unlockedPost.post_view.post.locked).toBe(false);
219
220   // Make sure that post is unlocked on alpha
221   let searchAlpha2 = await searchPostLocal(alpha, postRes.post_view.post);
222   let alphaPost2 = searchAlpha2.posts[0];
223   expect(alphaPost2.community.local).toBe(false);
224   expect(alphaPost2.creator.local).toBe(true);
225   expect(alphaPost2.post.locked).toBe(false);
226
227   // Try to create a new comment, on alpha
228   let commentAlpha = await createComment(alpha, alphaPost1.post.id);
229   expect(commentAlpha).toBeDefined();
230 });
231
232 test("Delete a post", async () => {
233   if (!betaCommunity) {
234     throw "Missing beta community";
235   }
236
237   let postRes = await createPost(alpha, betaCommunity.community.id);
238   expect(postRes.post_view.post).toBeDefined();
239
240   let deletedPost = await deletePost(alpha, true, postRes.post_view.post);
241   expect(deletedPost.post_view.post.deleted).toBe(true);
242   expect(deletedPost.post_view.post.name).toBe(postRes.post_view.post.name);
243
244   // Make sure lemmy beta sees post is deleted
245   let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
246   // This will be undefined because of the tombstone
247   expect(betaPost).toBeUndefined();
248
249   // Undelete
250   let undeletedPost = await deletePost(alpha, false, postRes.post_view.post);
251   expect(undeletedPost.post_view.post.deleted).toBe(false);
252
253   // Make sure lemmy beta sees post is undeleted
254   let betaPost2 = (await resolvePost(beta, postRes.post_view.post)).post;
255   if (!betaPost2) {
256     throw "Missing beta post 2";
257   }
258   expect(betaPost2.post.deleted).toBe(false);
259   assertPostFederation(betaPost2, undeletedPost.post_view);
260
261   // Make sure lemmy beta cannot delete the post
262   let deletedPostBeta = (await deletePost(beta, true, betaPost2.post)) as any;
263   expect(deletedPostBeta.error).toStrictEqual("no_post_edit_allowed");
264 });
265
266 test("Remove a post from admin and community on different instance", async () => {
267   if (!betaCommunity) {
268     throw "Missing beta community";
269   }
270
271   let gammaCommunity = (
272     await resolveCommunity(gamma, betaCommunity.community.actor_id)
273   ).community?.community;
274   if (!gammaCommunity) {
275     throw "Missing gamma community";
276   }
277   let postRes = await createPost(gamma, gammaCommunity.id);
278
279   let alphaPost = (await resolvePost(alpha, postRes.post_view.post)).post;
280   if (!alphaPost) {
281     throw "Missing alpha post";
282   }
283   let removedPost = await removePost(alpha, true, alphaPost.post);
284   expect(removedPost.post_view.post.removed).toBe(true);
285   expect(removedPost.post_view.post.name).toBe(postRes.post_view.post.name);
286
287   // Make sure lemmy beta sees post is NOT removed
288   let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
289   if (!betaPost) {
290     throw "Missing beta post";
291   }
292   expect(betaPost.post.removed).toBe(false);
293
294   // Undelete
295   let undeletedPost = await removePost(alpha, false, alphaPost.post);
296   expect(undeletedPost.post_view.post.removed).toBe(false);
297
298   // Make sure lemmy beta sees post is undeleted
299   let betaPost2 = (await resolvePost(beta, postRes.post_view.post)).post;
300   expect(betaPost2?.post.removed).toBe(false);
301   assertPostFederation(betaPost2, undeletedPost.post_view);
302 });
303
304 test("Remove a post from admin and community on same instance", async () => {
305   if (!betaCommunity) {
306     throw "Missing beta community";
307   }
308   await followBeta(alpha);
309   let postRes = await createPost(alpha, betaCommunity.community.id);
310   expect(postRes.post_view.post).toBeDefined();
311
312   // Get the id for beta
313   let searchBeta = await searchPostLocal(beta, postRes.post_view.post);
314   let betaPost = searchBeta.posts[0];
315   expect(betaPost).toBeDefined();
316
317   // The beta admin removes it (the community lives on beta)
318   let removePostRes = await removePost(beta, true, betaPost.post);
319   expect(removePostRes.post_view.post.removed).toBe(true);
320
321   // Make sure lemmy alpha sees post is removed
322   // let alphaPost = await getPost(alpha, postRes.post_view.post.id);
323   // expect(alphaPost.post_view.post.removed).toBe(true); // TODO this shouldn't be commented
324   // assertPostFederation(alphaPost.post_view, removePostRes.post_view);
325
326   // Undelete
327   let undeletedPost = await removePost(beta, false, betaPost.post);
328   expect(undeletedPost.post_view.post.removed).toBe(false);
329
330   // Make sure lemmy alpha sees post is undeleted
331   let alphaPost2 = await getPost(alpha, postRes.post_view.post.id);
332   expect(alphaPost2.post_view.post.removed).toBe(false);
333   assertPostFederation(alphaPost2.post_view, undeletedPost.post_view);
334   await unfollowRemotes(alpha);
335 });
336
337 test("Search for a post", async () => {
338   if (!betaCommunity) {
339     throw "Missing beta community";
340   }
341   await unfollowRemotes(alpha);
342   let postRes = await createPost(alpha, betaCommunity.community.id);
343   expect(postRes.post_view.post).toBeDefined();
344
345   let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
346   expect(betaPost?.post.name).toBeDefined();
347 });
348
349 test("Enforce site ban for federated user", async () => {
350   if (!betaCommunity) {
351     throw "Missing beta community";
352   }
353   // create a test user
354   let alphaUserJwt = await registerUser(alpha);
355   expect(alphaUserJwt).toBeDefined();
356   let alpha_user: API = {
357     client: alpha.client,
358     auth: alphaUserJwt.jwt ?? "",
359   };
360   let alphaUserActorId = (await getSite(alpha_user)).my_user?.local_user_view
361     .person.actor_id;
362   if (!alphaUserActorId) {
363     throw "Missing alpha user actor id";
364   }
365   expect(alphaUserActorId).toBeDefined();
366   let alphaPerson = (await resolvePerson(alpha_user, alphaUserActorId)).person;
367   if (!alphaPerson) {
368     throw "Missing alpha person";
369   }
370   expect(alphaPerson).toBeDefined();
371
372   // alpha makes post in beta community, it federates to beta instance
373   let postRes1 = await createPost(alpha_user, betaCommunity.community.id);
374   let searchBeta1 = await searchPostLocal(beta, postRes1.post_view.post);
375   expect(searchBeta1.posts[0]).toBeDefined();
376
377   // ban alpha from its instance
378   let banAlpha = await banPersonFromSite(
379     alpha,
380     alphaPerson.person.id,
381     true,
382     true,
383   );
384   expect(banAlpha.banned).toBe(true);
385
386   // alpha ban should be federated to beta
387   let alphaUserOnBeta1 = await resolvePerson(beta, alphaUserActorId);
388   expect(alphaUserOnBeta1.person?.person.banned).toBe(true);
389
390   // existing alpha post should be removed on beta
391   let searchBeta2 = await searchPostLocal(beta, postRes1.post_view.post);
392   expect(searchBeta2.posts[0].post.removed).toBe(true);
393
394   // Unban alpha
395   let unBanAlpha = await banPersonFromSite(
396     alpha,
397     alphaPerson.person.id,
398     false,
399     false,
400   );
401   expect(unBanAlpha.banned).toBe(false);
402
403   // alpha makes new post in beta community, it federates
404   let postRes2 = await createPost(alpha_user, betaCommunity.community.id);
405   let searchBeta3 = await searchPostLocal(beta, postRes2.post_view.post);
406   expect(searchBeta3.posts[0]).toBeDefined();
407
408   let alphaUserOnBeta2 = await resolvePerson(beta, alphaUserActorId);
409   expect(alphaUserOnBeta2.person?.person.banned).toBe(false);
410 });
411
412 test("Enforce community ban for federated user", async () => {
413   if (!betaCommunity) {
414     throw "Missing beta community";
415   }
416   let alphaShortname = `@lemmy_alpha@lemmy-alpha:8541`;
417   let alphaPerson = (await resolvePerson(beta, alphaShortname)).person;
418   if (!alphaPerson) {
419     throw "Missing alpha person";
420   }
421   expect(alphaPerson).toBeDefined();
422
423   // make a post in beta, it goes through
424   let postRes1 = await createPost(alpha, betaCommunity.community.id);
425   let searchBeta1 = await searchPostLocal(beta, postRes1.post_view.post);
426   expect(searchBeta1.posts[0]).toBeDefined();
427
428   // ban alpha from beta community
429   let banAlpha = await banPersonFromCommunity(
430     beta,
431     alphaPerson.person.id,
432     2,
433     true,
434     true,
435   );
436   expect(banAlpha.banned).toBe(true);
437
438   // ensure that the post by alpha got removed
439   let searchAlpha1 = await searchPostLocal(alpha, postRes1.post_view.post);
440   expect(searchAlpha1.posts[0].post.removed).toBe(true);
441
442   // Alpha tries to make post on beta, but it fails because of ban
443   let postRes2 = await createPost(alpha, betaCommunity.community.id);
444   expect(postRes2.post_view).toBeUndefined();
445
446   // Unban alpha
447   let unBanAlpha = await banPersonFromCommunity(
448     beta,
449     alphaPerson.person.id,
450     2,
451     false,
452     false,
453   );
454   expect(unBanAlpha.banned).toBe(false);
455   let postRes3 = await createPost(alpha, betaCommunity.community.id);
456   expect(postRes3.post_view.post).toBeDefined();
457   expect(postRes3.post_view.community.local).toBe(false);
458   expect(postRes3.post_view.creator.local).toBe(true);
459   expect(postRes3.post_view.counts.score).toBe(1);
460
461   // Make sure that post makes it to beta community
462   let searchBeta2 = await searchPostLocal(beta, postRes3.post_view.post);
463   expect(searchBeta2.posts[0]).toBeDefined();
464 });
465
466 test("A and G subscribe to B (center) A posts, it gets announced to G", async () => {
467   if (!betaCommunity) {
468     throw "Missing beta community";
469   }
470   let postRes = await createPost(alpha, betaCommunity.community.id);
471   expect(postRes.post_view.post).toBeDefined();
472
473   let betaPost = (await resolvePost(gamma, postRes.post_view.post)).post;
474   expect(betaPost?.post.name).toBeDefined();
475 });
476
477 test("Report a post", async () => {
478   // Note, this is a different one from the setup
479   let betaCommunity = (await resolveBetaCommunity(beta)).community;
480   if (!betaCommunity) {
481     throw "Missing beta community";
482   }
483   let postRes = await createPost(beta, betaCommunity.community.id);
484   expect(postRes.post_view.post).toBeDefined();
485
486   let alphaPost = (await resolvePost(alpha, postRes.post_view.post)).post;
487   if (!alphaPost) {
488     throw "Missing alpha post";
489   }
490   let alphaReport = (
491     await reportPost(alpha, alphaPost.post.id, randomString(10))
492   ).post_report_view.post_report;
493
494   let betaReport = (await listPostReports(beta)).post_reports[0].post_report;
495   expect(betaReport).toBeDefined();
496   expect(betaReport.resolved).toBe(false);
497   expect(betaReport.original_post_name).toBe(alphaReport.original_post_name);
498   expect(betaReport.original_post_url).toBe(alphaReport.original_post_url);
499   expect(betaReport.original_post_body).toBe(alphaReport.original_post_body);
500   expect(betaReport.reason).toBe(alphaReport.reason);
501 });