]> Untitled Git - lemmy.git/blob - api_tests/src/community.spec.ts
Add moderator view parameter to list posts (#3176)
[lemmy.git] / api_tests / src / community.spec.ts
1 jest.setTimeout(120000);
2
3 import { CommunityView } from "lemmy-js-client/dist/types/CommunityView";
4 import {
5   alpha,
6   beta,
7   gamma,
8   setupLogins,
9   resolveCommunity,
10   createCommunity,
11   deleteCommunity,
12   removeCommunity,
13   getCommunity,
14   followCommunity,
15   banPersonFromCommunity,
16   resolvePerson,
17   getSite,
18   createPost,
19   getPost,
20   resolvePost,
21   registerUser,
22   API,
23   getPosts,
24 } from "./shared";
25
26 beforeAll(async () => {
27   await setupLogins();
28 });
29
30 function assertCommunityFederation(
31   communityOne?: CommunityView,
32   communityTwo?: CommunityView,
33 ) {
34   expect(communityOne?.community.actor_id).toBe(
35     communityTwo?.community.actor_id,
36   );
37   expect(communityOne?.community.name).toBe(communityTwo?.community.name);
38   expect(communityOne?.community.title).toBe(communityTwo?.community.title);
39   expect(communityOne?.community.description).toBe(
40     communityTwo?.community.description,
41   );
42   expect(communityOne?.community.icon).toBe(communityTwo?.community.icon);
43   expect(communityOne?.community.banner).toBe(communityTwo?.community.banner);
44   expect(communityOne?.community.published).toBe(
45     communityTwo?.community.published,
46   );
47   expect(communityOne?.community.nsfw).toBe(communityTwo?.community.nsfw);
48   expect(communityOne?.community.removed).toBe(communityTwo?.community.removed);
49   expect(communityOne?.community.deleted).toBe(communityTwo?.community.deleted);
50 }
51
52 test("Create community", async () => {
53   let communityRes = await createCommunity(alpha);
54   expect(communityRes.community_view.community.name).toBeDefined();
55
56   // A dupe check
57   let prevName = communityRes.community_view.community.name;
58   await expect(createCommunity(alpha, prevName)).rejects.toBe(
59     "community_already_exists",
60   );
61
62   // Cache the community on beta, make sure it has the other fields
63   let searchShort = `!${prevName}@lemmy-alpha:8541`;
64   let betaCommunity = (await resolveCommunity(beta, searchShort)).community;
65   assertCommunityFederation(betaCommunity, communityRes.community_view);
66 });
67
68 test("Delete community", async () => {
69   let communityRes = await createCommunity(beta);
70
71   // Cache the community on Alpha
72   let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`;
73   let alphaCommunity = (await resolveCommunity(alpha, searchShort)).community;
74   if (!alphaCommunity) {
75     throw "Missing alpha community";
76   }
77   assertCommunityFederation(alphaCommunity, communityRes.community_view);
78
79   // Follow the community from alpha
80   let follow = await followCommunity(alpha, true, alphaCommunity.community.id);
81
82   // Make sure the follow response went through
83   expect(follow.community_view.community.local).toBe(false);
84
85   let deleteCommunityRes = await deleteCommunity(
86     beta,
87     true,
88     communityRes.community_view.community.id,
89   );
90   expect(deleteCommunityRes.community_view.community.deleted).toBe(true);
91   expect(deleteCommunityRes.community_view.community.title).toBe(
92     communityRes.community_view.community.title,
93   );
94
95   // Make sure it got deleted on A
96   let communityOnAlphaDeleted = await getCommunity(
97     alpha,
98     alphaCommunity.community.id,
99   );
100   expect(communityOnAlphaDeleted.community_view.community.deleted).toBe(true);
101
102   // Undelete
103   let undeleteCommunityRes = await deleteCommunity(
104     beta,
105     false,
106     communityRes.community_view.community.id,
107   );
108   expect(undeleteCommunityRes.community_view.community.deleted).toBe(false);
109
110   // Make sure it got undeleted on A
111   let communityOnAlphaUnDeleted = await getCommunity(
112     alpha,
113     alphaCommunity.community.id,
114   );
115   expect(communityOnAlphaUnDeleted.community_view.community.deleted).toBe(
116     false,
117   );
118 });
119
120 test("Remove community", async () => {
121   let communityRes = await createCommunity(beta);
122
123   // Cache the community on Alpha
124   let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`;
125   let alphaCommunity = (await resolveCommunity(alpha, searchShort)).community;
126   if (!alphaCommunity) {
127     throw "Missing alpha community";
128   }
129   assertCommunityFederation(alphaCommunity, communityRes.community_view);
130
131   // Follow the community from alpha
132   let follow = await followCommunity(alpha, true, alphaCommunity.community.id);
133
134   // Make sure the follow response went through
135   expect(follow.community_view.community.local).toBe(false);
136
137   let removeCommunityRes = await removeCommunity(
138     beta,
139     true,
140     communityRes.community_view.community.id,
141   );
142   expect(removeCommunityRes.community_view.community.removed).toBe(true);
143   expect(removeCommunityRes.community_view.community.title).toBe(
144     communityRes.community_view.community.title,
145   );
146
147   // Make sure it got Removed on A
148   let communityOnAlphaRemoved = await getCommunity(
149     alpha,
150     alphaCommunity.community.id,
151   );
152   expect(communityOnAlphaRemoved.community_view.community.removed).toBe(true);
153
154   // unremove
155   let unremoveCommunityRes = await removeCommunity(
156     beta,
157     false,
158     communityRes.community_view.community.id,
159   );
160   expect(unremoveCommunityRes.community_view.community.removed).toBe(false);
161
162   // Make sure it got undeleted on A
163   let communityOnAlphaUnRemoved = await getCommunity(
164     alpha,
165     alphaCommunity.community.id,
166   );
167   expect(communityOnAlphaUnRemoved.community_view.community.removed).toBe(
168     false,
169   );
170 });
171
172 test("Search for beta community", async () => {
173   let communityRes = await createCommunity(beta);
174   expect(communityRes.community_view.community.name).toBeDefined();
175
176   let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`;
177   let alphaCommunity = (await resolveCommunity(alpha, searchShort)).community;
178   assertCommunityFederation(alphaCommunity, communityRes.community_view);
179 });
180
181 test("Admin actions in remote community are not federated to origin", async () => {
182   // create a community on alpha
183   let communityRes = (await createCommunity(alpha)).community_view;
184   expect(communityRes.community.name).toBeDefined();
185
186   // gamma follows community and posts in it
187   let gammaCommunity = (
188     await resolveCommunity(gamma, communityRes.community.actor_id)
189   ).community;
190   if (!gammaCommunity) {
191     throw "Missing gamma community";
192   }
193   await followCommunity(gamma, true, gammaCommunity.community.id);
194   gammaCommunity = (
195     await resolveCommunity(gamma, communityRes.community.actor_id)
196   ).community;
197   if (!gammaCommunity) {
198     throw "Missing gamma community";
199   }
200   expect(gammaCommunity.subscribed).toBe("Subscribed");
201   let gammaPost = (await createPost(gamma, gammaCommunity.community.id))
202     .post_view;
203   expect(gammaPost.post.id).toBeDefined();
204   expect(gammaPost.creator_banned_from_community).toBe(false);
205
206   // admin of beta decides to ban gamma from community
207   let betaCommunity = (
208     await resolveCommunity(beta, communityRes.community.actor_id)
209   ).community;
210   if (!betaCommunity) {
211     throw "Missing beta community";
212   }
213   let bannedUserInfo1 = (await getSite(gamma)).my_user?.local_user_view.person;
214   if (!bannedUserInfo1) {
215     throw "Missing banned user 1";
216   }
217   let bannedUserInfo2 = (await resolvePerson(beta, bannedUserInfo1.actor_id))
218     .person;
219   if (!bannedUserInfo2) {
220     throw "Missing banned user 2";
221   }
222   let banRes = await banPersonFromCommunity(
223     beta,
224     bannedUserInfo2.person.id,
225     betaCommunity.community.id,
226     true,
227     true,
228   );
229   expect(banRes.banned).toBe(true);
230
231   // ban doesnt federate to community's origin instance alpha
232   let alphaPost = (await resolvePost(alpha, gammaPost.post)).post;
233   expect(alphaPost?.creator_banned_from_community).toBe(false);
234
235   // and neither to gamma
236   let gammaPost2 = await getPost(gamma, gammaPost.post.id);
237   expect(gammaPost2.post_view.creator_banned_from_community).toBe(false);
238 });
239
240 test("moderator view", async () => {
241   // register a new user with their own community on alpha and post to it
242   let otherUser: API = {
243     auth: (await registerUser(alpha)).jwt ?? "",
244     client: alpha.client,
245   };
246   expect(otherUser.auth).not.toBe("");
247   let otherCommunity = (await createCommunity(otherUser)).community_view;
248   expect(otherCommunity.community.name).toBeDefined();
249   let otherPost = (await createPost(otherUser, otherCommunity.community.id))
250     .post_view;
251   expect(otherPost.post.id).toBeDefined();
252
253   // create a community and post on alpha
254   let alphaCommunity = (await createCommunity(alpha)).community_view;
255   expect(alphaCommunity.community.name).toBeDefined();
256   let alphaPost = (await createPost(alpha, alphaCommunity.community.id))
257     .post_view;
258   expect(alphaPost.post.id).toBeDefined();
259
260   // other user also posts on alpha's community
261   let otherAlphaPost = (
262     await createPost(otherUser, alphaCommunity.community.id)
263   ).post_view;
264   expect(otherAlphaPost.post.id).toBeDefined();
265
266   // alpha lists posts on home page, should contain all posts that were made
267   let posts = (await getPosts(alpha)).posts;
268   expect(posts).toBeDefined();
269   let postIds = posts.map(post => post.post.id);
270   expect(postIds).toContain(otherPost.post.id);
271   expect(postIds).toContain(alphaPost.post.id);
272   expect(postIds).toContain(otherAlphaPost.post.id);
273
274   // in moderator view, alpha should not see otherPost, wich was posted on a community alpha doesn't moderate
275   posts = (await getPosts(alpha, true)).posts;
276   expect(posts).toBeDefined();
277   postIds = posts.map(post => post.post.id);
278   expect(postIds).not.toContain(otherPost.post.id);
279   expect(postIds).toContain(alphaPost.post.id);
280   expect(postIds).toContain(otherAlphaPost.post.id);
281 });