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