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