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