]> Untitled Git - lemmy.git/blob - api_tests/src/shared.ts
Merge pull request #1666 from LemmyNet/resolve-endpoint
[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 } 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'),
61 };
62
63 export let beta: API = {
64   client: new LemmyHttp('http://localhost:8551'),
65 };
66
67 export let gamma: API = {
68   client: new LemmyHttp('http://localhost:8561'),
69 };
70
71 export let delta: API = {
72   client: new LemmyHttp('http://localhost:8571'),
73 };
74
75 export let epsilon: API = {
76   client: new LemmyHttp('http://localhost:8581'),
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 resolvePost(
207   api: API,
208   post: Post
209 ): Promise<ResolveObjectResponse> {
210   let form: ResolveObject = {
211     q: post.ap_id,
212   };
213   return api.client.resolveObject(form);
214 }
215
216 export async function searchPostLocal(
217   api: API,
218   post: Post
219 ): Promise<SearchResponse> {
220   let form: Search = {
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: GetPost = {
233     id: post_id,
234   };
235   return api.client.getPost(form);
236 }
237
238 export async function resolveComment(
239   api: API,
240   comment: Comment
241 ): Promise<ResolveObjectResponse> {
242   let form: ResolveObject = {
243     q: comment.ap_id,
244   };
245   return api.client.resolveObject(form);
246 }
247
248 export async function resolveBetaCommunity(
249   api: API
250 ): Promise<ResolveObjectResponse> {
251   // Use short-hand search url
252   let form: ResolveObject = {
253     q: '!main@lemmy-beta:8551',
254   };
255   return api.client.resolveObject(form);
256 }
257
258 export async function resolveCommunity(
259   api: API,
260   q: string
261 ): Promise<ResolveObjectResponse> {
262   let form: ResolveObject = {
263     q,
264   };
265   return api.client.resolveObject(form);
266 }
267
268 export async function resolvePerson(
269   api: API,
270   apShortname: string
271 ): Promise<ResolveObjectResponse> {
272   let form: ResolveObject = {
273     q: apShortname,
274   };
275   return api.client.resolveObject(form);
276 }
277
278 export async function banPersonFromSite(
279   api: API,
280   person_id: number,
281   ban: boolean
282 ): Promise<BanPersonResponse> {
283   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
284   let form: BanPerson = {
285     person_id,
286     ban,
287     remove_data: false,
288     auth: api.auth,
289   };
290   return api.client.banPerson(form);
291 }
292
293 export async function banPersonFromCommunity(
294   api: API,
295   person_id: number,
296   community_id: number,
297   ban: boolean
298 ): Promise<BanFromCommunityResponse> {
299   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
300   let form: BanFromCommunity = {
301     person_id,
302     community_id,
303     remove_data: false,
304     ban,
305     auth: api.auth,
306   };
307   return api.client.banFromCommunity(form);
308 }
309
310 export async function followCommunity(
311   api: API,
312   follow: boolean,
313   community_id: number
314 ): Promise<CommunityResponse> {
315   let form: FollowCommunity = {
316     community_id,
317     follow,
318     auth: api.auth,
319   };
320   return api.client.followCommunity(form);
321 }
322
323 export async function likePost(
324   api: API,
325   score: number,
326   post: Post
327 ): Promise<PostResponse> {
328   let form: CreatePostLike = {
329     post_id: post.id,
330     score: score,
331     auth: api.auth,
332   };
333
334   return api.client.likePost(form);
335 }
336
337 export async function createComment(
338   api: API,
339   post_id: number,
340   parent_id?: number,
341   content = 'a jest test comment'
342 ): Promise<CommentResponse> {
343   let form: CreateComment = {
344     content,
345     post_id,
346     parent_id,
347     auth: api.auth,
348   };
349   return api.client.createComment(form);
350 }
351
352 export async function editComment(
353   api: API,
354   comment_id: number,
355   content = 'A jest test federated comment update'
356 ): Promise<CommentResponse> {
357   let form: EditComment = {
358     content,
359     comment_id,
360     auth: api.auth,
361   };
362   return api.client.editComment(form);
363 }
364
365 export async function deleteComment(
366   api: API,
367   deleted: boolean,
368   comment_id: number
369 ): Promise<CommentResponse> {
370   let form: DeleteComment = {
371     comment_id,
372     deleted,
373     auth: api.auth,
374   };
375   return api.client.deleteComment(form);
376 }
377
378 export async function removeComment(
379   api: API,
380   removed: boolean,
381   comment_id: number
382 ): Promise<CommentResponse> {
383   let form: RemoveComment = {
384     comment_id,
385     removed,
386     auth: api.auth,
387   };
388   return api.client.removeComment(form);
389 }
390
391 export async function getMentions(api: API): Promise<GetPersonMentionsResponse> {
392   let form: GetPersonMentions = {
393     sort: SortType.New,
394     unread_only: false,
395     auth: api.auth,
396   };
397   return api.client.getPersonMentions(form);
398 }
399
400 export async function likeComment(
401   api: API,
402   score: number,
403   comment: Comment
404 ): Promise<CommentResponse> {
405   let form: CreateCommentLike = {
406     comment_id: comment.id,
407     score,
408     auth: api.auth,
409   };
410   return api.client.likeComment(form);
411 }
412
413 export async function createCommunity(
414   api: API,
415   name_: string = randomString(5)
416 ): Promise<CommunityResponse> {
417   let description = 'a sample description';
418   let icon = 'https://image.flaticon.com/icons/png/512/35/35896.png';
419   let banner = 'https://image.flaticon.com/icons/png/512/35/35896.png';
420   let form: CreateCommunity = {
421     name: name_,
422     title: name_,
423     description,
424     icon,
425     banner,
426     nsfw: false,
427     auth: api.auth,
428   };
429   return api.client.createCommunity(form);
430 }
431
432 export async function getCommunity(
433   api: API,
434   id: number
435 ): Promise<CommunityResponse> {
436   let form: GetCommunity = {
437     id,
438   };
439   return api.client.getCommunity(form);
440 }
441
442 export async function deleteCommunity(
443   api: API,
444   deleted: boolean,
445   community_id: number
446 ): Promise<CommunityResponse> {
447   let form: DeleteCommunity = {
448     community_id,
449     deleted,
450     auth: api.auth,
451   };
452   return api.client.deleteCommunity(form);
453 }
454
455 export async function removeCommunity(
456   api: API,
457   removed: boolean,
458   community_id: number
459 ): Promise<CommunityResponse> {
460   let form: RemoveCommunity = {
461     community_id,
462     removed,
463     auth: api.auth,
464   };
465   return api.client.removeCommunity(form);
466 }
467
468 export async function createPrivateMessage(
469   api: API,
470   recipient_id: number
471 ): Promise<PrivateMessageResponse> {
472   let content = 'A jest test federated private message';
473   let form: CreatePrivateMessage = {
474     content,
475     recipient_id,
476     auth: api.auth,
477   };
478   return api.client.createPrivateMessage(form);
479 }
480
481 export async function editPrivateMessage(
482   api: API,
483   private_message_id: number
484 ): Promise<PrivateMessageResponse> {
485   let updatedContent = 'A jest test federated private message edited';
486   let form: EditPrivateMessage = {
487     content: updatedContent,
488     private_message_id,
489     auth: api.auth,
490   };
491   return api.client.editPrivateMessage(form);
492 }
493
494 export async function deletePrivateMessage(
495   api: API,
496   deleted: boolean,
497   private_message_id: number
498 ): Promise<PrivateMessageResponse> {
499   let form: DeletePrivateMessage = {
500     deleted,
501     private_message_id,
502     auth: api.auth,
503   };
504   return api.client.deletePrivateMessage(form);
505 }
506
507 export async function registerUser(
508   api: API,
509   username: string = randomString(5)
510 ): Promise<LoginResponse> {
511   let form: Register = {
512     username,
513     password: 'test',
514     password_verify: 'test',
515     show_nsfw: true,
516   };
517   return api.client.register(form);
518 }
519
520 export async function saveUserSettingsBio(
521   api: API
522 ): Promise<LoginResponse> {
523   let form: SaveUserSettings = {
524     show_nsfw: true,
525     theme: 'darkly',
526     default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
527     default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
528     lang: 'en',
529     show_avatars: true,
530     send_notifications_to_email: false,
531     bio: 'a changed bio',
532     auth: api.auth,
533   };
534   return saveUserSettings(api, form);
535 }
536
537 export async function saveUserSettings(
538   api: API,
539   form: SaveUserSettings
540 ): Promise<LoginResponse> {
541   return api.client.saveUserSettings(form);
542 }
543
544 export async function getSite(
545   api: API
546 ): Promise<GetSiteResponse> {
547   let form: GetSite = {
548     auth: api.auth,
549   };
550   return api.client.getSite(form);
551 }
552
553 export async function listPrivateMessages(
554   api: API
555 ): Promise<PrivateMessagesResponse> {
556   let form: GetPrivateMessages = {
557     auth: api.auth,
558     unread_only: false,
559     limit: 999,
560   };
561   return api.client.getPrivateMessages(form);
562 }
563
564 export async function unfollowRemotes(
565   api: API
566 ): Promise<GetSiteResponse> {
567   // Unfollow all remote communities
568   let site = await getSite(api);
569   let remoteFollowed = site.my_user.follows.filter(
570     c => c.community.local == false
571   );
572   for (let cu of remoteFollowed) {
573     await followCommunity(api, false, cu.community.id);
574   }
575   let siteRes = await getSite(api);
576   return siteRes;
577 }
578
579 export async function followBeta(api: API): Promise<CommunityResponse> {
580   let betaCommunity = (await resolveBetaCommunity(api)).community;
581   if (betaCommunity) {
582     let follow = await followCommunity(api, true, betaCommunity.community.id);
583     return follow;
584   }
585 }
586
587 export function delay(millis: number = 500) {
588   return new Promise(resolve => setTimeout(resolve, millis));
589 }
590
591 export function longDelay() {
592   return delay(10000);
593 }
594
595 export function wrapper(form: any): string {
596   return JSON.stringify(form);
597 }
598
599 function randomString(length: number): string {
600   var result = '';
601   var characters = 'abcdefghijklmnopqrstuvwxyz0123456789_';
602   var charactersLength = characters.length;
603   for (var i = 0; i < length; i++) {
604     result += characters.charAt(Math.floor(Math.random() * charactersLength));
605   }
606   return result;
607 }