]> Untitled Git - lemmy.git/blob - api_tests/src/shared.ts
Merge pull request #1782 from LemmyNet/password_min_length
[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 const password = 'lemmylemmy'
80
81 export async function setupLogins() {
82   let formAlpha: Login = {
83     username_or_email: 'lemmy_alpha',
84     password,
85   };
86   let resAlpha = alpha.client.login(formAlpha);
87
88   let formBeta = {
89     username_or_email: 'lemmy_beta',
90     password,
91   };
92   let resBeta = beta.client.login(formBeta);
93
94   let formGamma = {
95     username_or_email: 'lemmy_gamma',
96     password,
97   };
98   let resGamma = gamma.client.login(formGamma);
99
100   let formDelta = {
101     username_or_email: 'lemmy_delta',
102     password,
103   };
104   let resDelta = delta.client.login(formDelta);
105
106   let formEpsilon = {
107     username_or_email: 'lemmy_epsilon',
108     password,
109   };
110   let resEpsilon = epsilon.client.login(formEpsilon);
111
112   let res = await Promise.all([
113     resAlpha,
114     resBeta,
115     resGamma,
116     resDelta,
117     resEpsilon,
118   ]);
119
120   alpha.auth = res[0].jwt;
121   beta.auth = res[1].jwt;
122   gamma.auth = res[2].jwt;
123   delta.auth = res[3].jwt;
124   epsilon.auth = res[4].jwt;
125 }
126
127 export async function createPost(
128   api: API,
129   community_id: number
130 ): Promise<PostResponse> {
131   let name = randomString(5);
132   let body = randomString(10);
133   let url = 'https://google.com/';
134   let form: CreatePost = {
135     name,
136     url,
137     body,
138     auth: api.auth,
139     community_id,
140     nsfw: false,
141   };
142   return api.client.createPost(form);
143 }
144
145 export async function editPost(api: API, post: Post): Promise<PostResponse> {
146   let name = 'A jest test federated post, updated';
147   let form: EditPost = {
148     name,
149     post_id: post.id,
150     auth: api.auth,
151     nsfw: false,
152   };
153   return api.client.editPost(form);
154 }
155
156 export async function deletePost(
157   api: API,
158   deleted: boolean,
159   post: Post
160 ): Promise<PostResponse> {
161   let form: DeletePost = {
162     post_id: post.id,
163     deleted: deleted,
164     auth: api.auth,
165   };
166   return api.client.deletePost(form);
167 }
168
169 export async function removePost(
170   api: API,
171   removed: boolean,
172   post: Post
173 ): Promise<PostResponse> {
174   let form: RemovePost = {
175     post_id: post.id,
176     removed,
177     auth: api.auth,
178   };
179   return api.client.removePost(form);
180 }
181
182 export async function stickyPost(
183   api: API,
184   stickied: boolean,
185   post: Post
186 ): Promise<PostResponse> {
187   let form: StickyPost = {
188     post_id: post.id,
189     stickied,
190     auth: api.auth,
191   };
192   return api.client.stickyPost(form);
193 }
194
195 export async function lockPost(
196   api: API,
197   locked: boolean,
198   post: Post
199 ): Promise<PostResponse> {
200   let form: LockPost = {
201     post_id: post.id,
202     locked,
203     auth: api.auth,
204   };
205   return api.client.lockPost(form);
206 }
207
208 export async function resolvePost(
209   api: API,
210   post: Post
211 ): Promise<ResolveObjectResponse> {
212   let form: ResolveObject = {
213     q: post.ap_id,
214   };
215   return api.client.resolveObject(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 resolveComment(
241   api: API,
242   comment: Comment
243 ): Promise<ResolveObjectResponse> {
244   let form: ResolveObject = {
245     q: comment.ap_id,
246   };
247   return api.client.resolveObject(form);
248 }
249
250 export async function resolveBetaCommunity(
251   api: API
252 ): Promise<ResolveObjectResponse> {
253   // Use short-hand search url
254   let form: ResolveObject = {
255     q: '!main@lemmy-beta:8551',
256   };
257   return api.client.resolveObject(form);
258 }
259
260 export async function resolveCommunity(
261   api: API,
262   q: string
263 ): Promise<ResolveObjectResponse> {
264   let form: ResolveObject = {
265     q,
266   };
267   return api.client.resolveObject(form);
268 }
269
270 export async function resolvePerson(
271   api: API,
272   apShortname: string
273 ): Promise<ResolveObjectResponse> {
274   let form: ResolveObject = {
275     q: apShortname,
276   };
277   return api.client.resolveObject(form);
278 }
279
280 export async function banPersonFromSite(
281   api: API,
282   person_id: number,
283   ban: boolean
284 ): Promise<BanPersonResponse> {
285   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
286   let form: BanPerson = {
287     person_id,
288     ban,
289     remove_data: false,
290     auth: api.auth,
291   };
292   return api.client.banPerson(form);
293 }
294
295 export async function banPersonFromCommunity(
296   api: API,
297   person_id: number,
298   community_id: number,
299   ban: boolean
300 ): Promise<BanFromCommunityResponse> {
301   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
302   let form: BanFromCommunity = {
303     person_id,
304     community_id,
305     remove_data: false,
306     ban,
307     auth: api.auth,
308   };
309   return api.client.banFromCommunity(form);
310 }
311
312 export async function followCommunity(
313   api: API,
314   follow: boolean,
315   community_id: number
316 ): Promise<CommunityResponse> {
317   let form: FollowCommunity = {
318     community_id,
319     follow,
320     auth: api.auth,
321   };
322   return api.client.followCommunity(form);
323 }
324
325 export async function likePost(
326   api: API,
327   score: number,
328   post: Post
329 ): Promise<PostResponse> {
330   let form: CreatePostLike = {
331     post_id: post.id,
332     score: score,
333     auth: api.auth,
334   };
335
336   return api.client.likePost(form);
337 }
338
339 export async function createComment(
340   api: API,
341   post_id: number,
342   parent_id?: number,
343   content = 'a jest test comment'
344 ): Promise<CommentResponse> {
345   let form: CreateComment = {
346     content,
347     post_id,
348     parent_id,
349     auth: api.auth,
350   };
351   return api.client.createComment(form);
352 }
353
354 export async function editComment(
355   api: API,
356   comment_id: number,
357   content = 'A jest test federated comment update'
358 ): Promise<CommentResponse> {
359   let form: EditComment = {
360     content,
361     comment_id,
362     auth: api.auth,
363   };
364   return api.client.editComment(form);
365 }
366
367 export async function deleteComment(
368   api: API,
369   deleted: boolean,
370   comment_id: number
371 ): Promise<CommentResponse> {
372   let form: DeleteComment = {
373     comment_id,
374     deleted,
375     auth: api.auth,
376   };
377   return api.client.deleteComment(form);
378 }
379
380 export async function removeComment(
381   api: API,
382   removed: boolean,
383   comment_id: number
384 ): Promise<CommentResponse> {
385   let form: RemoveComment = {
386     comment_id,
387     removed,
388     auth: api.auth,
389   };
390   return api.client.removeComment(form);
391 }
392
393 export async function getMentions(api: API): Promise<GetPersonMentionsResponse> {
394   let form: GetPersonMentions = {
395     sort: SortType.New,
396     unread_only: false,
397     auth: api.auth,
398   };
399   return api.client.getPersonMentions(form);
400 }
401
402 export async function likeComment(
403   api: API,
404   score: number,
405   comment: Comment
406 ): Promise<CommentResponse> {
407   let form: CreateCommentLike = {
408     comment_id: comment.id,
409     score,
410     auth: api.auth,
411   };
412   return api.client.likeComment(form);
413 }
414
415 export async function createCommunity(
416   api: API,
417   name_: string = randomString(5)
418 ): Promise<CommunityResponse> {
419   let description = 'a sample description';
420   let icon = 'https://image.flaticon.com/icons/png/512/35/35896.png';
421   let banner = 'https://image.flaticon.com/icons/png/512/35/35896.png';
422   let form: CreateCommunity = {
423     name: name_,
424     title: name_,
425     description,
426     icon,
427     banner,
428     nsfw: false,
429     auth: api.auth,
430   };
431   return api.client.createCommunity(form);
432 }
433
434 export async function getCommunity(
435   api: API,
436   id: number
437 ): Promise<CommunityResponse> {
438   let form: GetCommunity = {
439     id,
440   };
441   return api.client.getCommunity(form);
442 }
443
444 export async function deleteCommunity(
445   api: API,
446   deleted: boolean,
447   community_id: number
448 ): Promise<CommunityResponse> {
449   let form: DeleteCommunity = {
450     community_id,
451     deleted,
452     auth: api.auth,
453   };
454   return api.client.deleteCommunity(form);
455 }
456
457 export async function removeCommunity(
458   api: API,
459   removed: boolean,
460   community_id: number
461 ): Promise<CommunityResponse> {
462   let form: RemoveCommunity = {
463     community_id,
464     removed,
465     auth: api.auth,
466   };
467   return api.client.removeCommunity(form);
468 }
469
470 export async function createPrivateMessage(
471   api: API,
472   recipient_id: number
473 ): Promise<PrivateMessageResponse> {
474   let content = 'A jest test federated private message';
475   let form: CreatePrivateMessage = {
476     content,
477     recipient_id,
478     auth: api.auth,
479   };
480   return api.client.createPrivateMessage(form);
481 }
482
483 export async function editPrivateMessage(
484   api: API,
485   private_message_id: number
486 ): Promise<PrivateMessageResponse> {
487   let updatedContent = 'A jest test federated private message edited';
488   let form: EditPrivateMessage = {
489     content: updatedContent,
490     private_message_id,
491     auth: api.auth,
492   };
493   return api.client.editPrivateMessage(form);
494 }
495
496 export async function deletePrivateMessage(
497   api: API,
498   deleted: boolean,
499   private_message_id: number
500 ): Promise<PrivateMessageResponse> {
501   let form: DeletePrivateMessage = {
502     deleted,
503     private_message_id,
504     auth: api.auth,
505   };
506   return api.client.deletePrivateMessage(form);
507 }
508
509 export async function registerUser(
510   api: API,
511   username: string = randomString(5)
512 ): Promise<LoginResponse> {
513   let form: Register = {
514     username,
515     password,
516     password_verify: password,
517     show_nsfw: true,
518   };
519   return api.client.register(form);
520 }
521
522 export async function saveUserSettingsBio(
523   api: API
524 ): Promise<LoginResponse> {
525   let form: SaveUserSettings = {
526     show_nsfw: true,
527     theme: 'darkly',
528     default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
529     default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
530     lang: 'en',
531     show_avatars: true,
532     send_notifications_to_email: false,
533     bio: 'a changed bio',
534     auth: api.auth,
535   };
536   return saveUserSettings(api, form);
537 }
538
539 export async function saveUserSettings(
540   api: API,
541   form: SaveUserSettings
542 ): Promise<LoginResponse> {
543   return api.client.saveUserSettings(form);
544 }
545
546 export async function getSite(
547   api: API
548 ): Promise<GetSiteResponse> {
549   let form: GetSite = {
550     auth: api.auth,
551   };
552   return api.client.getSite(form);
553 }
554
555 export async function listPrivateMessages(
556   api: API
557 ): Promise<PrivateMessagesResponse> {
558   let form: GetPrivateMessages = {
559     auth: api.auth,
560     unread_only: false,
561     limit: 999,
562   };
563   return api.client.getPrivateMessages(form);
564 }
565
566 export async function unfollowRemotes(
567   api: API
568 ): Promise<GetSiteResponse> {
569   // Unfollow all remote communities
570   let site = await getSite(api);
571   let remoteFollowed = site.my_user.follows.filter(
572     c => c.community.local == false
573   );
574   for (let cu of remoteFollowed) {
575     await followCommunity(api, false, cu.community.id);
576   }
577   let siteRes = await getSite(api);
578   return siteRes;
579 }
580
581 export async function followBeta(api: API): Promise<CommunityResponse> {
582   let betaCommunity = (await resolveBetaCommunity(api)).community;
583   if (betaCommunity) {
584     let follow = await followCommunity(api, true, betaCommunity.community.id);
585     return follow;
586   }
587 }
588
589 export function delay(millis: number = 500) {
590   return new Promise(resolve => setTimeout(resolve, millis));
591 }
592
593 export function longDelay() {
594   return delay(10000);
595 }
596
597 export function wrapper(form: any): string {
598   return JSON.stringify(form);
599 }
600
601 function randomString(length: number): string {
602   var result = '';
603   var characters = 'abcdefghijklmnopqrstuvwxyz0123456789_';
604   var charactersLength = characters.length;
605   for (var i = 0; i < length; i++) {
606     result += characters.charAt(Math.floor(Math.random() * charactersLength));
607   }
608   return result;
609 }