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