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