]> Untitled Git - lemmy.git/blob - api_tests/src/community.spec.ts
d9a76cf875bfb211186f9a34951903c7ce7e78d5
[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 } from "./shared";
22
23 beforeAll(async () => {
24   await setupLogins();
25 });
26
27 function assertCommunityFederation(
28   communityOne?: CommunityView,
29   communityTwo?: CommunityView,
30 ) {
31   expect(communityOne?.community.actor_id).toBe(
32     communityTwo?.community.actor_id,
33   );
34   expect(communityOne?.community.name).toBe(communityTwo?.community.name);
35   expect(communityOne?.community.title).toBe(communityTwo?.community.title);
36   expect(communityOne?.community.description).toBe(
37     communityTwo?.community.description,
38   );
39   expect(communityOne?.community.icon).toBe(communityTwo?.community.icon);
40   expect(communityOne?.community.banner).toBe(communityTwo?.community.banner);
41   expect(communityOne?.community.published).toBe(
42     communityTwo?.community.published,
43   );
44   expect(communityOne?.community.nsfw).toBe(communityTwo?.community.nsfw);
45   expect(communityOne?.community.removed).toBe(communityTwo?.community.removed);
46   expect(communityOne?.community.deleted).toBe(communityTwo?.community.deleted);
47 }
48
49 test("Create community", async () => {
50   let communityRes = await createCommunity(alpha);
51   expect(communityRes.community_view.community.name).toBeDefined();
52
53   // A dupe check
54   let prevName = communityRes.community_view.community.name;
55   let communityRes2: any = await createCommunity(alpha, prevName);
56   expect(communityRes2["error"]).toBe("community_already_exists");
57
58   // Cache the community on beta, make sure it has the other fields
59   let searchShort = `!${prevName}@lemmy-alpha:8541`;
60   let betaCommunity = (await resolveCommunity(beta, searchShort)).community;
61   assertCommunityFederation(betaCommunity, communityRes.community_view);
62 });
63
64 test("Delete community", async () => {
65   let communityRes = await createCommunity(beta);
66
67   // Cache the community on Alpha
68   let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`;
69   let alphaCommunity = (await resolveCommunity(alpha, searchShort)).community;
70   if (!alphaCommunity) {
71     throw "Missing alpha community";
72   }
73   assertCommunityFederation(alphaCommunity, communityRes.community_view);
74
75   // Follow the community from alpha
76   let follow = await followCommunity(alpha, true, alphaCommunity.community.id);
77
78   // Make sure the follow response went through
79   expect(follow.community_view.community.local).toBe(false);
80
81   let deleteCommunityRes = await deleteCommunity(
82     beta,
83     true,
84     communityRes.community_view.community.id,
85   );
86   expect(deleteCommunityRes.community_view.community.deleted).toBe(true);
87   expect(deleteCommunityRes.community_view.community.title).toBe(
88     communityRes.community_view.community.title,
89   );
90
91   // Make sure it got deleted on A
92   let communityOnAlphaDeleted = await getCommunity(
93     alpha,
94     alphaCommunity.community.id,
95   );
96   expect(communityOnAlphaDeleted.community_view.community.deleted).toBe(true);
97
98   // Undelete
99   let undeleteCommunityRes = await deleteCommunity(
100     beta,
101     false,
102     communityRes.community_view.community.id,
103   );
104   expect(undeleteCommunityRes.community_view.community.deleted).toBe(false);
105
106   // Make sure it got undeleted on A
107   let communityOnAlphaUnDeleted = await getCommunity(
108     alpha,
109     alphaCommunity.community.id,
110   );
111   expect(communityOnAlphaUnDeleted.community_view.community.deleted).toBe(
112     false,
113   );
114 });
115
116 test("Remove community", async () => {
117   let communityRes = await createCommunity(beta);
118
119   // Cache the community on Alpha
120   let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`;
121   let alphaCommunity = (await resolveCommunity(alpha, searchShort)).community;
122   if (!alphaCommunity) {
123     throw "Missing alpha community";
124   }
125   assertCommunityFederation(alphaCommunity, communityRes.community_view);
126
127   // Follow the community from alpha
128   let follow = await followCommunity(alpha, true, alphaCommunity.community.id);
129
130   // Make sure the follow response went through
131   expect(follow.community_view.community.local).toBe(false);
132
133   let removeCommunityRes = await removeCommunity(
134     beta,
135     true,
136     communityRes.community_view.community.id,
137   );
138   expect(removeCommunityRes.community_view.community.removed).toBe(true);
139   expect(removeCommunityRes.community_view.community.title).toBe(
140     communityRes.community_view.community.title,
141   );
142
143   // Make sure it got Removed on A
144   let communityOnAlphaRemoved = await getCommunity(
145     alpha,
146     alphaCommunity.community.id,
147   );
148   expect(communityOnAlphaRemoved.community_view.community.removed).toBe(true);
149
150   // unremove
151   let unremoveCommunityRes = await removeCommunity(
152     beta,
153     false,
154     communityRes.community_view.community.id,
155   );
156   expect(unremoveCommunityRes.community_view.community.removed).toBe(false);
157
158   // Make sure it got undeleted on A
159   let communityOnAlphaUnRemoved = await getCommunity(
160     alpha,
161     alphaCommunity.community.id,
162   );
163   expect(communityOnAlphaUnRemoved.community_view.community.removed).toBe(
164     false,
165   );
166 });
167
168 test("Search for beta community", async () => {
169   let communityRes = await createCommunity(beta);
170   expect(communityRes.community_view.community.name).toBeDefined();
171
172   let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`;
173   let alphaCommunity = (await resolveCommunity(alpha, searchShort)).community;
174   assertCommunityFederation(alphaCommunity, communityRes.community_view);
175 });
176
177 test("Admin actions in remote community are not federated to origin", async () => {
178   // create a community on alpha
179   let communityRes = (await createCommunity(alpha)).community_view;
180   expect(communityRes.community.name).toBeDefined();
181
182   // gamma follows community and posts in it
183   let gammaCommunity = (
184     await resolveCommunity(gamma, communityRes.community.actor_id)
185   ).community;
186   if (!gammaCommunity) {
187     throw "Missing gamma community";
188   }
189   await followCommunity(gamma, true, gammaCommunity.community.id);
190   gammaCommunity = (
191     await resolveCommunity(gamma, communityRes.community.actor_id)
192   ).community;
193   if (!gammaCommunity) {
194     throw "Missing gamma community";
195   }
196   expect(gammaCommunity.subscribed).toBe("Subscribed");
197   let gammaPost = (await createPost(gamma, gammaCommunity.community.id))
198     .post_view;
199   expect(gammaPost.post.id).toBeDefined();
200   expect(gammaPost.creator_banned_from_community).toBe(false);
201
202   // admin of beta decides to ban gamma from community
203   let betaCommunity = (
204     await resolveCommunity(beta, communityRes.community.actor_id)
205   ).community;
206   if (!betaCommunity) {
207     throw "Missing beta community";
208   }
209   let bannedUserInfo1 = (await getSite(gamma)).my_user?.local_user_view.person;
210   if (!bannedUserInfo1) {
211     throw "Missing banned user 1";
212   }
213   let bannedUserInfo2 = (await resolvePerson(beta, bannedUserInfo1.actor_id))
214     .person;
215   if (!bannedUserInfo2) {
216     throw "Missing banned user 2";
217   }
218   let banRes = await banPersonFromCommunity(
219     beta,
220     bannedUserInfo2.person.id,
221     betaCommunity.community.id,
222     true,
223     true,
224   );
225   expect(banRes.banned).toBe(true);
226
227   // ban doesnt federate to community's origin instance alpha
228   let alphaPost = (await resolvePost(alpha, gammaPost.post)).post;
229   expect(alphaPost?.creator_banned_from_community).toBe(false);
230
231   // and neither to gamma
232   let gammaPost2 = await getPost(gamma, gammaPost.post.id);
233   expect(gammaPost2.post_view.creator_banned_from_community).toBe(false);
234 });