]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/extra-utils/users/users.ts
add blocked filter in users list to filter banned users
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / users / users.ts
index 0f2f0ae157ad4a3412c923365e20f2657279e54a..9f193680d2400f7f8b3f6a8ee5be9ab96f3f4c35 100644 (file)
@@ -1,20 +1,22 @@
 import * as request from 'supertest'
-import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests'
-
-import { UserCreate, UserRole } from '../../index'
-import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type'
-import { ServerInfo, userLogin } from '..'
+import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests'
 import { UserAdminFlag } from '../../models/users/user-flag.model'
 import { UserRegister } from '../../models/users/user-register.model'
+import { UserRole } from '../../models/users/user-role'
+import { ServerInfo } from '../server/servers'
+import { userLogin } from './login'
+import { UserUpdateMe } from '../../models/users'
+import { omit } from 'lodash'
 
-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) {
@@ -72,8 +74,8 @@ function registerUser (url: string, username: string, password: string, specialS
 }
 
 function registerUserWithChannel (options: {
-  url: string,
-  user: { username: string, password: string },
+  url: string
+  user: { username: string, password: string, displayName?: string }
   channel: { name: string, displayName: string }
 }) {
   const path = '/api/v1/users/register'
@@ -84,6 +86,10 @@ function registerUserWithChannel (options: {
     channel: options.channel
   }
 
+  if (options.user.displayName) {
+    Object.assign(body, { displayName: options.user.displayName })
+  }
+
   return makePostBodyRequest({
     url: options.url,
     path,
@@ -124,11 +130,12 @@ 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)
@@ -157,14 +164,23 @@ function getUsersList (url: string, accessToken: string) {
           .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)
@@ -209,44 +225,23 @@ function unblockUser (url: string, userId: number | string, accessToken: string,
     .expect(expectedStatus)
 }
 
-function updateMyUser (options: {
-  url: string
-  accessToken: string
-  currentPassword?: string
-  newPassword?: string
-  nsfwPolicy?: NSFWPolicyType
-  email?: string
-  autoPlayVideo?: boolean
-  displayName?: string
-  description?: string
-  videosHistoryEnabled?: boolean
-}) {
+function updateMyUser (options: { url: string, accessToken: string, statusCodeExpected?: number } & UserUpdateMe) {
   const path = '/api/v1/users/me'
 
-  const toSend = {}
-  if (options.currentPassword !== undefined && options.currentPassword !== null) toSend['currentPassword'] = options.currentPassword
-  if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
-  if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy
-  if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
-  if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
-  if (options.description !== undefined && options.description !== null) toSend['description'] = options.description
-  if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName
-  if (options.videosHistoryEnabled !== undefined && options.videosHistoryEnabled !== null) {
-    toSend['videosHistoryEnabled'] = options.videosHistoryEnabled
-  }
+  const toSend: UserUpdateMe = omit(options, 'url', 'accessToken')
 
   return makePutBodyRequest({
     url: options.url,
     path,
     token: options.accessToken,
     fields: toSend,
-    statusCodeExpected: 204
+    statusCodeExpected: options.statusCodeExpected || 204
   })
 }
 
 function updateMyAvatar (options: {
-  url: string,
-  accessToken: string,
+  url: string
+  accessToken: string
   fixture: string
 }) {
   const path = '/api/v1/users/me/avatar/pick'
@@ -256,14 +251,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
@@ -319,13 +314,16 @@ function askSendVerifyEmail (url: string, email: string) {
   })
 }
 
-function verifyEmail (url: string, userId: number, verificationString: string, statusCodeExpected = 204) {
+function verifyEmail (url: string, userId: number, verificationString: string, isPendingEmail = false, statusCodeExpected = 204) {
   const path = '/api/v1/users/' + userId + '/verify-email'
 
   return makePostBodyRequest({
     url,
     path,
-    fields: { verificationString },
+    fields: {
+      verificationString,
+      isPendingEmail
+    },
     statusCodeExpected
   })
 }