]> Untitled Git - lemmy.git/blobdiff - api_tests/src/shared.ts
Adding diesel enums for SortType and ListingType (#2808)
[lemmy.git] / api_tests / src / shared.ts
index 8c7a793313d92d50b4c0c905b495734b1dc2f951..b8e97db0d37215979e29c3c33271674bd433bc33 100644 (file)
@@ -6,7 +6,6 @@ import {
   CreateComment,
   DeletePost,
   RemovePost,
-  StickyPost,
   LockPost,
   PostResponse,
   SearchResponse,
@@ -49,62 +48,84 @@ import {
   CreatePrivateMessage,
   ResolveObjectResponse,
   ResolveObject,
-} from 'lemmy-js-client';
+  CreatePostReport,
+  ListPostReports,
+  PostReportResponse,
+  ListPostReportsResponse,
+  CreateCommentReport,
+  CommentReportResponse,
+  ListCommentReports,
+  ListCommentReportsResponse,
+  DeleteAccount,
+  DeleteAccountResponse,
+  EditSite,
+  CommentSortType,
+  GetComments,
+  GetCommentsResponse,
+  FeaturePost,
+  PostFeatureType,
+  RegistrationMode,
+} from "lemmy-js-client";
 
 export interface API {
   client: LemmyHttp;
-  auth?: string;
+  auth: string;
 }
 
 export let alpha: API = {
-  client: new LemmyHttp('http://localhost:8541'),
+  client: new LemmyHttp("http://127.0.0.1:8541"),
+  auth: "",
 };
 
 export let beta: API = {
-  client: new LemmyHttp('http://localhost:8551'),
+  client: new LemmyHttp("http://127.0.0.1:8551"),
+  auth: "",
 };
 
 export let gamma: API = {
-  client: new LemmyHttp('http://localhost:8561'),
+  client: new LemmyHttp("http://127.0.0.1:8561"),
+  auth: "",
 };
 
 export let delta: API = {
-  client: new LemmyHttp('http://localhost:8571'),
+  client: new LemmyHttp("http://127.0.0.1:8571"),
+  auth: "",
 };
 
 export let epsilon: API = {
-  client: new LemmyHttp('http://localhost:8581'),
+  client: new LemmyHttp("http://127.0.0.1:8581"),
+  auth: "",
 };
 
-const password = 'lemmylemmy'
+const password = "lemmylemmy";
 
 export async function setupLogins() {
   let formAlpha: Login = {
-    username_or_email: 'lemmy_alpha',
+    username_or_email: "lemmy_alpha",
     password,
   };
   let resAlpha = alpha.client.login(formAlpha);
 
-  let formBeta = {
-    username_or_email: 'lemmy_beta',
+  let formBeta: Login = {
+    username_or_email: "lemmy_beta",
     password,
   };
   let resBeta = beta.client.login(formBeta);
 
-  let formGamma = {
-    username_or_email: 'lemmy_gamma',
+  let formGamma: Login = {
+    username_or_email: "lemmy_gamma",
     password,
   };
   let resGamma = gamma.client.login(formGamma);
 
-  let formDelta = {
-    username_or_email: 'lemmy_delta',
+  let formDelta: Login = {
+    username_or_email: "lemmy_delta",
     password,
   };
   let resDelta = delta.client.login(formDelta);
 
-  let formEpsilon = {
-    username_or_email: 'lemmy_epsilon',
+  let formEpsilon: Login = {
+    username_or_email: "lemmy_epsilon",
     password,
   };
   let resEpsilon = epsilon.client.login(formEpsilon);
@@ -117,11 +138,64 @@ export async function setupLogins() {
     resEpsilon,
   ]);
 
-  alpha.auth = res[0].jwt;
-  beta.auth = res[1].jwt;
-  gamma.auth = res[2].jwt;
-  delta.auth = res[3].jwt;
-  epsilon.auth = res[4].jwt;
+  alpha.auth = res[0].jwt ?? "";
+  beta.auth = res[1].jwt ?? "";
+  gamma.auth = res[2].jwt ?? "";
+  delta.auth = res[3].jwt ?? "";
+  epsilon.auth = res[4].jwt ?? "";
+
+  // Registration applications are now enabled by default, need to disable them
+  let editSiteForm: EditSite = {
+    registration_mode: RegistrationMode.Open,
+    rate_limit_message: 999,
+    rate_limit_post: 999,
+    rate_limit_register: 999,
+    rate_limit_image: 999,
+    rate_limit_comment: 999,
+    rate_limit_search: 999,
+    auth: "",
+  };
+
+  // Set the blocks and auths for each
+  editSiteForm.auth = alpha.auth;
+  editSiteForm.allowed_instances = [
+    "lemmy-beta",
+    "lemmy-gamma",
+    "lemmy-delta",
+    "lemmy-epsilon",
+  ];
+  await alpha.client.editSite(editSiteForm);
+
+  editSiteForm.auth = beta.auth;
+  editSiteForm.allowed_instances = [
+    "lemmy-alpha",
+    "lemmy-gamma",
+    "lemmy-delta",
+    "lemmy-epsilon",
+  ];
+  await beta.client.editSite(editSiteForm);
+
+  editSiteForm.auth = gamma.auth;
+  editSiteForm.allowed_instances = [
+    "lemmy-alpha",
+    "lemmy-beta",
+    "lemmy-delta",
+    "lemmy-epsilon",
+  ];
+  await gamma.client.editSite(editSiteForm);
+
+  editSiteForm.allowed_instances = ["lemmy-beta"];
+  editSiteForm.auth = delta.auth;
+  await delta.client.editSite(editSiteForm);
+
+  editSiteForm.auth = epsilon.auth;
+  editSiteForm.allowed_instances = [];
+  editSiteForm.blocked_instances = ["lemmy-alpha"];
+  await epsilon.client.editSite(editSiteForm);
+
+  // Create the main alpha/beta communities
+  await createCommunity(alpha, "main");
+  await createCommunity(beta, "main");
 }
 
 export async function createPost(
@@ -130,25 +204,23 @@ export async function createPost(
 ): Promise<PostResponse> {
   let name = randomString(5);
   let body = randomString(10);
-  let url = 'https://google.com/';
+  let url = "https://google.com/";
   let form: CreatePost = {
     name,
     url,
     body,
     auth: api.auth,
     community_id,
-    nsfw: false,
   };
   return api.client.createPost(form);
 }
 
 export async function editPost(api: API, post: Post): Promise<PostResponse> {
-  let name = 'A jest test federated post, updated';
+  let name = "A jest test federated post, updated";
   let form: EditPost = {
     name,
     post_id: post.id,
     auth: api.auth,
-    nsfw: false,
   };
   return api.client.editPost(form);
 }
@@ -179,17 +251,18 @@ export async function removePost(
   return api.client.removePost(form);
 }
 
-export async function stickyPost(
+export async function featurePost(
   api: API,
-  stickied: boolean,
+  featured: boolean,
   post: Post
 ): Promise<PostResponse> {
-  let form: StickyPost = {
+  let form: FeaturePost = {
     post_id: post.id,
-    stickied,
+    featured,
+    feature_type: PostFeatureType.Community,
     auth: api.auth,
   };
-  return api.client.stickyPost(form);
+  return api.client.featurePost(form);
 }
 
 export async function lockPost(
@@ -211,6 +284,7 @@ export async function resolvePost(
 ): Promise<ResolveObjectResponse> {
   let form: ResolveObject = {
     q: post.ap_id,
+    auth: api.auth,
   };
   return api.client.resolveObject(form);
 }
@@ -223,6 +297,7 @@ export async function searchPostLocal(
     q: post.name,
     type_: SearchType.Posts,
     sort: SortType.TopAll,
+    auth: api.auth,
   };
   return api.client.search(form);
 }
@@ -233,16 +308,31 @@ export async function getPost(
 ): Promise<GetPostResponse> {
   let form: GetPost = {
     id: post_id,
+    auth: api.auth,
   };
   return api.client.getPost(form);
 }
 
+export async function getComments(
+  api: API,
+  post_id: number
+): Promise<GetCommentsResponse> {
+  let form: GetComments = {
+    post_id: post_id,
+    type_: ListingType.All,
+    sort: CommentSortType.New,
+    auth: api.auth,
+  };
+  return api.client.getComments(form);
+}
+
 export async function resolveComment(
   api: API,
   comment: Comment
 ): Promise<ResolveObjectResponse> {
   let form: ResolveObject = {
     q: comment.ap_id,
+    auth: api.auth,
   };
   return api.client.resolveObject(form);
 }
@@ -252,7 +342,8 @@ export async function resolveBetaCommunity(
 ): Promise<ResolveObjectResponse> {
   // Use short-hand search url
   let form: ResolveObject = {
-    q: '!main@lemmy-beta:8551',
+    q: "!main@lemmy-beta:8551",
+    auth: api.auth,
   };
   return api.client.resolveObject(form);
 }
@@ -263,6 +354,7 @@ export async function resolveCommunity(
 ): Promise<ResolveObjectResponse> {
   let form: ResolveObject = {
     q,
+    auth: api.auth,
   };
   return api.client.resolveObject(form);
 }
@@ -273,6 +365,7 @@ export async function resolvePerson(
 ): Promise<ResolveObjectResponse> {
   let form: ResolveObject = {
     q: apShortname,
+    auth: api.auth,
   };
   return api.client.resolveObject(form);
 }
@@ -280,13 +373,14 @@ export async function resolvePerson(
 export async function banPersonFromSite(
   api: API,
   person_id: number,
-  ban: boolean
+  ban: boolean,
+  remove_data: boolean
 ): Promise<BanPersonResponse> {
   // Make sure lemmy-beta/c/main is cached on lemmy_alpha
   let form: BanPerson = {
     person_id,
     ban,
-    remove_data: false,
+    remove_data: remove_data,
     auth: api.auth,
   };
   return api.client.banPerson(form);
@@ -296,13 +390,13 @@ export async function banPersonFromCommunity(
   api: API,
   person_id: number,
   community_id: number,
+  remove_data: boolean,
   ban: boolean
 ): Promise<BanFromCommunityResponse> {
-  // Make sure lemmy-beta/c/main is cached on lemmy_alpha
   let form: BanFromCommunity = {
     person_id,
     community_id,
-    remove_data: false,
+    remove_data: remove_data,
     ban,
     auth: api.auth,
   };
@@ -340,7 +434,7 @@ export async function createComment(
   api: API,
   post_id: number,
   parent_id?: number,
-  content = 'a jest test comment'
+  content = "a jest test comment"
 ): Promise<CommentResponse> {
   let form: CreateComment = {
     content,
@@ -354,7 +448,7 @@ export async function createComment(
 export async function editComment(
   api: API,
   comment_id: number,
-  content = 'A jest test federated comment update'
+  content = "A jest test federated comment update"
 ): Promise<CommentResponse> {
   let form: EditComment = {
     content,
@@ -390,9 +484,11 @@ export async function removeComment(
   return api.client.removeComment(form);
 }
 
-export async function getMentions(api: API): Promise<GetPersonMentionsResponse> {
+export async function getMentions(
+  api: API
+): Promise<GetPersonMentionsResponse> {
   let form: GetPersonMentions = {
-    sort: SortType.New,
+    sort: CommentSortType.New,
     unread_only: false,
     auth: api.auth,
   };
@@ -416,16 +512,11 @@ export async function createCommunity(
   api: API,
   name_: string = randomString(5)
 ): Promise<CommunityResponse> {
-  let description = 'a sample description';
-  let icon = 'https://image.flaticon.com/icons/png/512/35/35896.png';
-  let banner = 'https://image.flaticon.com/icons/png/512/35/35896.png';
+  let description = "a sample description";
   let form: CreateCommunity = {
     name: name_,
     title: name_,
     description,
-    icon,
-    banner,
-    nsfw: false,
     auth: api.auth,
   };
   return api.client.createCommunity(form);
@@ -437,6 +528,7 @@ export async function getCommunity(
 ): Promise<CommunityResponse> {
   let form: GetCommunity = {
     id,
+    auth: api.auth,
   };
   return api.client.getCommunity(form);
 }
@@ -471,7 +563,7 @@ export async function createPrivateMessage(
   api: API,
   recipient_id: number
 ): Promise<PrivateMessageResponse> {
-  let content = 'A jest test federated private message';
+  let content = "A jest test federated private message";
   let form: CreatePrivateMessage = {
     content,
     recipient_id,
@@ -484,7 +576,7 @@ export async function editPrivateMessage(
   api: API,
   private_message_id: number
 ): Promise<PrivateMessageResponse> {
-  let updatedContent = 'A jest test federated private message edited';
+  let updatedContent = "A jest test federated private message edited";
   let form: EditPrivateMessage = {
     content: updatedContent,
     private_message_id,
@@ -519,23 +611,43 @@ export async function registerUser(
   return api.client.register(form);
 }
 
-export async function saveUserSettingsBio(
-  api: API
-): Promise<LoginResponse> {
+export async function saveUserSettingsBio(api: API): Promise<LoginResponse> {
   let form: SaveUserSettings = {
     show_nsfw: true,
-    theme: 'darkly',
-    default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
-    default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
-    lang: 'en',
+    theme: "darkly",
+    default_sort_type: SortType.Active,
+    default_listing_type: ListingType.All,
+    interface_language: "en",
     show_avatars: true,
     send_notifications_to_email: false,
-    bio: 'a changed bio',
+    bio: "a changed bio",
     auth: api.auth,
   };
   return saveUserSettings(api, form);
 }
 
+export async function saveUserSettingsFederated(
+  api: API
+): Promise<LoginResponse> {
+  let avatar = "https://image.flaticon.com/icons/png/512/35/35896.png";
+  let banner = "https://image.flaticon.com/icons/png/512/36/35896.png";
+  let bio = "a changed bio";
+  let form: SaveUserSettings = {
+    show_nsfw: false,
+    default_sort_type: SortType.Hot,
+    default_listing_type: ListingType.All,
+    interface_language: "",
+    avatar,
+    banner,
+    display_name: "user321",
+    show_avatars: false,
+    send_notifications_to_email: false,
+    bio,
+    auth: api.auth,
+  };
+  return await saveUserSettings(alpha, form);
+}
+
 export async function saveUserSettings(
   api: API,
   form: SaveUserSettings
@@ -543,9 +655,15 @@ export async function saveUserSettings(
   return api.client.saveUserSettings(form);
 }
 
-export async function getSite(
-  api: API
-): Promise<GetSiteResponse> {
+export async function deleteUser(api: API): Promise<DeleteAccountResponse> {
+  let form: DeleteAccount = {
+    auth: api.auth,
+    password,
+  };
+  return api.client.deleteAccount(form);
+}
+
+export async function getSite(api: API): Promise<GetSiteResponse> {
   let form: GetSite = {
     auth: api.auth,
   };
@@ -558,19 +676,15 @@ export async function listPrivateMessages(
   let form: GetPrivateMessages = {
     auth: api.auth,
     unread_only: false,
-    limit: 999,
   };
   return api.client.getPrivateMessages(form);
 }
 
-export async function unfollowRemotes(
-  api: API
-): Promise<GetSiteResponse> {
+export async function unfollowRemotes(api: API): Promise<GetSiteResponse> {
   // Unfollow all remote communities
   let site = await getSite(api);
-  let remoteFollowed = site.my_user.follows.filter(
-    c => c.community.local == false
-  );
+  let remoteFollowed =
+    site.my_user?.follows.filter(c => c.community.local == false) ?? [];
   for (let cu of remoteFollowed) {
     await followCommunity(api, false, cu.community.id);
   }
@@ -583,10 +697,56 @@ export async function followBeta(api: API): Promise<CommunityResponse> {
   if (betaCommunity) {
     let follow = await followCommunity(api, true, betaCommunity.community.id);
     return follow;
+  } else {
+    return Promise.reject("no community worked");
   }
 }
 
-export function delay(millis: number = 500) {
+export async function reportPost(
+  api: API,
+  post_id: number,
+  reason: string
+): Promise<PostReportResponse> {
+  let form: CreatePostReport = {
+    post_id,
+    reason,
+    auth: api.auth,
+  };
+  return api.client.createPostReport(form);
+}
+
+export async function listPostReports(
+  api: API
+): Promise<ListPostReportsResponse> {
+  let form: ListPostReports = {
+    auth: api.auth,
+  };
+  return api.client.listPostReports(form);
+}
+
+export async function reportComment(
+  api: API,
+  comment_id: number,
+  reason: string
+): Promise<CommentReportResponse> {
+  let form: CreateCommentReport = {
+    comment_id,
+    reason,
+    auth: api.auth,
+  };
+  return api.client.createCommentReport(form);
+}
+
+export async function listCommentReports(
+  api: API
+): Promise<ListCommentReportsResponse> {
+  let form: ListCommentReports = {
+    auth: api.auth,
+  };
+  return api.client.listCommentReports(form);
+}
+
+export function delay(millis = 500) {
   return new Promise(resolve => setTimeout(resolve, millis));
 }
 
@@ -598,12 +758,32 @@ export function wrapper(form: any): string {
   return JSON.stringify(form);
 }
 
-function randomString(length: number): string {
-  var result = '';
-  var characters = 'abcdefghijklmnopqrstuvwxyz0123456789_';
+export function randomString(length: number): string {
+  var result = "";
+  var characters =
+    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
   var charactersLength = characters.length;
   for (var i = 0; i < length; i++) {
     result += characters.charAt(Math.floor(Math.random() * charactersLength));
   }
   return result;
 }
+
+export async function unfollows() {
+  await unfollowRemotes(alpha);
+  await unfollowRemotes(gamma);
+  await unfollowRemotes(delta);
+  await unfollowRemotes(epsilon);
+}
+
+export function getCommentParentId(comment: Comment): number | undefined {
+  let split = comment.path.split(".");
+  // remove the 0
+  split.shift();
+
+  if (split.length > 1) {
+    return Number(split[split.length - 2]);
+  } else {
+    return undefined;
+  }
+}