]> Untitled Git - lemmy.git/blob - api_tests/src/comment.spec.ts
Merge branch 'drone-io-dess' into move_views_to_diesel_drone
[lemmy.git] / api_tests / src / comment.spec.ts
1 jest.setTimeout(180000);
2 import {
3   alpha,
4   beta,
5   gamma,
6   setupLogins,
7   createPost,
8   getPost,
9   searchComment,
10   likeComment,
11   followBeta,
12   searchForBetaCommunity,
13   createComment,
14   editComment,
15   deleteComment,
16   removeComment,
17   getMentions,
18   searchPost,
19   unfollowRemotes,
20   createCommunity,
21   registerUser,
22   API,
23 } from './shared';
24 import { CommentView } from 'lemmy-js-client';
25
26 import { PostResponse } from 'lemmy-js-client';
27
28 let postRes: PostResponse;
29
30 beforeAll(async () => {
31   await setupLogins();
32   await followBeta(alpha);
33   await followBeta(gamma);
34   let search = await searchForBetaCommunity(alpha);
35   postRes = await createPost(
36     alpha,
37     search.communities.find(c => c.community.local == false).community.id
38   );
39 });
40
41 afterAll(async () => {
42   await unfollowRemotes(alpha);
43   await unfollowRemotes(gamma);
44 });
45
46 function assertCommentFederation(
47   commentOne: CommentView,
48   commentTwo: CommentView
49 ) {
50   expect(commentOne.comment.ap_id).toBe(commentOne.comment.ap_id);
51   expect(commentOne.comment.content).toBe(commentTwo.comment.content);
52   expect(commentOne.creator.name).toBe(commentTwo.creator.name);
53   expect(commentOne.community.actor_id).toBe(commentTwo.community.actor_id);
54   expect(commentOne.comment.published).toBe(commentTwo.comment.published);
55   expect(commentOne.comment.updated).toBe(commentOne.comment.updated);
56   expect(commentOne.comment.deleted).toBe(commentOne.comment.deleted);
57   expect(commentOne.comment.removed).toBe(commentOne.comment.removed);
58 }
59
60 test('Create a comment', async () => {
61   let commentRes = await createComment(alpha, postRes.post_view.post.id);
62   expect(commentRes.comment_view.comment.content).toBeDefined();
63   expect(commentRes.comment_view.community.local).toBe(false);
64   expect(commentRes.comment_view.creator.local).toBe(true);
65   expect(commentRes.comment_view.counts.score).toBe(1);
66
67   // Make sure that comment is liked on beta
68   let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
69   let betaComment = searchBeta.comments[0];
70   expect(betaComment).toBeDefined();
71   expect(betaComment.community.local).toBe(true);
72   expect(betaComment.creator.local).toBe(false);
73   expect(betaComment.counts.score).toBe(1);
74   assertCommentFederation(betaComment, commentRes.comment_view);
75 });
76
77 test('Create a comment in a non-existent post', async () => {
78   let commentRes = await createComment(alpha, -1);
79   expect(commentRes).toStrictEqual({ error: 'couldnt_find_post' });
80 });
81
82 test('Update a comment', async () => {
83   let commentRes = await createComment(alpha, postRes.post_view.post.id);
84   // Federate the comment first
85   let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
86   assertCommentFederation(searchBeta.comments[0], commentRes.comment_view);
87
88   let updateCommentRes = await editComment(
89     alpha,
90     commentRes.comment_view.comment.id
91   );
92   expect(updateCommentRes.comment_view.comment.content).toBe(
93     'A jest test federated comment update'
94   );
95   expect(updateCommentRes.comment_view.community.local).toBe(false);
96   expect(updateCommentRes.comment_view.creator.local).toBe(true);
97
98   // Make sure that post is updated on beta
99   let searchBetaUpdated = await searchComment(
100     beta,
101     commentRes.comment_view.comment
102   );
103   assertCommentFederation(
104     searchBetaUpdated.comments[0],
105     updateCommentRes.comment_view
106   );
107 });
108
109 test('Delete a comment', async () => {
110   let commentRes = await createComment(alpha, postRes.post_view.post.id);
111
112   let deleteCommentRes = await deleteComment(
113     alpha,
114     true,
115     commentRes.comment_view.comment.id
116   );
117   expect(deleteCommentRes.comment_view.comment.deleted).toBe(true);
118
119   // Make sure that comment is undefined on beta
120   let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
121   let betaComment = searchBeta.comments[0];
122   expect(betaComment).toBeUndefined();
123
124   let undeleteCommentRes = await deleteComment(
125     alpha,
126     false,
127     commentRes.comment_view.comment.id
128   );
129   expect(undeleteCommentRes.comment_view.comment.deleted).toBe(false);
130
131   // Make sure that comment is undeleted on beta
132   let searchBeta2 = await searchComment(beta, commentRes.comment_view.comment);
133   let betaComment2 = searchBeta2.comments[0];
134   expect(betaComment2.comment.deleted).toBe(false);
135   assertCommentFederation(
136     searchBeta2.comments[0],
137     undeleteCommentRes.comment_view
138   );
139 });
140
141 test('Remove a comment from admin and community on the same instance', async () => {
142   let commentRes = await createComment(alpha, postRes.post_view.post.id);
143
144   // Get the id for beta
145   let betaCommentId = (
146     await searchComment(beta, commentRes.comment_view.comment)
147   ).comments[0].comment.id;
148
149   // The beta admin removes it (the community lives on beta)
150   let removeCommentRes = await removeComment(beta, true, betaCommentId);
151   expect(removeCommentRes.comment_view.comment.removed).toBe(true);
152
153   // Make sure that comment is removed on alpha (it gets pushed since an admin from beta removed it)
154   let refetchedPost = await getPost(alpha, postRes.post_view.post.id);
155   expect(refetchedPost.comments[0].comment.removed).toBe(true);
156
157   let unremoveCommentRes = await removeComment(beta, false, betaCommentId);
158   expect(unremoveCommentRes.comment_view.comment.removed).toBe(false);
159
160   // Make sure that comment is unremoved on beta
161   let refetchedPost2 = await getPost(alpha, postRes.post_view.post.id);
162   expect(refetchedPost2.comments[0].comment.removed).toBe(false);
163   assertCommentFederation(
164     refetchedPost2.comments[0],
165     unremoveCommentRes.comment_view
166   );
167 });
168
169 test('Remove a comment from admin and community on different instance', async () => {
170   let alphaUser = await registerUser(alpha);
171   let newAlphaApi: API = {
172     client: alpha.client,
173     auth: alphaUser.jwt,
174   };
175
176   // New alpha user creates a community, post, and comment.
177   let newCommunity = await createCommunity(newAlphaApi);
178   let newPost = await createPost(
179     newAlphaApi,
180     newCommunity.community_view.community.id
181   );
182   let commentRes = await createComment(newAlphaApi, newPost.post_view.post.id);
183   expect(commentRes.comment_view.comment.content).toBeDefined();
184
185   // Beta searches that to cache it, then removes it
186   let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
187   let betaComment = searchBeta.comments[0];
188   let removeCommentRes = await removeComment(
189     beta,
190     true,
191     betaComment.comment.id
192   );
193   expect(removeCommentRes.comment_view.comment.removed).toBe(true);
194
195   // Make sure its not removed on alpha
196   let refetchedPost = await getPost(newAlphaApi, newPost.post_view.post.id);
197   expect(refetchedPost.comments[0].comment.removed).toBe(false);
198   assertCommentFederation(refetchedPost.comments[0], commentRes.comment_view);
199 });
200
201 test('Unlike a comment', async () => {
202   let commentRes = await createComment(alpha, postRes.post_view.post.id);
203   let unlike = await likeComment(alpha, 0, commentRes.comment_view.comment);
204   expect(unlike.comment_view.counts.score).toBe(0);
205
206   // Make sure that post is unliked on beta
207   let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
208   let betaComment = searchBeta.comments[0];
209   expect(betaComment).toBeDefined();
210   expect(betaComment.community.local).toBe(true);
211   expect(betaComment.creator.local).toBe(false);
212   expect(betaComment.counts.score).toBe(0);
213 });
214
215 test('Federated comment like', async () => {
216   let commentRes = await createComment(alpha, postRes.post_view.post.id);
217
218   // Find the comment on beta
219   let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
220   let betaComment = searchBeta.comments[0];
221
222   let like = await likeComment(beta, 1, betaComment.comment);
223   expect(like.comment_view.counts.score).toBe(2);
224
225   // Get the post from alpha, check the likes
226   let post = await getPost(alpha, postRes.post_view.post.id);
227   expect(post.comments[0].counts.score).toBe(2);
228 });
229
230 test('Reply to a comment', async () => {
231   // Create a comment on alpha, find it on beta
232   let commentRes = await createComment(alpha, postRes.post_view.post.id);
233   let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
234   let betaComment = searchBeta.comments[0];
235
236   // find that comment id on beta
237
238   // Reply from beta
239   let replyRes = await createComment(
240     beta,
241     betaComment.post.id,
242     betaComment.comment.id
243   );
244   expect(replyRes.comment_view.comment.content).toBeDefined();
245   expect(replyRes.comment_view.community.local).toBe(true);
246   expect(replyRes.comment_view.creator.local).toBe(true);
247   expect(replyRes.comment_view.comment.parent_id).toBe(betaComment.comment.id);
248   expect(replyRes.comment_view.counts.score).toBe(1);
249
250   // Make sure that comment is seen on alpha
251   // TODO not sure why, but a searchComment back to alpha, for the ap_id of betas
252   // comment, isn't working.
253   // let searchAlpha = await searchComment(alpha, replyRes.comment);
254   let post = await getPost(alpha, postRes.post_view.post.id);
255   let alphaComment = post.comments[0];
256   expect(alphaComment.comment.content).toBeDefined();
257   expect(alphaComment.comment.parent_id).toBe(post.comments[1].comment.id);
258   expect(alphaComment.community.local).toBe(false);
259   expect(alphaComment.creator.local).toBe(false);
260   expect(alphaComment.counts.score).toBe(1);
261   assertCommentFederation(alphaComment, replyRes.comment_view);
262 });
263
264 test('Mention beta', async () => {
265   // Create a mention on alpha
266   let mentionContent = 'A test mention of @lemmy_beta@lemmy-beta:8551';
267   let commentRes = await createComment(alpha, postRes.post_view.post.id);
268   let mentionRes = await createComment(
269     alpha,
270     postRes.post_view.post.id,
271     commentRes.comment_view.comment.id,
272     mentionContent
273   );
274   expect(mentionRes.comment_view.comment.content).toBeDefined();
275   expect(mentionRes.comment_view.community.local).toBe(false);
276   expect(mentionRes.comment_view.creator.local).toBe(true);
277   expect(mentionRes.comment_view.counts.score).toBe(1);
278
279   let mentionsRes = await getMentions(beta);
280   expect(mentionsRes.mentions[0].comment.content).toBeDefined();
281   expect(mentionsRes.mentions[0].community.local).toBe(true);
282   expect(mentionsRes.mentions[0].creator.local).toBe(false);
283   expect(mentionsRes.mentions[0].counts.score).toBe(1);
284 });
285
286 test('Comment Search', async () => {
287   let commentRes = await createComment(alpha, postRes.post_view.post.id);
288   let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
289   assertCommentFederation(searchBeta.comments[0], commentRes.comment_view);
290 });
291
292 test('A and G subscribe to B (center) A posts, G mentions B, it gets announced to A', async () => {
293   // Create a local post
294   let alphaPost = await createPost(alpha, 2);
295   expect(alphaPost.post_view.community.local).toBe(true);
296
297   // Make sure gamma sees it
298   let search = await searchPost(gamma, alphaPost.post_view.post);
299   let gammaPost = search.posts[0];
300
301   let commentContent =
302     'A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8551';
303   let commentRes = await createComment(
304     gamma,
305     gammaPost.post.id,
306     undefined,
307     commentContent
308   );
309   expect(commentRes.comment_view.comment.content).toBe(commentContent);
310   expect(commentRes.comment_view.community.local).toBe(false);
311   expect(commentRes.comment_view.creator.local).toBe(true);
312   expect(commentRes.comment_view.counts.score).toBe(1);
313
314   // Make sure alpha sees it
315   let alphaPost2 = await getPost(alpha, alphaPost.post_view.post.id);
316   expect(alphaPost2.comments[0].comment.content).toBe(commentContent);
317   expect(alphaPost2.comments[0].community.local).toBe(true);
318   expect(alphaPost2.comments[0].creator.local).toBe(false);
319   expect(alphaPost2.comments[0].counts.score).toBe(1);
320   assertCommentFederation(alphaPost2.comments[0], commentRes.comment_view);
321
322   // Make sure beta has mentions
323   let mentionsRes = await getMentions(beta);
324   expect(mentionsRes.mentions[0].comment.content).toBe(commentContent);
325   expect(mentionsRes.mentions[0].community.local).toBe(false);
326   expect(mentionsRes.mentions[0].creator.local).toBe(false);
327   // TODO this is failing because fetchInReplyTos aren't getting score
328   // expect(mentionsRes.mentions[0].score).toBe(1);
329 });
330
331 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 () => {
332   // Unfollow all remote communities
333   let followed = await unfollowRemotes(alpha);
334   expect(
335     followed.communities.filter(c => c.community.local == false).length
336   ).toBe(0);
337
338   // B creates a post, and two comments, should be invisible to A
339   let postRes = await createPost(beta, 2);
340   expect(postRes.post_view.post.name).toBeDefined();
341
342   let parentCommentContent = 'An invisible top level comment from beta';
343   let parentCommentRes = await createComment(
344     beta,
345     postRes.post_view.post.id,
346     undefined,
347     parentCommentContent
348   );
349   expect(parentCommentRes.comment_view.comment.content).toBe(
350     parentCommentContent
351   );
352
353   // B creates a comment, then a child one of that.
354   let childCommentContent = 'An invisible child comment from beta';
355   let childCommentRes = await createComment(
356     beta,
357     postRes.post_view.post.id,
358     parentCommentRes.comment_view.comment.id,
359     childCommentContent
360   );
361   expect(childCommentRes.comment_view.comment.content).toBe(
362     childCommentContent
363   );
364
365   // Follow beta again
366   let follow = await followBeta(alpha);
367   expect(follow.community_view.community.local).toBe(false);
368   expect(follow.community_view.community.name).toBe('main');
369
370   // An update to the child comment on beta, should push the post, parent, and child to alpha now
371   let updatedCommentContent = 'An update child comment from beta';
372   let updateRes = await editComment(
373     beta,
374     childCommentRes.comment_view.comment.id,
375     updatedCommentContent
376   );
377   expect(updateRes.comment_view.comment.content).toBe(updatedCommentContent);
378
379   // Get the post from alpha
380   let search = await searchPost(alpha, postRes.post_view.post);
381   let alphaPostB = search.posts[0];
382
383   let alphaPost = await getPost(alpha, alphaPostB.post.id);
384   expect(alphaPost.post_view.post.name).toBeDefined();
385   assertCommentFederation(alphaPost.comments[1], parentCommentRes.comment_view);
386   assertCommentFederation(alphaPost.comments[0], updateRes.comment_view);
387   expect(alphaPost.post_view.community.local).toBe(false);
388   expect(alphaPost.post_view.creator.local).toBe(false);
389
390   await unfollowRemotes(alpha);
391 });