]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/utils/users/users.ts
Add ability to set video thumbnail/preview
[github/Chocobozzz/PeerTube.git] / server / tests / utils / users / users.ts
index bd8d7ab04cbefa26b42b7894f8f87a4dff8ddcb2..3c9d4624642b2154186b90183aa91d74a41285c9 100644 (file)
@@ -1,4 +1,6 @@
+import { isAbsolute, join } from 'path'
 import * as request from 'supertest'
+import { makePostBodyRequest, makeUploadRequest, makePutBodyRequest } from '../'
 
 import { UserRole } from '../../../../shared/index'
 
@@ -9,7 +11,7 @@ function createUser (
   password: string,
   videoQuota = 1000000,
   role: UserRole = UserRole.USER,
-  specialStatus = 204
+  specialStatus = 200
 ) {
   const path = '/api/v1/users'
   const body = {
@@ -43,14 +45,25 @@ function registerUser (url: string, username: string, password: string, specialS
           .expect(specialStatus)
 }
 
-function getMyUserInformation (url: string, accessToken: string) {
+function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) {
   const path = '/api/v1/users/me'
 
   return request(url)
           .get(path)
           .set('Accept', 'application/json')
           .set('Authorization', 'Bearer ' + accessToken)
-          .expect(200)
+          .expect(specialStatus)
+          .expect('Content-Type', /json/)
+}
+
+function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) {
+  const path = '/api/v1/users/me/video-quota-used'
+
+  return request(url)
+          .get(path)
+          .set('Accept', 'application/json')
+          .set('Authorization', 'Bearer ' + accessToken)
+          .expect(specialStatus)
           .expect('Content-Type', /json/)
 }
 
@@ -65,14 +78,14 @@ function getUserInformation (url: string, accessToken: string, userId: number) {
     .expect('Content-Type', /json/)
 }
 
-function getUserVideoRating (url: string, accessToken: string, videoId: number) {
+function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) {
   const path = '/api/v1/users/me/videos/' + videoId + '/rating'
 
   return request(url)
           .get(path)
           .set('Accept', 'application/json')
           .set('Authorization', 'Bearer ' + accessToken)
-          .expect(200)
+          .expect(specialStatus)
           .expect('Content-Type', /json/)
 }
 
@@ -101,7 +114,7 @@ function getUsersListPaginationAndSort (url: string, accessToken: string, start:
           .expect('Content-Type', /json/)
 }
 
-function removeUser (url: string, userId: number, accessToken: string, expectedStatus = 204) {
+function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) {
   const path = '/api/v1/users'
 
   return request(url)
@@ -111,38 +124,98 @@ function removeUser (url: string, userId: number, accessToken: string, expectedS
           .expect(expectedStatus)
 }
 
-function updateMyUser (url: string, accessToken: string, newPassword: string, displayNSFW?: boolean,
-  email?: string, autoPlayVideo?: boolean) {
+function updateMyUser (options: {
+  url: string
+  accessToken: string,
+  newPassword?: string,
+  displayNSFW?: boolean,
+  email?: string,
+  autoPlayVideo?: boolean
+}) {
   const path = '/api/v1/users/me'
 
   const toSend = {}
-  if (newPassword !== undefined && newPassword !== null) toSend['password'] = newPassword
-  if (displayNSFW !== undefined && displayNSFW !== null) toSend['displayNSFW'] = displayNSFW
-  if (autoPlayVideo !== undefined && autoPlayVideo !== null) toSend['autoPlayVideo'] = autoPlayVideo
-  if (email !== undefined && email !== null) toSend['email'] = email
+  if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword
+  if (options.displayNSFW !== undefined && options.displayNSFW !== null) toSend['displayNSFW'] = options.displayNSFW
+  if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo
+  if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
+
+  return makePutBodyRequest({
+    url: options.url,
+    path,
+    token: options.accessToken,
+    fields: toSend,
+    statusCodeExpected: 204
+  })
+}
 
-  return request(url)
-    .put(path)
-    .set('Accept', 'application/json')
-    .set('Authorization', 'Bearer ' + accessToken)
-    .send(toSend)
-    .expect(204)
+function updateMyAvatar (options: {
+  url: string,
+  accessToken: string,
+  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
+  })
 }
 
-function updateUser (url: string, userId: number, accessToken: string, email: string, videoQuota: number, role: UserRole) {
-  const path = '/api/v1/users/' + userId
+function updateUser (options: {
+  url: string
+  userId: number,
+  accessToken: string,
+  email?: string,
+  videoQuota?: number,
+  role?: UserRole
+}) {
+  const path = '/api/v1/users/' + options.userId
 
   const toSend = {}
-  if (email !== undefined && email !== null) toSend['email'] = email
-  if (videoQuota !== undefined && videoQuota !== null) toSend['videoQuota'] = videoQuota
-  if (role !== undefined && role !== null) toSend['role'] = role
+  if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
+  if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
+  if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
+
+  return makePutBodyRequest({
+    url: options.url,
+    path,
+    token: options.accessToken,
+    fields: toSend,
+    statusCodeExpected: 204
+  })
+}
 
-  return request(url)
-          .put(path)
-          .set('Accept', 'application/json')
-          .set('Authorization', 'Bearer ' + accessToken)
-          .send(toSend)
-          .expect(204)
+function askResetPassword (url: string, email: string) {
+  const path = '/api/v1/users/ask-reset-password'
+
+  return makePostBodyRequest({
+    url,
+    path,
+    fields: { email },
+    statusCodeExpected: 204
+  })
+}
+
+function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) {
+  const path = '/api/v1/users/' + userId + '/reset-password'
+
+  return makePostBodyRequest({
+    url,
+    path,
+    fields: { password, verificationString },
+    statusCodeExpected
+  })
 }
 
 // ---------------------------------------------------------------------------
@@ -151,11 +224,15 @@ export {
   createUser,
   registerUser,
   getMyUserInformation,
-  getUserVideoRating,
+  getMyUserVideoRating,
+  getMyUserVideoQuotaUsed,
   getUsersList,
   getUsersListPaginationAndSort,
   removeUser,
   updateUser,
   updateMyUser,
-  getUserInformation
+  getUserInformation,
+  askResetPassword,
+  resetPassword,
+  updateMyAvatar
 }