]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/utils/users/users.ts
add user account email verificiation (#977)
[github/Chocobozzz/PeerTube.git] / server / tests / utils / users / users.ts
index daf731a14b9e01604b49c8d88a9c520d89ff67c2..cd1b0770149f43ca8b88844031354748b1f2e741 100644 (file)
@@ -1,8 +1,8 @@
-import { isAbsolute, join } from 'path'
 import * as request from 'supertest'
-import { makePostBodyRequest, makeUploadRequest, makePutBodyRequest } from '../'
+import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../'
 
 import { UserRole } from '../../../../shared/index'
+import { NSFWPolicyType } from '../../../../shared/models/videos/nsfw-policy.type'
 
 function createUser (
   url: string,
@@ -10,6 +10,7 @@ function createUser (
   username: string,
   password: string,
   videoQuota = 1000000,
+  videoQuotaDaily = -1,
   role: UserRole = UserRole.USER,
   specialStatus = 200
 ) {
@@ -19,7 +20,8 @@ function createUser (
     password,
     role,
     email: username + '@example.com',
-    videoQuota
+    videoQuota,
+    videoQuotaDaily
   }
 
   return request(url)
@@ -56,6 +58,16 @@ function getMyUserInformation (url: string, accessToken: string, specialStatus =
           .expect('Content-Type', /json/)
 }
 
+function deleteMe (url: string, accessToken: string, specialStatus = 204) {
+  const path = '/api/v1/users/me'
+
+  return request(url)
+    .delete(path)
+    .set('Accept', 'application/json')
+    .set('Authorization', 'Bearer ' + accessToken)
+    .expect(specialStatus)
+}
+
 function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
   const path = '/api/v1/users/me/video-quota-used'
 
@@ -124,23 +136,48 @@ function removeUser (url: string, userId: number | string, accessToken: string,
           .expect(expectedStatus)
 }
 
+function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) {
+  const path = '/api/v1/users'
+  let body: any
+  if (reason) body = { reason }
+
+  return request(url)
+    .post(path + '/' + userId + '/block')
+    .send(body)
+    .set('Accept', 'application/json')
+    .set('Authorization', 'Bearer ' + accessToken)
+    .expect(expectedStatus)
+}
+
+function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
+  const path = '/api/v1/users'
+
+  return request(url)
+    .post(path + '/' + userId + '/unblock')
+    .set('Accept', 'application/json')
+    .set('Authorization', 'Bearer ' + accessToken)
+    .expect(expectedStatus)
+}
+
 function updateMyUser (options: {
   url: string
   accessToken: string,
   newPassword?: string,
-  displayNSFW?: boolean,
+  nsfwPolicy?: NSFWPolicyType,
   email?: string,
   autoPlayVideo?: boolean
+  displayName?: string,
   description?: string
 }) {
   const path = '/api/v1/users/me'
 
   const toSend = {}
   if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
-  if (options.displayNSFW !== undefined && options.displayNSFW !== null) toSend['displayNSFW'] = options.displayNSFW
+  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
 
   return makePutBodyRequest({
     url: options.url,
@@ -157,21 +194,8 @@ function updateMyAvatar (options: {
   fixture: string
 }) {
   const path = '/api/v1/users/me/avatar/pick'
-  let filePath = ''
-  if (isAbsolute(options.fixture)) {
-    filePath = options.fixture
-  } else {
-    filePath = join(__dirname, '..', '..', 'api', 'fixtures', options.fixture)
-  }
 
-  return makeUploadRequest({
-    url: options.url,
-    path,
-    token: options.accessToken,
-    fields: {},
-    attaches: { avatarfile: filePath },
-    statusCodeExpected: 200
-  })
+  return updateAvatarRequest(Object.assign(options, { path }))
 }
 
 function updateUser (options: {
@@ -180,6 +204,7 @@ function updateUser (options: {
   accessToken: string,
   email?: string,
   videoQuota?: number,
+  videoQuotaDaily?: number,
   role?: UserRole
 }) {
   const path = '/api/v1/users/' + options.userId
@@ -187,6 +212,7 @@ function updateUser (options: {
   const toSend = {}
   if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
   if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
+  if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
   if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
 
   return makePutBodyRequest({
@@ -220,6 +246,28 @@ function resetPassword (url: string, userId: number, verificationString: string,
   })
 }
 
+function askSendVerifyEmail (url: string, email: string) {
+  const path = '/api/v1/users/ask-send-verify-email'
+
+  return makePostBodyRequest({
+    url,
+    path,
+    fields: { email },
+    statusCodeExpected: 204
+  })
+}
+
+function verifyEmail (url: string, userId: number, verificationString: string, statusCodeExpected = 204) {
+  const path = '/api/v1/users/' + userId + '/verify-email'
+
+  return makePostBodyRequest({
+    url,
+    path,
+    fields: { verificationString },
+    statusCodeExpected
+  })
+}
+
 // ---------------------------------------------------------------------------
 
 export {
@@ -227,6 +275,7 @@ export {
   registerUser,
   getMyUserInformation,
   getMyUserVideoRating,
+  deleteMe,
   getMyUserVideoQuotaUsed,
   getUsersList,
   getUsersListPaginationAndSort,
@@ -234,7 +283,11 @@ export {
   updateUser,
   updateMyUser,
   getUserInformation,
+  blockUser,
+  unblockUser,
   askResetPassword,
   resetPassword,
-  updateMyAvatar
+  updateMyAvatar,
+  askSendVerifyEmail,
+  verifyEmail
 }