]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/utils/requests/requests.ts
Add ability for users to block an account/instance on server side
[github/Chocobozzz/PeerTube.git] / server / tests / utils / requests / requests.ts
index 52b7a4c29ca73e9d671010798666e5185abb5788..5796540f7c3e45b4d54d44fee95c099cd30ecdff 100644 (file)
@@ -1,26 +1,64 @@
 import * as request from 'supertest'
+import { buildAbsoluteFixturePath } from '../miscs/miscs'
+import { isAbsolute, join } from 'path'
 
-function makeGetRequest (url: string, path: string) {
-  return request(url)
-    .get(path)
+function makeGetRequest (options: {
+  url: string,
+  path: string,
+  query?: any,
+  token?: string,
+  statusCodeExpected?: number,
+  contentType?: string
+}) {
+  if (!options.statusCodeExpected) options.statusCodeExpected = 400
+  if (options.contentType === undefined) options.contentType = 'application/json'
+
+  const req = request(options.url)
+    .get(options.path)
+
+  if (options.contentType) req.set('Accept', options.contentType)
+  if (options.token) req.set('Authorization', 'Bearer ' + options.token)
+  if (options.query) req.query(options.query)
+
+  return req.expect(options.statusCodeExpected)
+}
+
+function makeDeleteRequest (options: {
+  url: string,
+  path: string,
+  token?: string,
+  statusCodeExpected?: number
+}) {
+  if (!options.statusCodeExpected) options.statusCodeExpected = 400
+
+  const req = request(options.url)
+    .delete(options.path)
     .set('Accept', 'application/json')
-    .expect(200)
-    .expect('Content-Type', /json/)
+
+  if (options.token) req.set('Authorization', 'Bearer ' + options.token)
+
+  return req.expect(options.statusCodeExpected)
 }
 
-function makePostUploadRequest (options: {
+function makeUploadRequest (options: {
   url: string,
+  method?: 'POST' | 'PUT',
   path: string,
-  token: string,
+  token?: string,
   fields: { [ fieldName: string ]: any },
-  attaches: { [ attachName: string ]: any },
+  attaches: { [ attachName: string ]: any | any[] },
   statusCodeExpected?: number
 }) {
   if (!options.statusCodeExpected) options.statusCodeExpected = 400
 
-  const req = request(options.url)
-                .post(options.path)
-                .set('Accept', 'application/json')
+  let req: request.Test
+  if (options.method === 'PUT') {
+    req = request(options.url).put(options.path)
+  } else {
+    req = request(options.url).post(options.path)
+  }
+
+  req.set('Accept', 'application/json')
 
   if (options.token) req.set('Authorization', 'Bearer ' + options.token)
 
@@ -38,7 +76,11 @@ function makePostUploadRequest (options: {
 
   Object.keys(options.attaches).forEach(attach => {
     const value = options.attaches[attach]
-    req.attach(attach, value)
+    if (Array.isArray(value)) {
+      req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
+    } else {
+      req.attach(attach, buildAbsoluteFixturePath(value))
+    }
   })
 
   return req.expect(options.statusCodeExpected)
@@ -48,9 +90,10 @@ function makePostBodyRequest (options: {
   url: string,
   path: string,
   token?: string,
-  fields: { [ fieldName: string ]: any },
+  fields?: { [ fieldName: string ]: any },
   statusCodeExpected?: number
 }) {
+  if (!options.fields) options.fields = {}
   if (!options.statusCodeExpected) options.statusCodeExpected = 400
 
   const req = request(options.url)
@@ -66,7 +109,7 @@ function makePostBodyRequest (options: {
 function makePutBodyRequest (options: {
   url: string,
   path: string,
-  token: string,
+  token?: string,
   fields: { [ fieldName: string ]: any },
   statusCodeExpected?: number
 }) {
@@ -82,11 +125,44 @@ function makePutBodyRequest (options: {
             .expect(options.statusCodeExpected)
 }
 
+function makeHTMLRequest (url: string, path: string) {
+  return request(url)
+    .get(path)
+    .set('Accept', 'text/html')
+    .expect(200)
+}
+
+function updateAvatarRequest (options: {
+  url: string,
+  path: string,
+  accessToken: string,
+  fixture: string
+}) {
+  let filePath = ''
+  if (isAbsolute(options.fixture)) {
+    filePath = options.fixture
+  } else {
+    filePath = join(__dirname, '..', '..', 'fixtures', options.fixture)
+  }
+
+  return makeUploadRequest({
+    url: options.url,
+    path: options.path,
+    token: options.accessToken,
+    fields: {},
+    attaches: { avatarfile: filePath },
+    statusCodeExpected: 200
+  })
+}
+
 // ---------------------------------------------------------------------------
 
 export {
+  makeHTMLRequest,
   makeGetRequest,
-  makePostUploadRequest,
+  makeUploadRequest,
   makePostBodyRequest,
-  makePutBodyRequest
+  makePutBodyRequest,
+  makeDeleteRequest,
+  updateAvatarRequest
 }