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