1 jest.setTimeout(120000);
3 import { CommunityView } from "lemmy-js-client/dist/types/CommunityView";
15 banPersonFromCommunity,
26 beforeAll(async () => {
30 function assertCommunityFederation(
31 communityOne?: CommunityView,
32 communityTwo?: CommunityView,
34 expect(communityOne?.community.actor_id).toBe(
35 communityTwo?.community.actor_id,
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,
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,
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);
52 test("Create community", async () => {
53 let communityRes = await createCommunity(alpha);
54 expect(communityRes.community_view.community.name).toBeDefined();
57 let prevName = communityRes.community_view.community.name;
58 await expect(createCommunity(alpha, prevName)).rejects.toBe(
59 "community_already_exists",
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);
68 test("Delete community", async () => {
69 let communityRes = await createCommunity(beta);
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";
77 assertCommunityFederation(alphaCommunity, communityRes.community_view);
79 // Follow the community from alpha
80 let follow = await followCommunity(alpha, true, alphaCommunity.community.id);
82 // Make sure the follow response went through
83 expect(follow.community_view.community.local).toBe(false);
85 let deleteCommunityRes = await deleteCommunity(
88 communityRes.community_view.community.id,
90 expect(deleteCommunityRes.community_view.community.deleted).toBe(true);
91 expect(deleteCommunityRes.community_view.community.title).toBe(
92 communityRes.community_view.community.title,
95 // Make sure it got deleted on A
96 let communityOnAlphaDeleted = await getCommunity(
98 alphaCommunity.community.id,
100 expect(communityOnAlphaDeleted.community_view.community.deleted).toBe(true);
103 let undeleteCommunityRes = await deleteCommunity(
106 communityRes.community_view.community.id,
108 expect(undeleteCommunityRes.community_view.community.deleted).toBe(false);
110 // Make sure it got undeleted on A
111 let communityOnAlphaUnDeleted = await getCommunity(
113 alphaCommunity.community.id,
115 expect(communityOnAlphaUnDeleted.community_view.community.deleted).toBe(
120 test("Remove community", async () => {
121 let communityRes = await createCommunity(beta);
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";
129 assertCommunityFederation(alphaCommunity, communityRes.community_view);
131 // Follow the community from alpha
132 let follow = await followCommunity(alpha, true, alphaCommunity.community.id);
134 // Make sure the follow response went through
135 expect(follow.community_view.community.local).toBe(false);
137 let removeCommunityRes = await removeCommunity(
140 communityRes.community_view.community.id,
142 expect(removeCommunityRes.community_view.community.removed).toBe(true);
143 expect(removeCommunityRes.community_view.community.title).toBe(
144 communityRes.community_view.community.title,
147 // Make sure it got Removed on A
148 let communityOnAlphaRemoved = await getCommunity(
150 alphaCommunity.community.id,
152 expect(communityOnAlphaRemoved.community_view.community.removed).toBe(true);
155 let unremoveCommunityRes = await removeCommunity(
158 communityRes.community_view.community.id,
160 expect(unremoveCommunityRes.community_view.community.removed).toBe(false);
162 // Make sure it got undeleted on A
163 let communityOnAlphaUnRemoved = await getCommunity(
165 alphaCommunity.community.id,
167 expect(communityOnAlphaUnRemoved.community_view.community.removed).toBe(
172 test("Search for beta community", async () => {
173 let communityRes = await createCommunity(beta);
174 expect(communityRes.community_view.community.name).toBeDefined();
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);
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();
186 // gamma follows community and posts in it
187 let gammaCommunity = (
188 await resolveCommunity(gamma, communityRes.community.actor_id)
190 if (!gammaCommunity) {
191 throw "Missing gamma community";
193 await followCommunity(gamma, true, gammaCommunity.community.id);
195 await resolveCommunity(gamma, communityRes.community.actor_id)
197 if (!gammaCommunity) {
198 throw "Missing gamma community";
200 expect(gammaCommunity.subscribed).toBe("Subscribed");
201 let gammaPost = (await createPost(gamma, gammaCommunity.community.id))
203 expect(gammaPost.post.id).toBeDefined();
204 expect(gammaPost.creator_banned_from_community).toBe(false);
206 // admin of beta decides to ban gamma from community
207 let betaCommunity = (
208 await resolveCommunity(beta, communityRes.community.actor_id)
210 if (!betaCommunity) {
211 throw "Missing beta community";
213 let bannedUserInfo1 = (await getSite(gamma)).my_user?.local_user_view.person;
214 if (!bannedUserInfo1) {
215 throw "Missing banned user 1";
217 let bannedUserInfo2 = (await resolvePerson(beta, bannedUserInfo1.actor_id))
219 if (!bannedUserInfo2) {
220 throw "Missing banned user 2";
222 let banRes = await banPersonFromCommunity(
224 bannedUserInfo2.person.id,
225 betaCommunity.community.id,
229 expect(banRes.banned).toBe(true);
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);
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);
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,
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))
251 expect(otherPost.post.id).toBeDefined();
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))
258 expect(alphaPost.post.id).toBeDefined();
260 // other user also posts on alpha's community
261 let otherAlphaPost = (
262 await createPost(otherUser, alphaCommunity.community.id)
264 expect(otherAlphaPost.post.id).toBeDefined();
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);
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);