]> Untitled Git - lemmy.git/blob - api_tests/src/comment.spec.ts
Moving settings to Database. (#2492)
[lemmy.git] / api_tests / src / comment.spec.ts
1 jest.setTimeout(180000);
2 import { None, Some } from "@sniptt/monads";
3 import { CommentView } from "lemmy-js-client";
4 import { PostResponse } from "lemmy-js-client";
5
6 import {
7   alpha,
8   beta,
9   gamma,
10   setupLogins,
11   createPost,
12   getPost,
13   resolveComment,
14   likeComment,
15   followBeta,
16   resolveBetaCommunity,
17   createComment,
18   editComment,
19   deleteComment,
20   removeComment,
21   getMentions,
22   resolvePost,
23   unfollowRemotes,
24   createCommunity,
25   registerUser,
26   reportComment,
27   listCommentReports,
28   randomString,
29   API,
30   unfollows,
31   getComments,
32   getCommentParentId,
33   resolveCommunity,
34 } from "./shared";
35
36 let postRes: PostResponse;
37
38 beforeAll(async () => {
39   await setupLogins();
40   await unfollows();
41   await followBeta(alpha);
42   await followBeta(gamma);
43   let betaCommunity = (await resolveBetaCommunity(alpha)).community;
44   postRes = await createPost(alpha, betaCommunity.unwrap().community.id);
45 });
46
47 afterAll(async () => {
48   await unfollows();
49 });
50
51 function assertCommentFederation(
52   commentOne: CommentView,
53   commentTwo: CommentView
54 ) {
55   expect(commentOne.comment.ap_id).toBe(commentOne.comment.ap_id);
56   expect(commentOne.comment.content).toBe(commentTwo.comment.content);
57   expect(commentOne.creator.name).toBe(commentTwo.creator.name);
58   expect(commentOne.community.actor_id).toBe(commentTwo.community.actor_id);
59   expect(commentOne.comment.published).toBe(commentTwo.comment.published);
60   expect(commentOne.comment.updated).toBe(commentOne.comment.updated);
61   expect(commentOne.comment.deleted).toBe(commentOne.comment.deleted);
62   expect(commentOne.comment.removed).toBe(commentOne.comment.removed);
63 }
64
65 test("Create a comment", async () => {
66   let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
67   expect(commentRes.comment_view.comment.content).toBeDefined();
68   expect(commentRes.comment_view.community.local).toBe(false);
69   expect(commentRes.comment_view.creator.local).toBe(true);
70   expect(commentRes.comment_view.counts.score).toBe(1);
71
72   // Make sure that comment is liked on beta
73   let betaComment = (
74     await resolveComment(beta, commentRes.comment_view.comment)
75   ).comment.unwrap();
76   expect(betaComment).toBeDefined();
77   expect(betaComment.community.local).toBe(true);
78   expect(betaComment.creator.local).toBe(false);
79   expect(betaComment.counts.score).toBe(1);
80   assertCommentFederation(betaComment, commentRes.comment_view);
81 });
82
83 test("Create a comment in a non-existent post", async () => {
84   let commentRes = (await createComment(alpha, -1, None)) as any;
85   expect(commentRes.error).toBe("couldnt_find_post");
86 });
87
88 test("Update a comment", async () => {
89   let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
90   // Federate the comment first
91   let betaComment = (
92     await resolveComment(beta, commentRes.comment_view.comment)
93   ).comment;
94   assertCommentFederation(betaComment.unwrap(), commentRes.comment_view);
95
96   let updateCommentRes = await editComment(
97     alpha,
98     commentRes.comment_view.comment.id
99   );
100   expect(updateCommentRes.comment_view.comment.content).toBe(
101     "A jest test federated comment update"
102   );
103   expect(updateCommentRes.comment_view.community.local).toBe(false);
104   expect(updateCommentRes.comment_view.creator.local).toBe(true);
105
106   // Make sure that post is updated on beta
107   let betaCommentUpdated = (
108     await resolveComment(beta, commentRes.comment_view.comment)
109   ).comment.unwrap();
110   assertCommentFederation(betaCommentUpdated, updateCommentRes.comment_view);
111 });
112
113 test("Delete a comment", async () => {
114   let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
115
116   let deleteCommentRes = await deleteComment(
117     alpha,
118     true,
119     commentRes.comment_view.comment.id
120   );
121   expect(deleteCommentRes.comment_view.comment.deleted).toBe(true);
122   expect(deleteCommentRes.comment_view.comment.content).toBe("");
123
124   // Make sure that comment is undefined on beta
125   let betaCommentRes = (await resolveComment(
126     beta,
127     commentRes.comment_view.comment
128   )) as any;
129   expect(betaCommentRes.error).toBe("couldnt_find_object");
130
131   let undeleteCommentRes = await deleteComment(
132     alpha,
133     false,
134     commentRes.comment_view.comment.id
135   );
136   expect(undeleteCommentRes.comment_view.comment.deleted).toBe(false);
137
138   // Make sure that comment is undeleted on beta
139   let betaComment2 = (
140     await resolveComment(beta, commentRes.comment_view.comment)
141   ).comment.unwrap();
142   expect(betaComment2.comment.deleted).toBe(false);
143   assertCommentFederation(betaComment2, undeleteCommentRes.comment_view);
144 });
145
146 test("Remove a comment from admin and community on the same instance", async () => {
147   let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
148
149   // Get the id for beta
150   let betaCommentId = (
151     await resolveComment(beta, commentRes.comment_view.comment)
152   ).comment.unwrap().comment.id;
153
154   // The beta admin removes it (the community lives on beta)
155   let removeCommentRes = await removeComment(beta, true, betaCommentId);
156   expect(removeCommentRes.comment_view.comment.removed).toBe(true);
157   expect(removeCommentRes.comment_view.comment.content).toBe("");
158
159   // Make sure that comment is removed on alpha (it gets pushed since an admin from beta removed it)
160   let refetchedPostComments = await getComments(
161     alpha,
162     postRes.post_view.post.id
163   );
164   expect(refetchedPostComments.comments[0].comment.removed).toBe(true);
165
166   let unremoveCommentRes = await removeComment(beta, false, betaCommentId);
167   expect(unremoveCommentRes.comment_view.comment.removed).toBe(false);
168
169   // Make sure that comment is unremoved on beta
170   let refetchedPostComments2 = await getComments(
171     alpha,
172     postRes.post_view.post.id
173   );
174   expect(refetchedPostComments2.comments[0].comment.removed).toBe(false);
175   assertCommentFederation(
176     refetchedPostComments2.comments[0],
177     unremoveCommentRes.comment_view
178   );
179 });
180
181 test("Remove a comment from admin and community on different instance", async () => {
182   let alpha_user = await registerUser(alpha);
183   let newAlphaApi: API = {
184     client: alpha.client,
185     auth: alpha_user.jwt,
186   };
187
188   // New alpha user creates a community, post, and comment.
189   let newCommunity = await createCommunity(newAlphaApi);
190   let newPost = await createPost(
191     newAlphaApi,
192     newCommunity.community_view.community.id
193   );
194   let commentRes = await createComment(
195     newAlphaApi,
196     newPost.post_view.post.id,
197     None
198   );
199   expect(commentRes.comment_view.comment.content).toBeDefined();
200
201   // Beta searches that to cache it, then removes it
202   let betaComment = (
203     await resolveComment(beta, commentRes.comment_view.comment)
204   ).comment.unwrap();
205   let removeCommentRes = await removeComment(
206     beta,
207     true,
208     betaComment.comment.id
209   );
210   expect(removeCommentRes.comment_view.comment.removed).toBe(true);
211
212   // Make sure its not removed on alpha
213   let refetchedPostComments = await getComments(
214     alpha,
215     newPost.post_view.post.id
216   );
217   expect(refetchedPostComments.comments[0].comment.removed).toBe(false);
218   assertCommentFederation(
219     refetchedPostComments.comments[0],
220     commentRes.comment_view
221   );
222 });
223
224 test("Unlike a comment", async () => {
225   let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
226   let unlike = await likeComment(alpha, 0, commentRes.comment_view.comment);
227   expect(unlike.comment_view.counts.score).toBe(0);
228
229   // Make sure that post is unliked on beta
230   let betaComment = (
231     await resolveComment(beta, commentRes.comment_view.comment)
232   ).comment.unwrap();
233   expect(betaComment).toBeDefined();
234   expect(betaComment.community.local).toBe(true);
235   expect(betaComment.creator.local).toBe(false);
236   expect(betaComment.counts.score).toBe(0);
237 });
238
239 test("Federated comment like", async () => {
240   let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
241
242   // Find the comment on beta
243   let betaComment = (
244     await resolveComment(beta, commentRes.comment_view.comment)
245   ).comment.unwrap();
246
247   let like = await likeComment(beta, 1, betaComment.comment);
248   expect(like.comment_view.counts.score).toBe(2);
249
250   // Get the post from alpha, check the likes
251   let postComments = await getComments(alpha, postRes.post_view.post.id);
252   expect(postComments.comments[0].counts.score).toBe(2);
253 });
254
255 test("Reply to a comment", async () => {
256   // Create a comment on alpha, find it on beta
257   let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
258   let betaComment = (
259     await resolveComment(beta, commentRes.comment_view.comment)
260   ).comment.unwrap();
261
262   // find that comment id on beta
263
264   // Reply from beta
265   let replyRes = await createComment(
266     beta,
267     betaComment.post.id,
268     Some(betaComment.comment.id)
269   );
270   expect(replyRes.comment_view.comment.content).toBeDefined();
271   expect(replyRes.comment_view.community.local).toBe(true);
272   expect(replyRes.comment_view.creator.local).toBe(true);
273   expect(getCommentParentId(replyRes.comment_view.comment).unwrap()).toBe(
274     betaComment.comment.id
275   );
276   expect(replyRes.comment_view.counts.score).toBe(1);
277
278   // Make sure that comment is seen on alpha
279   // TODO not sure why, but a searchComment back to alpha, for the ap_id of betas
280   // comment, isn't working.
281   // let searchAlpha = await searchComment(alpha, replyRes.comment);
282   let postComments = await getComments(alpha, postRes.post_view.post.id);
283   let alphaComment = postComments.comments[0];
284   expect(alphaComment.comment.content).toBeDefined();
285   expect(getCommentParentId(alphaComment.comment).unwrap()).toBe(
286     postComments.comments[1].comment.id
287   );
288   expect(alphaComment.community.local).toBe(false);
289   expect(alphaComment.creator.local).toBe(false);
290   expect(alphaComment.counts.score).toBe(1);
291   assertCommentFederation(alphaComment, replyRes.comment_view);
292 });
293
294 test("Mention beta", async () => {
295   // Create a mention on alpha
296   let mentionContent = "A test mention of @lemmy_beta@lemmy-beta:8551";
297   let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
298   let mentionRes = await createComment(
299     alpha,
300     postRes.post_view.post.id,
301     Some(commentRes.comment_view.comment.id),
302     mentionContent
303   );
304   expect(mentionRes.comment_view.comment.content).toBeDefined();
305   expect(mentionRes.comment_view.community.local).toBe(false);
306   expect(mentionRes.comment_view.creator.local).toBe(true);
307   expect(mentionRes.comment_view.counts.score).toBe(1);
308
309   let mentionsRes = await getMentions(beta);
310   expect(mentionsRes.mentions[0].comment.content).toBeDefined();
311   expect(mentionsRes.mentions[0].community.local).toBe(true);
312   expect(mentionsRes.mentions[0].creator.local).toBe(false);
313   expect(mentionsRes.mentions[0].counts.score).toBe(1);
314 });
315
316 test("Comment Search", async () => {
317   let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
318   let betaComment = (
319     await resolveComment(beta, commentRes.comment_view.comment)
320   ).comment.unwrap();
321   assertCommentFederation(betaComment, commentRes.comment_view);
322 });
323
324 test("A and G subscribe to B (center) A posts, G mentions B, it gets announced to A", async () => {
325   // Create a local post
326   let alphaCommunity = (
327     await resolveCommunity(alpha, "!main@lemmy-alpha:8541")
328   ).community.unwrap();
329   let alphaPost = await createPost(alpha, alphaCommunity.community.id);
330   expect(alphaPost.post_view.community.local).toBe(true);
331
332   // Make sure gamma sees it
333   let gammaPost = (
334     await resolvePost(gamma, alphaPost.post_view.post)
335   ).post.unwrap();
336
337   let commentContent =
338     "A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8551";
339   let commentRes = await createComment(
340     gamma,
341     gammaPost.post.id,
342     None,
343     commentContent
344   );
345   expect(commentRes.comment_view.comment.content).toBe(commentContent);
346   expect(commentRes.comment_view.community.local).toBe(false);
347   expect(commentRes.comment_view.creator.local).toBe(true);
348   expect(commentRes.comment_view.counts.score).toBe(1);
349
350   // Make sure alpha sees it
351   let alphaPostComments2 = await getComments(
352     alpha,
353     alphaPost.post_view.post.id
354   );
355   expect(alphaPostComments2.comments[0].comment.content).toBe(commentContent);
356   expect(alphaPostComments2.comments[0].community.local).toBe(true);
357   expect(alphaPostComments2.comments[0].creator.local).toBe(false);
358   expect(alphaPostComments2.comments[0].counts.score).toBe(1);
359   assertCommentFederation(
360     alphaPostComments2.comments[0],
361     commentRes.comment_view
362   );
363
364   // Make sure beta has mentions
365   let mentionsRes = await getMentions(beta);
366   expect(mentionsRes.mentions[0].comment.content).toBe(commentContent);
367   expect(mentionsRes.mentions[0].community.local).toBe(false);
368   expect(mentionsRes.mentions[0].creator.local).toBe(false);
369   // TODO this is failing because fetchInReplyTos aren't getting score
370   // expect(mentionsRes.mentions[0].score).toBe(1);
371 });
372
373 test("Check that activity from another instance is sent to third instance", async () => {
374   // Alpha and gamma users follow beta community
375   let alphaFollow = await followBeta(alpha);
376   expect(alphaFollow.community_view.community.local).toBe(false);
377   expect(alphaFollow.community_view.community.name).toBe("main");
378
379   let gammaFollow = await followBeta(gamma);
380   expect(gammaFollow.community_view.community.local).toBe(false);
381   expect(gammaFollow.community_view.community.name).toBe("main");
382
383   // Create a post on beta
384   let betaPost = await createPost(beta, 2);
385   expect(betaPost.post_view.community.local).toBe(true);
386
387   // Make sure gamma and alpha see it
388   let gammaPost = (
389     await resolvePost(gamma, betaPost.post_view.post)
390   ).post.unwrap();
391   expect(gammaPost.post).toBeDefined();
392   let alphaPost = (
393     await resolvePost(alpha, betaPost.post_view.post)
394   ).post.unwrap();
395   expect(alphaPost.post).toBeDefined();
396
397   // The bug: gamma comments, and alpha should see it.
398   let commentContent = "Comment from gamma";
399   let commentRes = await createComment(
400     gamma,
401     gammaPost.post.id,
402     None,
403     commentContent
404   );
405   expect(commentRes.comment_view.comment.content).toBe(commentContent);
406   expect(commentRes.comment_view.community.local).toBe(false);
407   expect(commentRes.comment_view.creator.local).toBe(true);
408   expect(commentRes.comment_view.counts.score).toBe(1);
409
410   // Make sure alpha sees it
411   let alphaPostComments2 = await getComments(alpha, alphaPost.post.id);
412   expect(alphaPostComments2.comments[0].comment.content).toBe(commentContent);
413   expect(alphaPostComments2.comments[0].community.local).toBe(false);
414   expect(alphaPostComments2.comments[0].creator.local).toBe(false);
415   expect(alphaPostComments2.comments[0].counts.score).toBe(1);
416   assertCommentFederation(
417     alphaPostComments2.comments[0],
418     commentRes.comment_view
419   );
420
421   await unfollowRemotes(alpha);
422   await unfollowRemotes(gamma);
423 });
424
425 test("Fetch in_reply_tos: A is unsubbed from B, B makes a post, and some embedded comments, A subs to B, B updates the lowest level comment, A fetches both the post and all the inreplyto comments for that post.", async () => {
426   // Unfollow all remote communities
427   let site = await unfollowRemotes(alpha);
428   expect(
429     site.my_user.unwrap().follows.filter(c => c.community.local == false).length
430   ).toBe(0);
431
432   // B creates a post, and two comments, should be invisible to A
433   let postRes = await createPost(beta, 2);
434   expect(postRes.post_view.post.name).toBeDefined();
435
436   let parentCommentContent = "An invisible top level comment from beta";
437   let parentCommentRes = await createComment(
438     beta,
439     postRes.post_view.post.id,
440     None,
441     parentCommentContent
442   );
443   expect(parentCommentRes.comment_view.comment.content).toBe(
444     parentCommentContent
445   );
446
447   // B creates a comment, then a child one of that.
448   let childCommentContent = "An invisible child comment from beta";
449   let childCommentRes = await createComment(
450     beta,
451     postRes.post_view.post.id,
452     Some(parentCommentRes.comment_view.comment.id),
453     childCommentContent
454   );
455   expect(childCommentRes.comment_view.comment.content).toBe(
456     childCommentContent
457   );
458
459   // Follow beta again
460   let follow = await followBeta(alpha);
461   expect(follow.community_view.community.local).toBe(false);
462   expect(follow.community_view.community.name).toBe("main");
463
464   // An update to the child comment on beta, should push the post, parent, and child to alpha now
465   let updatedCommentContent = Some("An update child comment from beta");
466   let updateRes = await editComment(
467     beta,
468     childCommentRes.comment_view.comment.id,
469     updatedCommentContent
470   );
471   expect(updateRes.comment_view.comment.content).toBe(
472     updatedCommentContent.unwrap()
473   );
474
475   // Get the post from alpha
476   let alphaPostB = (
477     await resolvePost(alpha, postRes.post_view.post)
478   ).post.unwrap();
479
480   let alphaPost = await getPost(alpha, alphaPostB.post.id);
481   let alphaPostComments = await getComments(alpha, alphaPostB.post.id);
482   expect(alphaPost.post_view.post.name).toBeDefined();
483   assertCommentFederation(
484     alphaPostComments.comments[1],
485     parentCommentRes.comment_view
486   );
487   assertCommentFederation(
488     alphaPostComments.comments[0],
489     updateRes.comment_view
490   );
491   expect(alphaPost.post_view.community.local).toBe(false);
492   expect(alphaPost.post_view.creator.local).toBe(false);
493
494   await unfollowRemotes(alpha);
495 });
496
497 test("Report a comment", async () => {
498   let betaCommunity = (await resolveBetaCommunity(beta)).community.unwrap();
499   let postRes = (await createPost(beta, betaCommunity.community.id)).post_view
500     .post;
501   expect(postRes).toBeDefined();
502   let commentRes = (await createComment(beta, postRes.id, None)).comment_view
503     .comment;
504   expect(commentRes).toBeDefined();
505
506   let alphaComment = (await resolveComment(alpha, commentRes)).comment.unwrap()
507     .comment;
508   let alphaReport = (
509     await reportComment(alpha, alphaComment.id, randomString(10))
510   ).comment_report_view.comment_report;
511
512   let betaReport = (await listCommentReports(beta)).comment_reports[0]
513     .comment_report;
514   expect(betaReport).toBeDefined();
515   expect(betaReport.resolved).toBe(false);
516   expect(betaReport.original_comment_text).toBe(
517     alphaReport.original_comment_text
518   );
519   expect(betaReport.reason).toBe(alphaReport.reason);
520 });