]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/extra-utils/users/users.ts
move from trending routes to alg param
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / users / users.ts
index 9959fd0745b0625b4b72ab0cd9a075a625539f33..c683dcdd12cedfabada121e1863ddb12900b683c 100644 (file)
@@ -1,21 +1,23 @@
+import { omit } from 'lodash'
 import * as request from 'supertest'
-import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests'
+import { UserUpdateMe } from '../../models/users'
 import { UserAdminFlag } from '../../models/users/user-flag.model'
 import { UserRegister } from '../../models/users/user-register.model'
 import { UserRole } from '../../models/users/user-role'
+import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests'
 import { ServerInfo } from '../server/servers'
 import { userLogin } from './login'
-import { UserUpdateMe } from '../../models/users'
-import { omit } from 'lodash'
+import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
 
-type CreateUserArgs = { url: string,
-  accessToken: string,
-  username: string,
-  password: string,
-  videoQuota?: number,
-  videoQuotaDaily?: number,
-  role?: UserRole,
-  adminFlags?: UserAdminFlag,
+type CreateUserArgs = {
+  url: string
+  accessToken: string
+  username: string
+  password: string
+  videoQuota?: number
+  videoQuotaDaily?: number
+  role?: UserRole
+  adminFlags?: UserAdminFlag
   specialStatus?: number
 }
 function createUser (parameters: CreateUserArgs) {
@@ -28,7 +30,7 @@ function createUser (parameters: CreateUserArgs) {
     videoQuota = 1000000,
     videoQuotaDaily = -1,
     role = UserRole.USER,
-    specialStatus = 200
+    specialStatus = HttpStatusCode.OK_200
   } = parameters
 
   const path = '/api/v1/users'
@@ -57,7 +59,7 @@ async function generateUserAccessToken (server: ServerInfo, username: string) {
   return userLogin(server, { username, password })
 }
 
-function registerUser (url: string, username: string, password: string, specialStatus = 204) {
+function registerUser (url: string, username: string, password: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
   const path = '/api/v1/users/register'
   const body = {
     username,
@@ -73,8 +75,8 @@ function registerUser (url: string, username: string, password: string, specialS
 }
 
 function registerUserWithChannel (options: {
-  url: string,
-  user: { username: string, password: string, displayName?: string },
+  url: string
+  user: { username: string, password: string, displayName?: string }
   channel: { name: string, displayName: string }
 }) {
   const path = '/api/v1/users/register'
@@ -93,11 +95,11 @@ function registerUserWithChannel (options: {
     url: options.url,
     path,
     fields: body,
-    statusCodeExpected: 204
+    statusCodeExpected: HttpStatusCode.NO_CONTENT_204
   })
 }
 
-function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
+function getMyUserInformation (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) {
   const path = '/api/v1/users/me'
 
   return request(url)
@@ -108,7 +110,29 @@ function getMyUserInformation (url: string, accessToken: string, specialStatus =
           .expect('Content-Type', /json/)
 }
 
-function deleteMe (url: string, accessToken: string, specialStatus = 204) {
+function getUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) {
+  const path = '/api/v1/users/scoped-tokens'
+
+  return makeGetRequest({
+    url,
+    path,
+    token,
+    statusCodeExpected
+  })
+}
+
+function renewUserScopedTokens (url: string, token: string, statusCodeExpected = HttpStatusCode.OK_200) {
+  const path = '/api/v1/users/scoped-tokens'
+
+  return makePostBodyRequest({
+    url,
+    path,
+    token,
+    statusCodeExpected
+  })
+}
+
+function deleteMe (url: string, accessToken: string, specialStatus = HttpStatusCode.NO_CONTENT_204) {
   const path = '/api/v1/users/me'
 
   return request(url)
@@ -118,7 +142,7 @@ function deleteMe (url: string, accessToken: string, specialStatus = 204) {
     .expect(specialStatus)
 }
 
-function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
+function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = HttpStatusCode.OK_200) {
   const path = '/api/v1/users/me/video-quota-used'
 
   return request(url)
@@ -129,18 +153,19 @@ function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatu
           .expect('Content-Type', /json/)
 }
 
-function getUserInformation (url: string, accessToken: string, userId: number) {
+function getUserInformation (url: string, accessToken: string, userId: number, withStats = false) {
   const path = '/api/v1/users/' + userId
 
   return request(url)
     .get(path)
+    .query({ withStats })
     .set('Accept', 'application/json')
     .set('Authorization', 'Bearer ' + accessToken)
-    .expect(200)
+    .expect(HttpStatusCode.OK_200)
     .expect('Content-Type', /json/)
 }
 
-function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
+function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = HttpStatusCode.OK_200) {
   const path = '/api/v1/users/me/videos/' + videoId + '/rating'
 
   return request(url)
@@ -158,18 +183,27 @@ function getUsersList (url: string, accessToken: string) {
           .get(path)
           .set('Accept', 'application/json')
           .set('Authorization', 'Bearer ' + accessToken)
-          .expect(200)
+          .expect(HttpStatusCode.OK_200)
           .expect('Content-Type', /json/)
 }
 
-function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) {
+function getUsersListPaginationAndSort (
+  url: string,
+  accessToken: string,
+  start: number,
+  count: number,
+  sort: string,
+  search?: string,
+  blocked?: boolean
+) {
   const path = '/api/v1/users'
 
   const query = {
     start,
     count,
     sort,
-    search
+    search,
+    blocked
   }
 
   return request(url)
@@ -177,11 +211,11 @@ function getUsersListPaginationAndSort (url: string, accessToken: string, start:
           .query(query)
           .set('Accept', 'application/json')
           .set('Authorization', 'Bearer ' + accessToken)
-          .expect(200)
+          .expect(HttpStatusCode.OK_200)
           .expect('Content-Type', /json/)
 }
 
-function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
+function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
   const path = '/api/v1/users'
 
   return request(url)
@@ -191,7 +225,13 @@ function removeUser (url: string, userId: number | string, accessToken: string,
           .expect(expectedStatus)
 }
 
-function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
+function blockUser (
+  url: string,
+  userId: number | string,
+  accessToken: string,
+  expectedStatus = HttpStatusCode.NO_CONTENT_204,
+  reason?: string
+) {
   const path = '/api/v1/users'
   let body: any
   if (reason) body = { reason }
@@ -204,7 +244,7 @@ function blockUser (url: string, userId: number | string, accessToken: string, e
     .expect(expectedStatus)
 }
 
-function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
+function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
   const path = '/api/v1/users'
 
   return request(url)
@@ -214,7 +254,7 @@ function unblockUser (url: string, userId: number | string, accessToken: string,
     .expect(expectedStatus)
 }
 
-function updateMyUser (options: { url: string, accessToken: string } & UserUpdateMe) {
+function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: HttpStatusCode } & UserUpdateMe) {
   const path = '/api/v1/users/me'
 
   const toSend: UserUpdateMe = omit(options, 'url', 'accessToken')
@@ -224,13 +264,13 @@ function updateMyUser (options: { url: string, accessToken: string } & UserUpdat
     path,
     token: options.accessToken,
     fields: toSend,
-    statusCodeExpected: 204
+    statusCodeExpected: options.statusCodeExpected || HttpStatusCode.NO_CONTENT_204
   })
 }
 
 function updateMyAvatar (options: {
-  url: string,
-  accessToken: string,
+  url: string
+  accessToken: string
   fixture: string
 }) {
   const path = '/api/v1/users/me/avatar/pick'
@@ -240,14 +280,14 @@ function updateMyAvatar (options: {
 
 function updateUser (options: {
   url: string
-  userId: number,
-  accessToken: string,
-  email?: string,
-  emailVerified?: boolean,
-  videoQuota?: number,
-  videoQuotaDaily?: number,
-  password?: string,
-  adminFlags?: UserAdminFlag,
+  userId: number
+  accessToken: string
+  email?: string
+  emailVerified?: boolean
+  videoQuota?: number
+  videoQuotaDaily?: number
+  password?: string
+  adminFlags?: UserAdminFlag
   role?: UserRole
 }) {
   const path = '/api/v1/users/' + options.userId
@@ -266,7 +306,7 @@ function updateUser (options: {
     path,
     token: options.accessToken,
     fields: toSend,
-    statusCodeExpected: 204
+    statusCodeExpected: HttpStatusCode.NO_CONTENT_204
   })
 }
 
@@ -277,11 +317,17 @@ function askResetPassword (url: string, email: string) {
     url,
     path,
     fields: { email },
-    statusCodeExpected: 204
+    statusCodeExpected: HttpStatusCode.NO_CONTENT_204
   })
 }
 
-function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
+function resetPassword (
+  url: string,
+  userId: number,
+  verificationString: string,
+  password: string,
+  statusCodeExpected = HttpStatusCode.NO_CONTENT_204
+) {
   const path = '/api/v1/users/' + userId + '/reset-password'
 
   return makePostBodyRequest({
@@ -299,11 +345,17 @@ function askSendVerifyEmail (url: string, email: string) {
     url,
     path,
     fields: { email },
-    statusCodeExpected: 204
+    statusCodeExpected: HttpStatusCode.NO_CONTENT_204
   })
 }
 
-function verifyEmail (url: string, userId: number, verificationString: string, isPendingEmail = false, statusCodeExpected = 204) {
+function verifyEmail (
+  url: string,
+  userId: number,
+  verificationString: string,
+  isPendingEmail = false,
+  statusCodeExpected = HttpStatusCode.NO_CONTENT_204
+) {
   const path = '/api/v1/users/' + userId + '/verify-email'
 
   return makePostBodyRequest({
@@ -337,8 +389,10 @@ export {
   unblockUser,
   askResetPassword,
   resetPassword,
+  renewUserScopedTokens,
   updateMyAvatar,
   askSendVerifyEmail,
   generateUserAccessToken,
-  verifyEmail
+  verifyEmail,
+  getUserScopedTokens
 }