]> Untitled Git - lemmy.git/blob - api_tests/src/shared.ts
Trying to fix post test again.
[lemmy.git] / api_tests / src / shared.ts
1 import {
2   LoginForm,
3   LoginResponse,
4   Post,
5   PostForm,
6   Comment,
7   DeletePostForm,
8   RemovePostForm,
9   StickyPostForm,
10   LockPostForm,
11   PostResponse,
12   SearchResponse,
13   FollowCommunityForm,
14   CommunityResponse,
15   GetFollowedCommunitiesResponse,
16   GetPostResponse,
17   RegisterForm,
18   CommentForm,
19   DeleteCommentForm,
20   RemoveCommentForm,
21   SearchForm,
22   CommentResponse,
23   GetCommunityForm,
24   CommunityForm,
25   DeleteCommunityForm,
26   RemoveCommunityForm,
27   GetUserMentionsForm,
28   CommentLikeForm,
29   CreatePostLikeForm,
30   PrivateMessageForm,
31   EditPrivateMessageForm,
32   DeletePrivateMessageForm,
33   GetFollowedCommunitiesForm,
34   GetPrivateMessagesForm,
35   GetSiteForm,
36   GetPostForm,
37   PrivateMessageResponse,
38   PrivateMessagesResponse,
39   GetUserMentionsResponse,
40   UserSettingsForm,
41   SortType,
42   ListingType,
43   GetSiteResponse,
44   SearchType,
45   LemmyHttp,
46   BanUserResponse,
47   BanUserForm,
48   BanFromCommunityForm,
49   BanFromCommunityResponse,
50 } from 'lemmy-js-client';
51
52 export interface API {
53   client: LemmyHttp;
54   auth?: string;
55 }
56
57 export let alpha: API = {
58   client: new LemmyHttp('http://localhost:8541/api/v1'),
59 };
60
61 export let beta: API = {
62   client: new LemmyHttp('http://localhost:8551/api/v1'),
63 };
64
65 export let gamma: API = {
66   client: new LemmyHttp('http://localhost:8561/api/v1'),
67 };
68
69 export let delta: API = {
70   client: new LemmyHttp('http://localhost:8571/api/v1'),
71 };
72
73 export let epsilon: API = {
74   client: new LemmyHttp('http://localhost:8581/api/v1'),
75 };
76
77 export async function setupLogins() {
78   let formAlpha: LoginForm = {
79     username_or_email: 'lemmy_alpha',
80     password: 'lemmy',
81   };
82   let resAlpha = alpha.client.login(formAlpha);
83
84   let formBeta = {
85     username_or_email: 'lemmy_beta',
86     password: 'lemmy',
87   };
88   let resBeta = beta.client.login(formBeta);
89
90   let formGamma = {
91     username_or_email: 'lemmy_gamma',
92     password: 'lemmy',
93   };
94   let resGamma = gamma.client.login(formGamma);
95
96   let formDelta = {
97     username_or_email: 'lemmy_delta',
98     password: 'lemmy',
99   };
100   let resDelta = delta.client.login(formDelta);
101
102   let formEpsilon = {
103     username_or_email: 'lemmy_epsilon',
104     password: 'lemmy',
105   };
106   let resEpsilon = epsilon.client.login(formEpsilon);
107
108   let res = await Promise.all([
109     resAlpha,
110     resBeta,
111     resGamma,
112     resDelta,
113     resEpsilon,
114   ]);
115
116   alpha.auth = res[0].jwt;
117   beta.auth = res[1].jwt;
118   gamma.auth = res[2].jwt;
119   delta.auth = res[3].jwt;
120   epsilon.auth = res[4].jwt;
121 }
122
123 export async function createPost(
124   api: API,
125   community_id: number
126 ): Promise<PostResponse> {
127   let name = randomString(5);
128   let body = randomString(10);
129   let url = 'https://google.com/';
130   let form: PostForm = {
131     name,
132     url,
133     body,
134     auth: api.auth,
135     community_id,
136     nsfw: false,
137   };
138   return api.client.createPost(form);
139 }
140
141 export async function updatePost(api: API, post: Post): Promise<PostResponse> {
142   let name = 'A jest test federated post, updated';
143   let form: PostForm = {
144     name,
145     edit_id: post.id,
146     auth: api.auth,
147     nsfw: false,
148   };
149   return api.client.editPost(form);
150 }
151
152 export async function deletePost(
153   api: API,
154   deleted: boolean,
155   post: Post
156 ): Promise<PostResponse> {
157   let form: DeletePostForm = {
158     edit_id: post.id,
159     deleted: deleted,
160     auth: api.auth,
161   };
162   return api.client.deletePost(form);
163 }
164
165 export async function removePost(
166   api: API,
167   removed: boolean,
168   post: Post
169 ): Promise<PostResponse> {
170   let form: RemovePostForm = {
171     edit_id: post.id,
172     removed,
173     auth: api.auth,
174   };
175   return api.client.removePost(form);
176 }
177
178 export async function stickyPost(
179   api: API,
180   stickied: boolean,
181   post: Post
182 ): Promise<PostResponse> {
183   let form: StickyPostForm = {
184     edit_id: post.id,
185     stickied,
186     auth: api.auth,
187   };
188   return api.client.stickyPost(form);
189 }
190
191 export async function lockPost(
192   api: API,
193   locked: boolean,
194   post: Post
195 ): Promise<PostResponse> {
196   let form: LockPostForm = {
197     edit_id: post.id,
198     locked,
199     auth: api.auth,
200   };
201   return api.client.lockPost(form);
202 }
203
204 export async function searchPost(
205   api: API,
206   post: Post
207 ): Promise<SearchResponse> {
208   let form: SearchForm = {
209     q: post.ap_id,
210     type_: SearchType.Posts,
211     sort: SortType.TopAll,
212   };
213   return api.client.search(form);
214 }
215
216 export async function searchPostLocal(
217   api: API,
218   post: Post
219 ): Promise<SearchResponse> {
220   let form: SearchForm = {
221     q: post.name,
222     type_: SearchType.Posts,
223     sort: SortType.TopAll,
224   };
225   return api.client.search(form);
226 }
227
228 export async function getPost(
229   api: API,
230   post_id: number
231 ): Promise<GetPostResponse> {
232   let form: GetPostForm = {
233     id: post_id,
234   };
235   return api.client.getPost(form);
236 }
237
238 export async function searchComment(
239   api: API,
240   comment: Comment
241 ): Promise<SearchResponse> {
242   let form: SearchForm = {
243     q: comment.ap_id,
244     type_: SearchType.Comments,
245     sort: SortType.TopAll,
246   };
247   return api.client.search(form);
248 }
249
250 export async function searchForBetaCommunity(
251   api: API
252 ): Promise<SearchResponse> {
253   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
254   // Use short-hand search url
255   let form: SearchForm = {
256     q: '!main@lemmy-beta:8551',
257     type_: SearchType.Communities,
258     sort: SortType.TopAll,
259   };
260   return api.client.search(form);
261 }
262
263 export async function searchForCommunity(
264   api: API,
265   q: string,
266 ): Promise<SearchResponse> {
267   // Use short-hand search url
268   let form: SearchForm = {
269     q,
270     type_: SearchType.Communities,
271     sort: SortType.TopAll,
272   };
273   return api.client.search(form);
274 }
275
276 export async function searchForUser(
277   api: API,
278   apShortname: string
279 ): Promise<SearchResponse> {
280   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
281   // Use short-hand search url
282   let form: SearchForm = {
283     q: apShortname,
284     type_: SearchType.Users,
285     sort: SortType.TopAll,
286   };
287   return api.client.search(form);
288 }
289
290 export async function banUserFromSite(
291   api: API,
292   user_id: number,
293   ban: boolean,
294 ): Promise<BanUserResponse> {
295   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
296   // Use short-hand search url
297   let form: BanUserForm = {
298     user_id,
299     ban,
300     auth: api.auth,
301   };
302   return api.client.banUser(form);
303 }
304
305 export async function banUserFromCommunity(
306   api: API,
307   user_id: number,
308   community_id: number,
309   ban: boolean,
310 ): Promise<BanFromCommunityResponse> {
311   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
312   // Use short-hand search url
313   let form: BanFromCommunityForm = {
314     user_id,
315     community_id,
316     ban,
317     auth: api.auth,
318   };
319   return api.client.banFromCommunity(form);
320 }
321
322 export async function followCommunity(
323   api: API,
324   follow: boolean,
325   community_id: number
326 ): Promise<CommunityResponse> {
327   let form: FollowCommunityForm = {
328     community_id,
329     follow,
330     auth: api.auth,
331   };
332   return api.client.followCommunity(form);
333 }
334
335 export async function checkFollowedCommunities(
336   api: API
337 ): Promise<GetFollowedCommunitiesResponse> {
338   let form: GetFollowedCommunitiesForm = {
339     auth: api.auth,
340   };
341   return api.client.getFollowedCommunities(form);
342 }
343
344 export async function likePost(
345   api: API,
346   score: number,
347   post: Post
348 ): Promise<PostResponse> {
349   let form: CreatePostLikeForm = {
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: CommentForm = {
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 updateComment(
374   api: API,
375   edit_id: number,
376   content = 'A jest test federated comment update'
377 ): Promise<CommentResponse> {
378   let form: CommentForm = {
379     content,
380     edit_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   edit_id: number
390 ): Promise<CommentResponse> {
391   let form: DeleteCommentForm = {
392     edit_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   edit_id: number
403 ): Promise<CommentResponse> {
404   let form: RemoveCommentForm = {
405     edit_id,
406     removed,
407     auth: api.auth,
408   };
409   return api.client.removeComment(form);
410 }
411
412 export async function getMentions(api: API): Promise<GetUserMentionsResponse> {
413   let form: GetUserMentionsForm = {
414     sort: SortType.New,
415     unread_only: false,
416     auth: api.auth,
417   };
418   return api.client.getUserMentions(form);
419 }
420
421 export async function likeComment(
422   api: API,
423   score: number,
424   comment: Comment
425 ): Promise<CommentResponse> {
426   let form: CommentLikeForm = {
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 icon = 'https://image.flaticon.com/icons/png/512/35/35896.png';
440   let banner = 'https://image.flaticon.com/icons/png/512/35/35896.png';
441   let form: CommunityForm = {
442     name: name_,
443     title: name_,
444     description,
445     icon,
446     banner,
447     category_id: 1,
448     nsfw: false,
449     auth: api.auth,
450   };
451   return api.client.createCommunity(form);
452 }
453
454 export async function getCommunity(
455   api: API,
456   id: number,
457 ): Promise<CommunityResponse> {
458   let form: GetCommunityForm = {
459     id,
460   };
461   return api.client.getCommunity(form);
462 }
463
464 export async function deleteCommunity(
465   api: API,
466   deleted: boolean,
467   edit_id: number
468 ): Promise<CommunityResponse> {
469   let form: DeleteCommunityForm = {
470     edit_id,
471     deleted,
472     auth: api.auth,
473   };
474   return api.client.deleteCommunity(form);
475 }
476
477 export async function removeCommunity(
478   api: API,
479   removed: boolean,
480   edit_id: number
481 ): Promise<CommunityResponse> {
482   let form: RemoveCommunityForm = {
483     edit_id,
484     removed,
485     auth: api.auth,
486   };
487   return api.client.removeCommunity(form);
488 }
489
490 export async function createPrivateMessage(
491   api: API,
492   recipient_id: number
493 ): Promise<PrivateMessageResponse> {
494   let content = 'A jest test federated private message';
495   let form: PrivateMessageForm = {
496     content,
497     recipient_id,
498     auth: api.auth,
499   };
500   return api.client.createPrivateMessage(form);
501 }
502
503 export async function updatePrivateMessage(
504   api: API,
505   edit_id: number
506 ): Promise<PrivateMessageResponse> {
507   let updatedContent = 'A jest test federated private message edited';
508   let form: EditPrivateMessageForm = {
509     content: updatedContent,
510     edit_id,
511     auth: api.auth,
512   };
513   return api.client.editPrivateMessage(form);
514 }
515
516 export async function deletePrivateMessage(
517   api: API,
518   deleted: boolean,
519   edit_id: number
520 ): Promise<PrivateMessageResponse> {
521   let form: DeletePrivateMessageForm = {
522     deleted,
523     edit_id,
524     auth: api.auth,
525   };
526   return api.client.deletePrivateMessage(form);
527 }
528
529 export async function registerUser(
530   api: API,
531   username: string = randomString(5)
532 ): Promise<LoginResponse> {
533   let form: RegisterForm = {
534     username,
535     password: 'test',
536     password_verify: 'test',
537     admin: false,
538     show_nsfw: true,
539   };
540   return api.client.register(form);
541 }
542
543 export async function saveUserSettingsBio(
544   api: API,
545   auth: string
546 ): Promise<LoginResponse> {
547   let form: UserSettingsForm = {
548     show_nsfw: true,
549     theme: 'darkly',
550     default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
551     default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
552     lang: 'en',
553     show_avatars: true,
554     send_notifications_to_email: false,
555     bio: 'a changed bio',
556     auth,
557   };
558   return saveUserSettings(api, form);
559 }
560
561 export async function saveUserSettings(
562   api: API,
563   form: UserSettingsForm
564 ): Promise<LoginResponse> {
565   return api.client.saveUserSettings(form);
566 }
567
568 export async function getSite(
569   api: API,
570   auth: string
571 ): Promise<GetSiteResponse> {
572   let form: GetSiteForm = {
573     auth,
574   };
575   return api.client.getSite(form);
576 }
577
578 export async function listPrivateMessages(
579   api: API
580 ): Promise<PrivateMessagesResponse> {
581   let form: GetPrivateMessagesForm = {
582     auth: api.auth,
583     unread_only: false,
584     limit: 999,
585   };
586   return api.client.getPrivateMessages(form);
587 }
588
589 export async function unfollowRemotes(
590   api: API
591 ): Promise<GetFollowedCommunitiesResponse> {
592   // Unfollow all remote communities
593   let followed = await checkFollowedCommunities(api);
594   let remoteFollowed = followed.communities.filter(
595     c => c.community_local == false
596   );
597   for (let cu of remoteFollowed) {
598     await followCommunity(api, false, cu.community_id);
599   }
600   let followed2 = await checkFollowedCommunities(api);
601   return followed2;
602 }
603
604 export async function followBeta(api: API): Promise<CommunityResponse> {
605   await unfollowRemotes(api);
606
607   // Cache it
608   let search = await searchForBetaCommunity(api);
609   let com = search.communities.filter(c => c.local == false);
610   if (com[0]) {
611     let follow = await followCommunity(api, true, com[0].id);
612     return follow;
613   }
614 }
615
616 export function delay(millis: number = 2000) {
617   return new Promise((resolve, _reject) => {
618     setTimeout(_ => resolve(), millis);
619   });
620 }
621
622 export function longDelay() {
623   return delay(10000);
624 }
625
626 export function wrapper(form: any): string {
627   return JSON.stringify(form);
628 }
629
630 function randomString(length: number): string {
631   var result = '';
632   var characters = 'abcdefghijklmnopqrstuvwxyz0123456789_';
633   var charactersLength = characters.length;
634   for (var i = 0; i < length; i++) {
635     result += characters.charAt(Math.floor(Math.random() * charactersLength));
636   }
637   return result;
638 }