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