]> Untitled Git - lemmy.git/blobdiff - api_tests/src/shared.ts
Revert "Add pending, and change use specific API response for FollowCommunity…" ...
[lemmy.git] / api_tests / src / shared.ts
index c11fa3b0e96d5055ef2b25552c58ded649aa3ea5..d5be61ac3f66e4164fd05cd780a5a01838170378 100644 (file)
@@ -12,7 +12,6 @@ import {
   SearchResponse,
   FollowCommunity,
   CommunityResponse,
-  GetFollowedCommunitiesResponse,
   GetPostResponse,
   Register,
   Comment,
@@ -30,7 +29,6 @@ import {
   CreatePostLike,
   EditPrivateMessage,
   DeletePrivateMessage,
-  GetFollowedCommunities,
   GetPrivateMessages,
   GetSite,
   GetPost,
@@ -49,6 +47,18 @@ import {
   BanFromCommunityResponse,
   Post,
   CreatePrivateMessage,
+  ResolveObjectResponse,
+  ResolveObject,
+  CreatePostReport,
+  ListPostReports,
+  PostReportResponse,
+  ListPostReportsResponse,
+  CreateCommentReport,
+  CommentReportResponse,
+  ListCommentReports,
+  ListCommentReportsResponse,
+  DeleteAccount,
+  DeleteAccountResponse
 } from 'lemmy-js-client';
 
 export interface API {
@@ -57,53 +67,55 @@ export interface API {
 }
 
 export let alpha: API = {
-  client: new LemmyHttp('http://localhost:8541/api/v2'),
+  client: new LemmyHttp('http://127.0.0.1:8541'),
 };
 
 export let beta: API = {
-  client: new LemmyHttp('http://localhost:8551/api/v2'),
+  client: new LemmyHttp('http://127.0.0.1:8551'),
 };
 
 export let gamma: API = {
-  client: new LemmyHttp('http://localhost:8561/api/v2'),
+  client: new LemmyHttp('http://127.0.0.1:8561'),
 };
 
 export let delta: API = {
-  client: new LemmyHttp('http://localhost:8571/api/v2'),
+  client: new LemmyHttp('http://127.0.0.1:8571'),
 };
 
 export let epsilon: API = {
-  client: new LemmyHttp('http://localhost:8581/api/v2'),
+  client: new LemmyHttp('http://127.0.0.1:8581'),
 };
 
+const password = 'lemmylemmy'
+
 export async function setupLogins() {
   let formAlpha: Login = {
     username_or_email: 'lemmy_alpha',
-    password: 'lemmy',
+    password,
   };
   let resAlpha = alpha.client.login(formAlpha);
 
   let formBeta = {
     username_or_email: 'lemmy_beta',
-    password: 'lemmy',
+    password,
   };
   let resBeta = beta.client.login(formBeta);
 
   let formGamma = {
     username_or_email: 'lemmy_gamma',
-    password: 'lemmy',
+    password,
   };
   let resGamma = gamma.client.login(formGamma);
 
   let formDelta = {
     username_or_email: 'lemmy_delta',
-    password: 'lemmy',
+    password,
   };
   let resDelta = delta.client.login(formDelta);
 
   let formEpsilon = {
     username_or_email: 'lemmy_epsilon',
-    password: 'lemmy',
+    password,
   };
   let resEpsilon = epsilon.client.login(formEpsilon);
 
@@ -120,6 +132,13 @@ export async function setupLogins() {
   gamma.auth = res[2].jwt;
   delta.auth = res[3].jwt;
   epsilon.auth = res[4].jwt;
+
+  // regstration applications are now enabled by default, need to disable them
+  await alpha.client.editSite({ require_application: false, auth: alpha.auth});
+  await beta.client.editSite({ require_application: false, auth: beta.auth});
+  await gamma.client.editSite({ require_application: false, auth: gamma.auth});
+  await delta.client.editSite({ require_application: false, auth: delta.auth});
+  await epsilon.client.editSite({ require_application: false, auth: epsilon.auth});
 }
 
 export async function createPost(
@@ -203,16 +222,14 @@ export async function lockPost(
   return api.client.lockPost(form);
 }
 
-export async function searchPost(
+export async function resolvePost(
   api: API,
   post: Post
-): Promise<SearchResponse> {
-  let form: Search = {
+): Promise<ResolveObjectResponse> {
+  let form: ResolveObject = {
     q: post.ap_id,
-    type_: SearchType.Posts,
-    sort: SortType.TopAll,
   };
-  return api.client.search(form);
+  return api.client.resolveObject(form);
 }
 
 export async function searchPostLocal(
@@ -237,69 +254,57 @@ export async function getPost(
   return api.client.getPost(form);
 }
 
-export async function searchComment(
+export async function resolveComment(
   api: API,
   comment: Comment
-): Promise<SearchResponse> {
-  let form: Search = {
+): Promise<ResolveObjectResponse> {
+  let form: ResolveObject = {
     q: comment.ap_id,
-    type_: SearchType.Comments,
-    sort: SortType.TopAll,
   };
-  return api.client.search(form);
+  return api.client.resolveObject(form);
 }
 
-export async function searchForBetaCommunity(
+export async function resolveBetaCommunity(
   api: API
-): Promise<SearchResponse> {
-  // Make sure lemmy-beta/c/main is cached on lemmy_alpha
+): Promise<ResolveObjectResponse> {
   // Use short-hand search url
-  let form: Search = {
+  let form: ResolveObject = {
     q: '!main@lemmy-beta:8551',
-    type_: SearchType.Communities,
-    sort: SortType.TopAll,
   };
-  return api.client.search(form);
+  return api.client.resolveObject(form);
 }
 
-export async function searchForCommunity(
+export async function resolveCommunity(
   api: API,
   q: string
-): Promise<SearchResponse> {
-  // Use short-hand search url
-  let form: Search = {
+): Promise<ResolveObjectResponse> {
+  let form: ResolveObject = {
     q,
-    type_: SearchType.Communities,
-    sort: SortType.TopAll,
   };
-  return api.client.search(form);
+  return api.client.resolveObject(form);
 }
 
-export async function searchForUser(
+export async function resolvePerson(
   api: API,
   apShortname: string
-): Promise<SearchResponse> {
-  // Make sure lemmy-beta/c/main is cached on lemmy_alpha
-  // Use short-hand search url
-  let form: Search = {
+): Promise<ResolveObjectResponse> {
+  let form: ResolveObject = {
     q: apShortname,
-    type_: SearchType.Users,
-    sort: SortType.TopAll,
   };
-  return api.client.search(form);
+  return api.client.resolveObject(form);
 }
 
 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
-  // Use short-hand search url
   let form: BanPerson = {
     person_id,
     ban,
-    remove_data: false,
+    remove_data,
     auth: api.auth,
   };
   return api.client.banPerson(form);
@@ -309,14 +314,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
-  // Use short-hand search url
   let form: BanFromCommunity = {
     person_id,
     community_id,
-    remove_data: false,
+    remove_data,
     ban,
     auth: api.auth,
   };
@@ -336,15 +340,6 @@ export async function followCommunity(
   return api.client.followCommunity(form);
 }
 
-export async function checkFollowedCommunities(
-  api: API
-): Promise<GetFollowedCommunitiesResponse> {
-  let form: GetFollowedCommunities = {
-    auth: api.auth,
-  };
-  return api.client.getFollowedCommunities(form);
-}
-
 export async function likePost(
   api: API,
   score: number,
@@ -440,14 +435,10 @@ export async function createCommunity(
   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 form: CreateCommunity = {
     name: name_,
     title: name_,
     description,
-    icon,
-    banner,
     nsfw: false,
     auth: api.auth,
   };
@@ -535,16 +526,15 @@ export async function registerUser(
 ): Promise<LoginResponse> {
   let form: Register = {
     username,
-    password: 'test',
-    password_verify: 'test',
+    password,
+    password_verify: password,
     show_nsfw: true,
   };
   return api.client.register(form);
 }
 
 export async function saveUserSettingsBio(
-  api: API,
-  auth: string
+  api: API
 ): Promise<LoginResponse> {
   let form: SaveUserSettings = {
     show_nsfw: true,
@@ -555,7 +545,7 @@ export async function saveUserSettingsBio(
     show_avatars: true,
     send_notifications_to_email: false,
     bio: 'a changed bio',
-    auth,
+    auth: api.auth,
   };
   return saveUserSettings(api, form);
 }
@@ -567,12 +557,21 @@ export async function saveUserSettings(
   return api.client.saveUserSettings(form);
 }
 
-export async function getSite(
+export async function deleteUser(
   api: API,
-  auth: string
+): 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,
+    auth: api.auth,
   };
   return api.client.getSite(form);
 }
@@ -590,29 +589,67 @@ export async function listPrivateMessages(
 
 export async function unfollowRemotes(
   api: API
-): Promise<GetFollowedCommunitiesResponse> {
+): Promise<GetSiteResponse> {
   // Unfollow all remote communities
-  let followed = await checkFollowedCommunities(api);
-  let remoteFollowed = followed.communities.filter(
+  let site = await getSite(api);
+  let remoteFollowed = site.my_user.follows.filter(
     c => c.community.local == false
   );
   for (let cu of remoteFollowed) {
     await followCommunity(api, false, cu.community.id);
   }
-  let followed2 = await checkFollowedCommunities(api);
-  return followed2;
+  let siteRes = await getSite(api);
+  return siteRes;
 }
 
 export async function followBeta(api: API): Promise<CommunityResponse> {
-  // Cache it
-  let search = await searchForBetaCommunity(api);
-  let com = search.communities.find(c => c.community.local == false);
-  if (com) {
-    let follow = await followCommunity(api, true, com.community.id);
+  let betaCommunity = (await resolveBetaCommunity(api)).community;
+  if (betaCommunity) {
+    let follow = await followCommunity(api, true, betaCommunity.community.id);
     return follow;
   }
 }
 
+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: number = 500) {
   return new Promise(resolve => setTimeout(resolve, millis));
 }
@@ -625,12 +662,19 @@ export function wrapper(form: any): string {
   return JSON.stringify(form);
 }
 
-function randomString(length: number): string {
+export function randomString(length: number): string {
   var result = '';
-  var characters = 'abcdefghijklmnopqrstuvwxyz0123456789_';
+  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);
+}