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