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