]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/utils/videos/videos.ts
fix typo in CONTRIBUTING.md
[github/Chocobozzz/PeerTube.git] / server / tests / utils / videos / videos.ts
index a42d0f0431642041bd7fdc364ea26ba1c52d89fd..87c385f3877f87109c23e21f187a87fc0c793b4a 100644 (file)
@@ -1,13 +1,14 @@
 /* tslint:disable:no-unused-expression */
 
 import { expect } from 'chai'
-import { existsSync, readFile } from 'fs'
+import { existsSync, readdir, readFile } from 'fs-extra'
 import * as parseTorrent from 'parse-torrent'
 import { extname, join } from 'path'
 import * as request from 'supertest'
 import {
   buildAbsoluteFixturePath,
   getMyUserInformation,
+  immutableAssign,
   makeGetRequest,
   makePutBodyRequest,
   makeUploadRequest,
@@ -16,7 +17,6 @@ import {
   testImage
 } from '../'
 import { VideoDetails, VideoPrivacy } from '../../../../shared/models/videos'
-import { readdirPromise } from '../../../helpers/core-utils'
 import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../initializers'
 import { dateIsValid, webtorrentAdd } from '../index'
 
@@ -133,13 +133,13 @@ function getVideosList (url: string) {
           .expect('Content-Type', /json/)
 }
 
-function getVideosListWithToken (url: string, token: string) {
+function getVideosListWithToken (url: string, token: string, query: { nsfw?: boolean } = {}) {
   const path = '/api/v1/videos'
 
   return request(url)
     .get(path)
     .set('Authorization', 'Bearer ' + token)
-    .query({ sort: 'name' })
+    .query(immutableAssign(query, { sort: 'name' }))
     .set('Accept', 'application/json')
     .expect(200)
     .expect('Content-Type', /json/)
@@ -172,17 +172,25 @@ function getMyVideos (url: string, accessToken: string, start: number, count: nu
     .expect('Content-Type', /json/)
 }
 
-function getAccountVideos (url: string, accessToken: string, accountName: string, start: number, count: number, sort?: string) {
+function getAccountVideos (
+  url: string,
+  accessToken: string,
+  accountName: string,
+  start: number,
+  count: number,
+  sort?: string,
+  query: { nsfw?: boolean } = {}
+) {
   const path = '/api/v1/accounts/' + accountName + '/videos'
 
   return makeGetRequest({
     url,
     path,
-    query: {
+    query: immutableAssign(query, {
       start,
       count,
       sort
-    },
+    }),
     token: accessToken,
     statusCodeExpected: 200
   })
@@ -191,21 +199,22 @@ function getAccountVideos (url: string, accessToken: string, accountName: string
 function getVideoChannelVideos (
   url: string,
   accessToken: string,
-  videoChannelId: number | string,
+  videoChannelName: string,
   start: number,
   count: number,
-  sort?: string
+  sort?: string,
+  query: { nsfw?: boolean } = {}
 ) {
-  const path = '/api/v1/video-channels/' + videoChannelId + '/videos'
+  const path = '/api/v1/video-channels/' + videoChannelName + '/videos'
 
   return makeGetRequest({
     url,
     path,
-    query: {
+    query: immutableAssign(query, {
       start,
       count,
       sort
-    },
+    }),
     token: accessToken,
     statusCodeExpected: 200
   })
@@ -237,77 +246,41 @@ function getVideosListSort (url: string, sort: string) {
           .expect('Content-Type', /json/)
 }
 
-function removeVideo (url: string, token: string, id: number | string, expectedStatus = 204) {
+function getVideosWithFilters (url: string, query: { tagsAllOf: string[], categoryOneOf: number[] | number }) {
   const path = '/api/v1/videos'
 
   return request(url)
-          .delete(path + '/' + id)
-          .set('Accept', 'application/json')
-          .set('Authorization', 'Bearer ' + token)
-          .expect(expectedStatus)
-}
-
-function searchVideo (url: string, search: string) {
-  const path = '/api/v1/search/videos'
-  const req = request(url)
     .get(path)
-    .query({ search })
+    .query(query)
     .set('Accept', 'application/json')
-
-  return req.expect(200)
+    .expect(200)
     .expect('Content-Type', /json/)
 }
 
-function searchVideoWithToken (url: string, search: string, token: string) {
+function removeVideo (url: string, token: string, id: number | string, expectedStatus = 204) {
   const path = '/api/v1/videos'
-  const req = request(url)
-    .get(path + '/search')
-    .set('Authorization', 'Bearer ' + token)
-    .query({ search })
-    .set('Accept', 'application/json')
-
-  return req.expect(200)
-            .expect('Content-Type', /json/)
-}
-
-function searchVideoWithPagination (url: string, search: string, start: number, count: number, sort?: string) {
-  const path = '/api/v1/search/videos'
-
-  const req = request(url)
-                .get(path)
-                .query({ start })
-                .query({ search })
-                .query({ count })
-
-  if (sort) req.query({ sort })
-
-  return req.set('Accept', 'application/json')
-            .expect(200)
-            .expect('Content-Type', /json/)
-}
-
-function searchVideoWithSort (url: string, search: string, sort: string) {
-  const path = '/api/v1/search/videos'
 
   return request(url)
-          .get(path)
-          .query({ search })
-          .query({ sort })
+          .delete(path + '/' + id)
           .set('Accept', 'application/json')
-          .expect(200)
-          .expect('Content-Type', /json/)
+          .set('Authorization', 'Bearer ' + token)
+          .expect(expectedStatus)
 }
 
-async function checkVideoFilesWereRemoved (videoUUID: string, serverNumber: number) {
+async function checkVideoFilesWereRemoved (
+  videoUUID: string,
+  serverNumber: number,
+  directories = [ 'videos', 'thumbnails', 'torrents', 'previews', 'captions' ]
+) {
   const testDirectory = 'test' + serverNumber
 
-  for (const directory of [ 'videos', 'thumbnails', 'torrents', 'previews', 'captions' ]) {
+  for (const directory of directories) {
     const directoryPath = join(root(), testDirectory, directory)
 
     const directoryExists = existsSync(directoryPath)
     expect(directoryExists).to.be.true
 
-    const files = await readdirPromise(directoryPath)
+    const files = await readdir(directoryPath)
     for (const file of files) {
       expect(file).to.not.contain(videoUUID)
     }
@@ -469,18 +442,19 @@ async function completeVideoCheck (
       name: string
       host: string
     }
-    isLocal: boolean,
-    tags: string[],
-    privacy: number,
-    likes?: number,
-    dislikes?: number,
-    duration: number,
+    isLocal: boolean
+    tags: string[]
+    privacy: number
+    likes?: number
+    dislikes?: number
+    duration: number
     channel: {
-      name: string,
+      displayName: string
+      name: string
       description
       isLocal: boolean
     }
-    fixture: string,
+    fixture: string
     files: {
       resolution: number
       size: number
@@ -507,8 +481,8 @@ async function completeVideoCheck (
   expect(video.account.uuid).to.be.a('string')
   expect(video.account.host).to.equal(attributes.account.host)
   expect(video.account.name).to.equal(attributes.account.name)
-  expect(video.channel.displayName).to.equal(attributes.channel.name)
-  expect(video.channel.name).to.have.lengthOf(36)
+  expect(video.channel.displayName).to.equal(attributes.channel.displayName)
+  expect(video.channel.name).to.equal(attributes.channel.name)
   expect(video.likes).to.equal(attributes.likes)
   expect(video.dislikes).to.equal(attributes.dislikes)
   expect(video.isLocal).to.equal(attributes.isLocal)
@@ -528,8 +502,8 @@ async function completeVideoCheck (
   expect(videoDetails.tags).to.deep.equal(attributes.tags)
   expect(videoDetails.account.name).to.equal(attributes.account.name)
   expect(videoDetails.account.host).to.equal(attributes.account.host)
-  expect(videoDetails.channel.displayName).to.equal(attributes.channel.name)
-  expect(videoDetails.channel.name).to.have.lengthOf(36)
+  expect(video.channel.displayName).to.equal(attributes.channel.displayName)
+  expect(video.channel.name).to.equal(attributes.channel.name)
   expect(videoDetails.channel.host).to.equal(attributes.account.host)
   expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
   expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
@@ -553,7 +527,9 @@ async function completeVideoCheck (
 
     const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
     const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
-    expect(file.size).to.be.above(minSize).and.below(maxSize)
+    expect(file.size,
+           'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')')
+      .to.be.above(minSize).and.below(maxSize)
 
     {
       await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
@@ -581,18 +557,15 @@ export {
   getMyVideos,
   getAccountVideos,
   getVideoChannelVideos,
-  searchVideoWithToken,
   getVideo,
   getVideoWithToken,
   getVideosList,
   getVideosListPagination,
   getVideosListSort,
   removeVideo,
-  searchVideo,
-  searchVideoWithPagination,
-  searchVideoWithSort,
   getVideosListWithToken,
   uploadVideo,
+  getVideosWithFilters,
   updateVideo,
   rateVideo,
   viewVideo,