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