]> Untitled Git - lemmy.git/blob - api_tests/src/shared.ts
Federate user account deletion (fixes #1284) (#2199)
[lemmy.git] / api_tests / src / shared.ts
1 import {
2   Login,
3   LoginResponse,
4   CreatePost,
5   EditPost,
6   CreateComment,
7   DeletePost,
8   RemovePost,
9   StickyPost,
10   LockPost,
11   PostResponse,
12   SearchResponse,
13   FollowCommunity,
14   CommunityResponse,
15   GetPostResponse,
16   Register,
17   Comment,
18   EditComment,
19   DeleteComment,
20   RemoveComment,
21   Search,
22   CommentResponse,
23   GetCommunity,
24   CreateCommunity,
25   DeleteCommunity,
26   RemoveCommunity,
27   GetPersonMentions,
28   CreateCommentLike,
29   CreatePostLike,
30   EditPrivateMessage,
31   DeletePrivateMessage,
32   GetPrivateMessages,
33   GetSite,
34   GetPost,
35   PrivateMessageResponse,
36   PrivateMessagesResponse,
37   GetPersonMentionsResponse,
38   SaveUserSettings,
39   SortType,
40   ListingType,
41   GetSiteResponse,
42   SearchType,
43   LemmyHttp,
44   BanPersonResponse,
45   BanPerson,
46   BanFromCommunity,
47   BanFromCommunityResponse,
48   Post,
49   CreatePrivateMessage,
50   ResolveObjectResponse,
51   ResolveObject,
52   CreatePostReport,
53   PostReport,
54   ListPostReports,
55   PostReportResponse,
56   ListPostReportsResponse,
57   CreateCommentReport,
58   CommentReportResponse,
59   ListCommentReports,
60   ListCommentReportsResponse,
61   DeleteAccount,
62 } from 'lemmy-js-client';
63
64 export interface API {
65   client: LemmyHttp;
66   auth?: string;
67 }
68
69 export let alpha: API = {
70   client: new LemmyHttp('http://127.0.0.1:8541'),
71 };
72
73 export let beta: API = {
74   client: new LemmyHttp('http://127.0.0.1:8551'),
75 };
76
77 export let gamma: API = {
78   client: new LemmyHttp('http://127.0.0.1:8561'),
79 };
80
81 export let delta: API = {
82   client: new LemmyHttp('http://127.0.0.1:8571'),
83 };
84
85 export let epsilon: API = {
86   client: new LemmyHttp('http://127.0.0.1:8581'),
87 };
88
89 const password = 'lemmylemmy'
90
91 export async function setupLogins() {
92   let formAlpha: Login = {
93     username_or_email: 'lemmy_alpha',
94     password,
95   };
96   let resAlpha = alpha.client.login(formAlpha);
97
98   let formBeta = {
99     username_or_email: 'lemmy_beta',
100     password,
101   };
102   let resBeta = beta.client.login(formBeta);
103
104   let formGamma = {
105     username_or_email: 'lemmy_gamma',
106     password,
107   };
108   let resGamma = gamma.client.login(formGamma);
109
110   let formDelta = {
111     username_or_email: 'lemmy_delta',
112     password,
113   };
114   let resDelta = delta.client.login(formDelta);
115
116   let formEpsilon = {
117     username_or_email: 'lemmy_epsilon',
118     password,
119   };
120   let resEpsilon = epsilon.client.login(formEpsilon);
121
122   let res = await Promise.all([
123     resAlpha,
124     resBeta,
125     resGamma,
126     resDelta,
127     resEpsilon,
128   ]);
129
130   alpha.auth = res[0].jwt;
131   beta.auth = res[1].jwt;
132   gamma.auth = res[2].jwt;
133   delta.auth = res[3].jwt;
134   epsilon.auth = res[4].jwt;
135 }
136
137 export async function createPost(
138   api: API,
139   community_id: number
140 ): Promise<PostResponse> {
141   let name = randomString(5);
142   let body = randomString(10);
143   let url = 'https://google.com/';
144   let form: CreatePost = {
145     name,
146     url,
147     body,
148     auth: api.auth,
149     community_id,
150     nsfw: false,
151   };
152   return api.client.createPost(form);
153 }
154
155 export async function editPost(api: API, post: Post): Promise<PostResponse> {
156   let name = 'A jest test federated post, updated';
157   let form: EditPost = {
158     name,
159     post_id: post.id,
160     auth: api.auth,
161     nsfw: false,
162   };
163   return api.client.editPost(form);
164 }
165
166 export async function deletePost(
167   api: API,
168   deleted: boolean,
169   post: Post
170 ): Promise<PostResponse> {
171   let form: DeletePost = {
172     post_id: post.id,
173     deleted: deleted,
174     auth: api.auth,
175   };
176   return api.client.deletePost(form);
177 }
178
179 export async function removePost(
180   api: API,
181   removed: boolean,
182   post: Post
183 ): Promise<PostResponse> {
184   let form: RemovePost = {
185     post_id: post.id,
186     removed,
187     auth: api.auth,
188   };
189   return api.client.removePost(form);
190 }
191
192 export async function stickyPost(
193   api: API,
194   stickied: boolean,
195   post: Post
196 ): Promise<PostResponse> {
197   let form: StickyPost = {
198     post_id: post.id,
199     stickied,
200     auth: api.auth,
201   };
202   return api.client.stickyPost(form);
203 }
204
205 export async function lockPost(
206   api: API,
207   locked: boolean,
208   post: Post
209 ): Promise<PostResponse> {
210   let form: LockPost = {
211     post_id: post.id,
212     locked,
213     auth: api.auth,
214   };
215   return api.client.lockPost(form);
216 }
217
218 export async function resolvePost(
219   api: API,
220   post: Post
221 ): Promise<ResolveObjectResponse> {
222   let form: ResolveObject = {
223     q: post.ap_id,
224   };
225   return api.client.resolveObject(form);
226 }
227
228 export async function searchPostLocal(
229   api: API,
230   post: Post
231 ): Promise<SearchResponse> {
232   let form: Search = {
233     q: post.name,
234     type_: SearchType.Posts,
235     sort: SortType.TopAll,
236   };
237   return api.client.search(form);
238 }
239
240 export async function getPost(
241   api: API,
242   post_id: number
243 ): Promise<GetPostResponse> {
244   let form: GetPost = {
245     id: post_id,
246   };
247   return api.client.getPost(form);
248 }
249
250 export async function resolveComment(
251   api: API,
252   comment: Comment
253 ): Promise<ResolveObjectResponse> {
254   let form: ResolveObject = {
255     q: comment.ap_id,
256   };
257   return api.client.resolveObject(form);
258 }
259
260 export async function resolveBetaCommunity(
261   api: API
262 ): Promise<ResolveObjectResponse> {
263   // Use short-hand search url
264   let form: ResolveObject = {
265     q: '!main@lemmy-beta:8551',
266   };
267   return api.client.resolveObject(form);
268 }
269
270 export async function resolveCommunity(
271   api: API,
272   q: string
273 ): Promise<ResolveObjectResponse> {
274   let form: ResolveObject = {
275     q,
276   };
277   return api.client.resolveObject(form);
278 }
279
280 export async function resolvePerson(
281   api: API,
282   apShortname: string
283 ): Promise<ResolveObjectResponse> {
284   let form: ResolveObject = {
285     q: apShortname,
286   };
287   return api.client.resolveObject(form);
288 }
289
290 export async function banPersonFromSite(
291   api: API,
292   person_id: number,
293   ban: boolean,
294   remove_data: boolean,
295 ): Promise<BanPersonResponse> {
296   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
297   let form: BanPerson = {
298     person_id,
299     ban,
300     remove_data,
301     auth: api.auth,
302   };
303   return api.client.banPerson(form);
304 }
305
306 export async function banPersonFromCommunity(
307   api: API,
308   person_id: number,
309   community_id: number,
310   remove_data: boolean,
311   ban: boolean
312 ): Promise<BanFromCommunityResponse> {
313   let form: BanFromCommunity = {
314     person_id,
315     community_id,
316     remove_data,
317     ban,
318     auth: api.auth,
319   };
320   return api.client.banFromCommunity(form);
321 }
322
323 export async function followCommunity(
324   api: API,
325   follow: boolean,
326   community_id: number
327 ): Promise<CommunityResponse> {
328   let form: FollowCommunity = {
329     community_id,
330     follow,
331     auth: api.auth,
332   };
333   return api.client.followCommunity(form);
334 }
335
336 export async function likePost(
337   api: API,
338   score: number,
339   post: Post
340 ): Promise<PostResponse> {
341   let form: CreatePostLike = {
342     post_id: post.id,
343     score: score,
344     auth: api.auth,
345   };
346
347   return api.client.likePost(form);
348 }
349
350 export async function createComment(
351   api: API,
352   post_id: number,
353   parent_id?: number,
354   content = 'a jest test comment'
355 ): Promise<CommentResponse> {
356   let form: CreateComment = {
357     content,
358     post_id,
359     parent_id,
360     auth: api.auth,
361   };
362   return api.client.createComment(form);
363 }
364
365 export async function editComment(
366   api: API,
367   comment_id: number,
368   content = 'A jest test federated comment update'
369 ): Promise<CommentResponse> {
370   let form: EditComment = {
371     content,
372     comment_id,
373     auth: api.auth,
374   };
375   return api.client.editComment(form);
376 }
377
378 export async function deleteComment(
379   api: API,
380   deleted: boolean,
381   comment_id: number
382 ): Promise<CommentResponse> {
383   let form: DeleteComment = {
384     comment_id,
385     deleted,
386     auth: api.auth,
387   };
388   return api.client.deleteComment(form);
389 }
390
391 export async function removeComment(
392   api: API,
393   removed: boolean,
394   comment_id: number
395 ): Promise<CommentResponse> {
396   let form: RemoveComment = {
397     comment_id,
398     removed,
399     auth: api.auth,
400   };
401   return api.client.removeComment(form);
402 }
403
404 export async function getMentions(api: API): Promise<GetPersonMentionsResponse> {
405   let form: GetPersonMentions = {
406     sort: SortType.New,
407     unread_only: false,
408     auth: api.auth,
409   };
410   return api.client.getPersonMentions(form);
411 }
412
413 export async function likeComment(
414   api: API,
415   score: number,
416   comment: Comment
417 ): Promise<CommentResponse> {
418   let form: CreateCommentLike = {
419     comment_id: comment.id,
420     score,
421     auth: api.auth,
422   };
423   return api.client.likeComment(form);
424 }
425
426 export async function createCommunity(
427   api: API,
428   name_: string = randomString(5)
429 ): Promise<CommunityResponse> {
430   let description = 'a sample description';
431   let form: CreateCommunity = {
432     name: name_,
433     title: name_,
434     description,
435     nsfw: false,
436     auth: api.auth,
437   };
438   return api.client.createCommunity(form);
439 }
440
441 export async function getCommunity(
442   api: API,
443   id: number
444 ): Promise<CommunityResponse> {
445   let form: GetCommunity = {
446     id,
447   };
448   return api.client.getCommunity(form);
449 }
450
451 export async function deleteCommunity(
452   api: API,
453   deleted: boolean,
454   community_id: number
455 ): Promise<CommunityResponse> {
456   let form: DeleteCommunity = {
457     community_id,
458     deleted,
459     auth: api.auth,
460   };
461   return api.client.deleteCommunity(form);
462 }
463
464 export async function removeCommunity(
465   api: API,
466   removed: boolean,
467   community_id: number
468 ): Promise<CommunityResponse> {
469   let form: RemoveCommunity = {
470     community_id,
471     removed,
472     auth: api.auth,
473   };
474   return api.client.removeCommunity(form);
475 }
476
477 export async function createPrivateMessage(
478   api: API,
479   recipient_id: number
480 ): Promise<PrivateMessageResponse> {
481   let content = 'A jest test federated private message';
482   let form: CreatePrivateMessage = {
483     content,
484     recipient_id,
485     auth: api.auth,
486   };
487   return api.client.createPrivateMessage(form);
488 }
489
490 export async function editPrivateMessage(
491   api: API,
492   private_message_id: number
493 ): Promise<PrivateMessageResponse> {
494   let updatedContent = 'A jest test federated private message edited';
495   let form: EditPrivateMessage = {
496     content: updatedContent,
497     private_message_id,
498     auth: api.auth,
499   };
500   return api.client.editPrivateMessage(form);
501 }
502
503 export async function deletePrivateMessage(
504   api: API,
505   deleted: boolean,
506   private_message_id: number
507 ): Promise<PrivateMessageResponse> {
508   let form: DeletePrivateMessage = {
509     deleted,
510     private_message_id,
511     auth: api.auth,
512   };
513   return api.client.deletePrivateMessage(form);
514 }
515
516 export async function registerUser(
517   api: API,
518   username: string = randomString(5)
519 ): Promise<LoginResponse> {
520   let form: Register = {
521     username,
522     password,
523     password_verify: password,
524     show_nsfw: true,
525   };
526   return api.client.register(form);
527 }
528
529 export async function saveUserSettingsBio(
530   api: API
531 ): Promise<LoginResponse> {
532   let form: SaveUserSettings = {
533     show_nsfw: true,
534     theme: 'darkly',
535     default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
536     default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
537     lang: 'en',
538     show_avatars: true,
539     send_notifications_to_email: false,
540     bio: 'a changed bio',
541     auth: api.auth,
542   };
543   return saveUserSettings(api, form);
544 }
545
546 export async function saveUserSettings(
547   api: API,
548   form: SaveUserSettings
549 ): Promise<LoginResponse> {
550   return api.client.saveUserSettings(form);
551 }
552
553 export async function deleteUser(
554   api: API,
555 ): Promise<LoginResponse> {
556   let form: DeleteAccount = {
557     auth: api.auth,
558     password
559   };
560   return api.client.deleteAccount(form);
561 }
562
563 export async function getSite(
564   api: API
565 ): Promise<GetSiteResponse> {
566   let form: GetSite = {
567     auth: api.auth,
568   };
569   return api.client.getSite(form);
570 }
571
572 export async function listPrivateMessages(
573   api: API
574 ): Promise<PrivateMessagesResponse> {
575   let form: GetPrivateMessages = {
576     auth: api.auth,
577     unread_only: false,
578     limit: 999,
579   };
580   return api.client.getPrivateMessages(form);
581 }
582
583 export async function unfollowRemotes(
584   api: API
585 ): Promise<GetSiteResponse> {
586   // Unfollow all remote communities
587   let site = await getSite(api);
588   let remoteFollowed = site.my_user.follows.filter(
589     c => c.community.local == false
590   );
591   for (let cu of remoteFollowed) {
592     await followCommunity(api, false, cu.community.id);
593   }
594   let siteRes = await getSite(api);
595   return siteRes;
596 }
597
598 export async function followBeta(api: API): Promise<CommunityResponse> {
599   let betaCommunity = (await resolveBetaCommunity(api)).community;
600   if (betaCommunity) {
601     let follow = await followCommunity(api, true, betaCommunity.community.id);
602     return follow;
603   }
604 }
605
606 export async function reportPost(
607   api: API,
608   post_id: number,
609   reason: string,
610 ): Promise<PostReportResponse> {
611   let form: CreatePostReport = {
612     post_id,
613     reason,
614     auth: api.auth,
615   };
616   return api.client.createPostReport(form);
617 }
618
619 export async function listPostReports(api: API): Promise<ListPostReportsResponse> {
620   let form: ListPostReports = {
621     auth: api.auth,
622   };
623   return api.client.listPostReports(form);
624 }
625
626 export async function reportComment(
627   api: API,
628   comment_id: number,
629   reason: string,
630 ): Promise<CommentReportResponse> {
631   let form: CreateCommentReport = {
632     comment_id,
633     reason,
634     auth: api.auth,
635   };
636   return api.client.createCommentReport(form);
637 }
638
639 export async function listCommentReports(api: API): Promise<ListCommentReportsResponse> {
640   let form: ListCommentReports = {
641     auth: api.auth,
642   };
643   return api.client.listCommentReports(form);
644 }
645
646 export function delay(millis: number = 500) {
647   return new Promise(resolve => setTimeout(resolve, millis));
648 }
649
650 export function longDelay() {
651   return delay(10000);
652 }
653
654 export function wrapper(form: any): string {
655   return JSON.stringify(form);
656 }
657
658 export function randomString(length: number): string {
659   var result = '';
660   var characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
661   var charactersLength = characters.length;
662   for (var i = 0; i < length; i++) {
663     result += characters.charAt(Math.floor(Math.random() * charactersLength));
664   }
665   return result;
666 }
667
668 export async function unfollows() {
669   await unfollowRemotes(alpha);
670   await unfollowRemotes(gamma);
671   await unfollowRemotes(delta);
672   await unfollowRemotes(epsilon);
673 }