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