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