]> Untitled Git - lemmy.git/blob - api_tests/src/community.spec.ts
Changing unit tests to api v2.
[lemmy.git] / api_tests / src / community.spec.ts
1 jest.setTimeout(120000);
2 import {
3   alpha,
4   beta,
5   setupLogins,
6   searchForCommunity,
7   createCommunity,
8   deleteCommunity,
9   removeCommunity,
10   getCommunity,
11   followCommunity,
12   delay,
13 } from './shared';
14 import { CommunityView } from 'lemmy-js-client';
15
16 beforeAll(async () => {
17   await setupLogins();
18 });
19
20 function assertCommunityFederation(
21   communityOne: CommunityView,
22   communityTwo: CommunityView
23 ) {
24   expect(communityOne.community.actor_id).toBe(communityTwo.community.actor_id);
25   expect(communityOne.community.name).toBe(communityTwo.community.name);
26   expect(communityOne.community.title).toBe(communityTwo.community.title);
27   expect(communityOne.community.description).toBe(
28     communityTwo.community.description
29   );
30   expect(communityOne.community.icon).toBe(communityTwo.community.icon);
31   expect(communityOne.community.banner).toBe(communityTwo.community.banner);
32   expect(communityOne.community.published).toBe(
33     communityTwo.community.published
34   );
35   expect(communityOne.creator.actor_id).toBe(communityTwo.creator.actor_id);
36   expect(communityOne.community.nsfw).toBe(communityTwo.community.nsfw);
37   expect(communityOne.community.category_id).toBe(
38     communityTwo.community.category_id
39   );
40   expect(communityOne.community.removed).toBe(communityTwo.community.removed);
41   expect(communityOne.community.deleted).toBe(communityTwo.community.deleted);
42 }
43
44 test('Create community', async () => {
45   let communityRes = await createCommunity(alpha);
46   expect(communityRes.community_view.community.name).toBeDefined();
47
48   // A dupe check
49   let prevName = communityRes.community_view.community.name;
50   let communityRes2: any = await createCommunity(alpha, prevName);
51   expect(communityRes2['error']).toBe('community_already_exists');
52   await delay();
53
54   // Cache the community on beta, make sure it has the other fields
55   let searchShort = `!${prevName}@lemmy-alpha:8541`;
56   let search = await searchForCommunity(beta, searchShort);
57   let communityOnBeta = search.communities[0];
58   assertCommunityFederation(communityOnBeta, communityRes.community_view);
59 });
60
61 test('Delete community', async () => {
62   let communityRes = await createCommunity(beta);
63   await delay();
64
65   // Cache the community on Alpha
66   let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`;
67   let search = await searchForCommunity(alpha, searchShort);
68   let communityOnAlpha = search.communities[0];
69   assertCommunityFederation(communityOnAlpha, communityRes.community_view);
70   await delay();
71
72   // Follow the community from alpha
73   let follow = await followCommunity(
74     alpha,
75     true,
76     communityOnAlpha.community.id
77   );
78
79   // Make sure the follow response went through
80   expect(follow.community_view.community.local).toBe(false);
81   await delay();
82
83   let deleteCommunityRes = await deleteCommunity(
84     beta,
85     true,
86     communityRes.community_view.community.id
87   );
88   expect(deleteCommunityRes.community_view.community.deleted).toBe(true);
89   await delay();
90
91   // Make sure it got deleted on A
92   let communityOnAlphaDeleted = await getCommunity(
93     alpha,
94     communityOnAlpha.community.id
95   );
96   expect(communityOnAlphaDeleted.community_view.community.deleted).toBe(true);
97   await delay();
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   await delay();
107
108   // Make sure it got undeleted on A
109   let communityOnAlphaUnDeleted = await getCommunity(
110     alpha,
111     communityOnAlpha.community.id
112   );
113   expect(communityOnAlphaUnDeleted.community_view.community.deleted).toBe(
114     false
115   );
116 });
117
118 test('Remove community', async () => {
119   let communityRes = await createCommunity(beta);
120   await delay();
121
122   // Cache the community on Alpha
123   let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`;
124   let search = await searchForCommunity(alpha, searchShort);
125   let communityOnAlpha = search.communities[0];
126   assertCommunityFederation(communityOnAlpha, communityRes.community_view);
127   await delay();
128
129   // Follow the community from alpha
130   let follow = await followCommunity(
131     alpha,
132     true,
133     communityOnAlpha.community.id
134   );
135
136   // Make sure the follow response went through
137   expect(follow.community_view.community.local).toBe(false);
138   await delay();
139
140   let removeCommunityRes = await removeCommunity(
141     beta,
142     true,
143     communityRes.community_view.community.id
144   );
145   expect(removeCommunityRes.community_view.community.removed).toBe(true);
146   await delay();
147
148   // Make sure it got Removed on A
149   let communityOnAlphaRemoved = await getCommunity(
150     alpha,
151     communityOnAlpha.community.id
152   );
153   expect(communityOnAlphaRemoved.community_view.community.removed).toBe(true);
154   await delay();
155
156   // unremove
157   let unremoveCommunityRes = await removeCommunity(
158     beta,
159     false,
160     communityRes.community_view.community.id
161   );
162   expect(unremoveCommunityRes.community_view.community.removed).toBe(false);
163   await delay();
164
165   // Make sure it got undeleted on A
166   let communityOnAlphaUnRemoved = await getCommunity(
167     alpha,
168     communityOnAlpha.community.id
169   );
170   expect(communityOnAlphaUnRemoved.community_view.community.removed).toBe(
171     false
172   );
173 });
174
175 test('Search for beta community', async () => {
176   let communityRes = await createCommunity(beta);
177   expect(communityRes.community_view.community.name).toBeDefined();
178   await delay();
179
180   let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`;
181   let search = await searchForCommunity(alpha, searchShort);
182   let communityOnAlpha = search.communities[0];
183   assertCommunityFederation(communityOnAlpha, communityRes.community_view);
184 });