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