]> Untitled Git - lemmy.git/blob - api_tests/src/comment.spec.ts
47e6dba2d86bbfd6e8bc2325c9636c6b525dba07
[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 } from './shared';
34
35 let postRes: PostResponse;
36
37 beforeAll(async () => {
38   await setupLogins();
39   await unfollows();
40   await followBeta(alpha);
41   await followBeta(gamma);
42   let betaCommunity = (await resolveBetaCommunity(alpha)).community;
43   postRes = await createPost(
44     alpha,
45     betaCommunity.unwrap().community.id
46   );
47 });
48
49 afterAll(async () => {
50   await unfollows();
51 });
52
53 function assertCommentFederation(
54   commentOne: CommentView,
55   commentTwo: CommentView
56 ) {
57   expect(commentOne.comment.ap_id).toBe(commentOne.comment.ap_id);
58   expect(commentOne.comment.content).toBe(commentTwo.comment.content);
59   expect(commentOne.creator.name).toBe(commentTwo.creator.name);
60   expect(commentOne.community.actor_id).toBe(commentTwo.community.actor_id);
61   expect(commentOne.comment.published).toBe(commentTwo.comment.published);
62   expect(commentOne.comment.updated).toBe(commentOne.comment.updated);
63   expect(commentOne.comment.deleted).toBe(commentOne.comment.deleted);
64   expect(commentOne.comment.removed).toBe(commentOne.comment.removed);
65 }
66
67 test('Create a comment', async () => {
68   let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
69   expect(commentRes.comment_view.comment.content).toBeDefined();
70   expect(commentRes.comment_view.community.local).toBe(false);
71   expect(commentRes.comment_view.creator.local).toBe(true);
72   expect(commentRes.comment_view.counts.score).toBe(1);
73
74   // Make sure that comment is liked on beta
75   let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).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 = (await resolveComment(beta, commentRes.comment_view.comment)).comment;
92   assertCommentFederation(betaComment.unwrap(), commentRes.comment_view);
93
94   let updateCommentRes = await editComment(
95     alpha,
96     commentRes.comment_view.comment.id
97   );
98   expect(updateCommentRes.comment_view.comment.content).toBe(
99     'A jest test federated comment update'
100   );
101   expect(updateCommentRes.comment_view.community.local).toBe(false);
102   expect(updateCommentRes.comment_view.creator.local).toBe(true);
103
104   // Make sure that post is updated on beta
105   let betaCommentUpdated = (await resolveComment(
106     beta,
107     commentRes.comment_view.comment
108   )).comment.unwrap();
109   assertCommentFederation(
110     betaCommentUpdated,
111     updateCommentRes.comment_view
112   );
113 });
114
115 test('Delete a comment', async () => {
116   let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
117
118   let deleteCommentRes = await deleteComment(
119     alpha,
120     true,
121     commentRes.comment_view.comment.id
122   );
123   expect(deleteCommentRes.comment_view.comment.deleted).toBe(true);
124   expect(deleteCommentRes.comment_view.comment.content).toBe("");
125
126   // Make sure that comment is undefined on beta
127   let betaCommentRes = await resolveComment(beta, commentRes.comment_view.comment) as any;
128   expect(betaCommentRes.error).toBe('couldnt_find_object');
129
130   let undeleteCommentRes = await deleteComment(
131     alpha,
132     false,
133     commentRes.comment_view.comment.id
134   );
135   expect(undeleteCommentRes.comment_view.comment.deleted).toBe(false);
136
137   // Make sure that comment is undeleted on beta
138   let betaComment2 = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
139   expect(betaComment2.comment.deleted).toBe(false);
140   assertCommentFederation(
141     betaComment2,
142     undeleteCommentRes.comment_view
143   );
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(alpha, postRes.post_view.post.id);
161   expect(refetchedPostComments.comments[0].comment.removed).toBe(true);
162
163   let unremoveCommentRes = await removeComment(beta, false, betaCommentId);
164   expect(unremoveCommentRes.comment_view.comment.removed).toBe(false);
165
166   // Make sure that comment is unremoved on beta
167   let refetchedPostComments2 = await getComments(alpha, postRes.post_view.post.id);
168   expect(refetchedPostComments2.comments[0].comment.removed).toBe(false);
169   assertCommentFederation(
170     refetchedPostComments2.comments[0],
171     unremoveCommentRes.comment_view
172   );
173 });
174
175 test('Remove a comment from admin and community on different instance', async () => {
176   let alpha_user = await registerUser(alpha);
177   let newAlphaApi: API = {
178     client: alpha.client,
179     auth: alpha_user.jwt,
180   };
181
182   // New alpha user creates a community, post, and comment.
183   let newCommunity = await createCommunity(newAlphaApi);
184   let newPost = await createPost(
185     newAlphaApi,
186     newCommunity.community_view.community.id
187   );
188   let commentRes = await createComment(newAlphaApi, newPost.post_view.post.id, None);
189   expect(commentRes.comment_view.comment.content).toBeDefined();
190
191   // Beta searches that to cache it, then removes it
192   let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
193   let removeCommentRes = await removeComment(
194     beta,
195     true,
196     betaComment.comment.id
197   );
198   expect(removeCommentRes.comment_view.comment.removed).toBe(true);
199
200   // Make sure its not removed on alpha
201   let refetchedPostComments = await getComments(alpha, newPost.post_view.post.id);
202   expect(refetchedPostComments.comments[0].comment.removed).toBe(false);
203   assertCommentFederation(refetchedPostComments.comments[0], commentRes.comment_view);
204 });
205
206 test('Unlike a comment', async () => {
207   let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
208   let unlike = await likeComment(alpha, 0, commentRes.comment_view.comment);
209   expect(unlike.comment_view.counts.score).toBe(0);
210
211   // Make sure that post is unliked on beta
212   let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
213   expect(betaComment).toBeDefined();
214   expect(betaComment.community.local).toBe(true);
215   expect(betaComment.creator.local).toBe(false);
216   expect(betaComment.counts.score).toBe(0);
217 });
218
219 test('Federated comment like', async () => {
220   let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
221
222   // Find the comment on beta
223   let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
224
225   let like = await likeComment(beta, 1, betaComment.comment);
226   expect(like.comment_view.counts.score).toBe(2);
227
228   // Get the post from alpha, check the likes
229   let postComments = await getComments(alpha, postRes.post_view.post.id);
230   expect(postComments.comments[0].counts.score).toBe(2);
231 });
232
233 test('Reply to a comment', async () => {
234   // Create a comment on alpha, find it on beta
235   let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
236   let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
237
238   // find that comment id on beta
239
240   // Reply from beta
241   let replyRes = await createComment(
242     beta,
243     betaComment.post.id,
244     Some(betaComment.comment.id)
245   );
246   expect(replyRes.comment_view.comment.content).toBeDefined();
247   expect(replyRes.comment_view.community.local).toBe(true);
248   expect(replyRes.comment_view.creator.local).toBe(true);
249   expect(getCommentParentId(replyRes.comment_view.comment).unwrap()).toBe(betaComment.comment.id);
250   expect(replyRes.comment_view.counts.score).toBe(1);
251
252   // Make sure that comment is seen on alpha
253   // TODO not sure why, but a searchComment back to alpha, for the ap_id of betas
254   // comment, isn't working.
255   // let searchAlpha = await searchComment(alpha, replyRes.comment);
256   let postComments = await getComments(alpha, postRes.post_view.post.id);
257   let alphaComment = postComments.comments[0];
258   expect(alphaComment.comment.content).toBeDefined();
259   expect(getCommentParentId(alphaComment.comment).unwrap()).toBe(postComments.comments[1].comment.id);
260   expect(alphaComment.community.local).toBe(false);
261   expect(alphaComment.creator.local).toBe(false);
262   expect(alphaComment.counts.score).toBe(1);
263   assertCommentFederation(alphaComment, replyRes.comment_view);
264 });
265
266 test('Mention beta', async () => {
267   // Create a mention on alpha
268   let mentionContent = 'A test mention of @lemmy_beta@lemmy-beta:8551';
269   let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
270   let mentionRes = await createComment(
271     alpha,
272     postRes.post_view.post.id,
273     Some(commentRes.comment_view.comment.id),
274     mentionContent
275   );
276   expect(mentionRes.comment_view.comment.content).toBeDefined();
277   expect(mentionRes.comment_view.community.local).toBe(false);
278   expect(mentionRes.comment_view.creator.local).toBe(true);
279   expect(mentionRes.comment_view.counts.score).toBe(1);
280
281   let mentionsRes = await getMentions(beta);
282   expect(mentionsRes.mentions[0].comment.content).toBeDefined();
283   expect(mentionsRes.mentions[0].community.local).toBe(true);
284   expect(mentionsRes.mentions[0].creator.local).toBe(false);
285   expect(mentionsRes.mentions[0].counts.score).toBe(1);
286 });
287
288 test('Comment Search', async () => {
289   let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
290   let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
291   assertCommentFederation(betaComment, commentRes.comment_view);
292 });
293
294 test('A and G subscribe to B (center) A posts, G mentions B, it gets announced to A', async () => {
295   // Create a local post
296   let alphaCommunity = await createCommunity(alpha, "main");
297   let alphaPost = await createPost(alpha, alphaCommunity.community_view.community.id);
298   expect(alphaPost.post_view.community.local).toBe(true);
299
300   // Make sure gamma sees it
301   let gammaPost = (await resolvePost(gamma, alphaPost.post_view.post)).post.unwrap();
302
303   let commentContent =
304     'A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8551';
305   let commentRes = await createComment(
306     gamma,
307     gammaPost.post.id,
308     None,
309     commentContent
310   );
311   expect(commentRes.comment_view.comment.content).toBe(commentContent);
312   expect(commentRes.comment_view.community.local).toBe(false);
313   expect(commentRes.comment_view.creator.local).toBe(true);
314   expect(commentRes.comment_view.counts.score).toBe(1);
315
316   // Make sure alpha sees it
317   let alphaPostComments2 = await getComments(alpha, alphaPost.post_view.post.id);
318   expect(alphaPostComments2.comments[0].comment.content).toBe(commentContent);
319   expect(alphaPostComments2.comments[0].community.local).toBe(true);
320   expect(alphaPostComments2.comments[0].creator.local).toBe(false);
321   expect(alphaPostComments2.comments[0].counts.score).toBe(1);
322   assertCommentFederation(alphaPostComments2.comments[0], commentRes.comment_view);
323
324   // Make sure beta has mentions
325   let mentionsRes = await getMentions(beta);
326   expect(mentionsRes.mentions[0].comment.content).toBe(commentContent);
327   expect(mentionsRes.mentions[0].community.local).toBe(false);
328   expect(mentionsRes.mentions[0].creator.local).toBe(false);
329   // TODO this is failing because fetchInReplyTos aren't getting score
330   // expect(mentionsRes.mentions[0].score).toBe(1);
331 });
332
333 test('Check that activity from another instance is sent to third instance', async () => {
334   // Alpha and gamma users follow beta community
335   let alphaFollow = await followBeta(alpha);
336   expect(alphaFollow.community_view.community.local).toBe(false);
337   expect(alphaFollow.community_view.community.name).toBe('main');
338
339   let gammaFollow = await followBeta(gamma);
340   expect(gammaFollow.community_view.community.local).toBe(false);
341   expect(gammaFollow.community_view.community.name).toBe('main');
342
343   // Create a post on beta
344   let betaPost = await createPost(beta, 2);
345   expect(betaPost.post_view.community.local).toBe(true);
346
347   // Make sure gamma and alpha see it
348   let gammaPost = (await resolvePost(gamma, betaPost.post_view.post)).post.unwrap();
349   expect(gammaPost.post).toBeDefined();
350   let alphaPost = (await resolvePost(alpha, betaPost.post_view.post)).post.unwrap();
351   expect(alphaPost.post).toBeDefined();
352
353   // The bug: gamma comments, and alpha should see it.
354   let commentContent = 'Comment from gamma';
355   let commentRes = await createComment(
356     gamma,
357     gammaPost.post.id,
358     None,
359     commentContent
360   );
361   expect(commentRes.comment_view.comment.content).toBe(commentContent);
362   expect(commentRes.comment_view.community.local).toBe(false);
363   expect(commentRes.comment_view.creator.local).toBe(true);
364   expect(commentRes.comment_view.counts.score).toBe(1);
365
366   // Make sure alpha sees it
367   let alphaPostComments2 = await getComments(alpha, alphaPost.post.id);
368   expect(alphaPostComments2.comments[0].comment.content).toBe(commentContent);
369   expect(alphaPostComments2.comments[0].community.local).toBe(false);
370   expect(alphaPostComments2.comments[0].creator.local).toBe(false);
371   expect(alphaPostComments2.comments[0].counts.score).toBe(1);
372   assertCommentFederation(alphaPostComments2.comments[0], commentRes.comment_view);
373
374   await unfollowRemotes(alpha);
375   await unfollowRemotes(gamma);
376 });
377
378 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 () => {
379   // Unfollow all remote communities
380   let site = await unfollowRemotes(alpha);
381   expect(
382     site.my_user.unwrap().follows.filter(c => c.community.local == false).length
383   ).toBe(0);
384
385   // B creates a post, and two comments, should be invisible to A
386   let postRes = await createPost(beta, 2);
387   expect(postRes.post_view.post.name).toBeDefined();
388
389   let parentCommentContent = 'An invisible top level comment from beta';
390   let parentCommentRes = await createComment(
391     beta,
392     postRes.post_view.post.id,
393     None,
394     parentCommentContent
395   );
396   expect(parentCommentRes.comment_view.comment.content).toBe(
397     parentCommentContent
398   );
399
400   // B creates a comment, then a child one of that.
401   let childCommentContent = 'An invisible child comment from beta';
402   let childCommentRes = await createComment(
403     beta,
404     postRes.post_view.post.id,
405     Some(parentCommentRes.comment_view.comment.id),
406     childCommentContent
407   );
408   expect(childCommentRes.comment_view.comment.content).toBe(
409     childCommentContent
410   );
411
412   // Follow beta again
413   let follow = await followBeta(alpha);
414   expect(follow.community_view.community.local).toBe(false);
415   expect(follow.community_view.community.name).toBe('main');
416
417   // An update to the child comment on beta, should push the post, parent, and child to alpha now
418   let updatedCommentContent = 'An update child comment from beta';
419   let updateRes = await editComment(
420     beta,
421     childCommentRes.comment_view.comment.id,
422     updatedCommentContent
423   );
424   expect(updateRes.comment_view.comment.content).toBe(updatedCommentContent);
425
426   // Get the post from alpha
427   let alphaPostB = (await resolvePost(alpha, postRes.post_view.post)).post.unwrap();
428
429   let alphaPost = await getPost(alpha, alphaPostB.post.id);
430   let alphaPostComments = await getComments(alpha, alphaPostB.post.id);
431   expect(alphaPost.post_view.post.name).toBeDefined();
432   assertCommentFederation(alphaPostComments.comments[1], parentCommentRes.comment_view);
433   assertCommentFederation(alphaPostComments.comments[0], updateRes.comment_view);
434   expect(alphaPost.post_view.community.local).toBe(false);
435   expect(alphaPost.post_view.creator.local).toBe(false);
436
437   await unfollowRemotes(alpha);
438 });
439
440
441 test('Report a comment', async () => {
442   let betaCommunity = (await resolveBetaCommunity(beta)).community.unwrap();
443   let postRes = (await createPost(beta, betaCommunity.community.id)).post_view.post;
444   expect(postRes).toBeDefined();
445   let commentRes = (await createComment(beta, postRes.id, None)).comment_view.comment;
446   expect(commentRes).toBeDefined();
447
448   let alphaComment = (await resolveComment(alpha, commentRes)).comment.unwrap().comment;
449   let alphaReport = (await reportComment(alpha, alphaComment.id, randomString(10)))
450         .comment_report_view.comment_report;
451
452   let betaReport = (await listCommentReports(beta)).comment_reports[0].comment_report;
453   expect(betaReport).toBeDefined();
454   expect(betaReport.resolved).toBe(false);
455   expect(betaReport.original_comment_text).toBe(alphaReport.original_comment_text);
456   expect(betaReport.reason).toBe(alphaReport.reason);
457 });
458 function N(gamma: API, id: number, N: any, commentContent: string) {
459   throw new Error('Function not implemented.');
460 }
461