]> Untitled Git - lemmy.git/blob - api_tests/src/community.spec.ts
723f3bb3f543e5699c83b71466f616d9d0fc5d34
[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   setupLogins,
8   resolveCommunity,
9   createCommunity,
10   deleteCommunity,
11   removeCommunity,
12   getCommunity,
13   followCommunity,
14 } from './shared';
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.unwrapOr("none")).toBe(
28     communityTwo.community.description.unwrapOr("none")
29   );
30   expect(communityOne.community.icon.unwrapOr("none")).toBe(communityTwo.community.icon.unwrapOr("none"));
31   expect(communityOne.community.banner.unwrapOr("none")).toBe(communityTwo.community.banner.unwrapOr("none"));
32   expect(communityOne.community.published).toBe(
33     communityTwo.community.published
34   );
35   expect(communityOne.community.nsfw).toBe(communityTwo.community.nsfw);
36   expect(communityOne.community.removed).toBe(communityTwo.community.removed);
37   expect(communityOne.community.deleted).toBe(communityTwo.community.deleted);
38 }
39
40 test('Create community', async () => {
41   let communityRes = await createCommunity(alpha);
42   expect(communityRes.community_view.community.name).toBeDefined();
43
44   // A dupe check
45   let prevName = communityRes.community_view.community.name;
46   let communityRes2: any = await createCommunity(alpha, prevName);
47   expect(communityRes2['error']).toBe('community_already_exists');
48
49   // Cache the community on beta, make sure it has the other fields
50   let searchShort = `!${prevName}@lemmy-alpha:8541`;
51   let betaCommunity = (await resolveCommunity(beta, searchShort)).community.unwrap();
52   assertCommunityFederation(betaCommunity, communityRes.community_view);
53 });
54
55 test('Delete community', async () => {
56   let communityRes = await createCommunity(beta);
57
58   // Cache the community on Alpha
59   let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`;
60   let alphaCommunity = (await resolveCommunity(alpha, searchShort)).community.unwrap();
61   assertCommunityFederation(alphaCommunity, communityRes.community_view);
62
63   // Follow the community from alpha
64   let follow = await followCommunity(
65     alpha,
66     true,
67     alphaCommunity.community.id
68   );
69
70   // Make sure the follow response went through
71   expect(follow.community_view.community.local).toBe(false);
72
73   let deleteCommunityRes = await deleteCommunity(
74     beta,
75     true,
76     communityRes.community_view.community.id
77   );
78   expect(deleteCommunityRes.community_view.community.deleted).toBe(true);
79   expect(deleteCommunityRes.community_view.community.title).toBe(communityRes.community_view.community.title);
80
81   // Make sure it got deleted on A
82   let communityOnAlphaDeleted = await getCommunity(
83     alpha,
84     alphaCommunity.community.id
85   );
86   expect(communityOnAlphaDeleted.community_view.community.deleted).toBe(true);
87
88   // Undelete
89   let undeleteCommunityRes = await deleteCommunity(
90     beta,
91     false,
92     communityRes.community_view.community.id
93   );
94   expect(undeleteCommunityRes.community_view.community.deleted).toBe(false);
95
96   // Make sure it got undeleted on A
97   let communityOnAlphaUnDeleted = await getCommunity(
98     alpha,
99     alphaCommunity.community.id
100   );
101   expect(communityOnAlphaUnDeleted.community_view.community.deleted).toBe(
102     false
103   );
104 });
105
106 test('Remove community', async () => {
107   let communityRes = await createCommunity(beta);
108
109   // Cache the community on Alpha
110   let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`;
111   let alphaCommunity = (await resolveCommunity(alpha, searchShort)).community.unwrap();
112   assertCommunityFederation(alphaCommunity, communityRes.community_view);
113
114   // Follow the community from alpha
115   let follow = await followCommunity(
116     alpha,
117     true,
118     alphaCommunity.community.id
119   );
120
121   // Make sure the follow response went through
122   expect(follow.community_view.community.local).toBe(false);
123
124   let removeCommunityRes = await removeCommunity(
125     beta,
126     true,
127     communityRes.community_view.community.id
128   );
129   expect(removeCommunityRes.community_view.community.removed).toBe(true);
130   expect(removeCommunityRes.community_view.community.title).toBe(communityRes.community_view.community.title);
131
132   // Make sure it got Removed on A
133   let communityOnAlphaRemoved = await getCommunity(
134     alpha,
135     alphaCommunity.community.id
136   );
137   expect(communityOnAlphaRemoved.community_view.community.removed).toBe(true);
138
139   // unremove
140   let unremoveCommunityRes = await removeCommunity(
141     beta,
142     false,
143     communityRes.community_view.community.id
144   );
145   expect(unremoveCommunityRes.community_view.community.removed).toBe(false);
146
147   // Make sure it got undeleted on A
148   let communityOnAlphaUnRemoved = await getCommunity(
149     alpha,
150     alphaCommunity.community.id
151   );
152   expect(communityOnAlphaUnRemoved.community_view.community.removed).toBe(
153     false
154   );
155 });
156
157 test('Search for beta community', async () => {
158   let communityRes = await createCommunity(beta);
159   expect(communityRes.community_view.community.name).toBeDefined();
160
161   let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`;
162   let alphaCommunity = (await resolveCommunity(alpha, searchShort)).community.unwrap();
163   assertCommunityFederation(alphaCommunity, communityRes.community_view);
164 });