1 import { readFile } from 'fs'
2 import * as request from 'supertest'
3 import { join, isAbsolute } from 'path'
4 import * as parseTorrent from 'parse-torrent'
6 import { makeGetRequest } from './requests'
7 import { readFilePromise } from './miscs'
8 import { ServerInfo } from './servers'
10 type VideoAttributes = {
21 function getVideoCategories (url: string) {
22 const path = '/api/v1/videos/categories'
24 return makeGetRequest(url, path)
27 function getVideoLicences (url: string) {
28 const path = '/api/v1/videos/licences'
30 return makeGetRequest(url, path)
33 function getVideoLanguages (url: string) {
34 const path = '/api/v1/videos/languages'
36 return makeGetRequest(url, path)
39 function getAllVideosListBy (url: string) {
40 const path = '/api/v1/videos'
44 .query({ sort: 'createdAt' })
46 .query({ count: 10000 })
47 .set('Accept', 'application/json')
49 .expect('Content-Type', /json/)
52 function getVideo (url: string, id: number | string) {
53 const path = '/api/v1/videos/' + id
57 .set('Accept', 'application/json')
59 .expect('Content-Type', /json/)
62 function getVideosList (url: string) {
63 const path = '/api/v1/videos'
67 .query({ sort: 'name' })
68 .set('Accept', 'application/json')
70 .expect('Content-Type', /json/)
73 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
74 const path = '/api/v1/videos'
76 const req = request(url)
78 .query({ start: start })
79 .query({ count: count })
81 if (sort) req.query({ sort })
83 return req.set('Accept', 'application/json')
85 .expect('Content-Type', /json/)
88 function getVideosListSort (url: string, sort: string) {
89 const path = '/api/v1/videos'
93 .query({ sort: sort })
94 .set('Accept', 'application/json')
96 .expect('Content-Type', /json/)
99 function removeVideo (url: string, token: string, id: number, expectedStatus = 204) {
100 const path = '/api/v1/videos'
103 .delete(path + '/' + id)
104 .set('Accept', 'application/json')
105 .set('Authorization', 'Bearer ' + token)
106 .expect(expectedStatus)
109 function searchVideo (url: string, search: string, field?: string) {
110 const path = '/api/v1/videos'
111 const req = request(url)
112 .get(path + '/search/' + search)
113 .set('Accept', 'application/json')
115 if (field) req.query({ field })
117 return req.expect(200)
118 .expect('Content-Type', /json/)
121 function searchVideoWithPagination (url: string, search: string, field: string, start: number, count: number, sort?: string) {
122 const path = '/api/v1/videos'
124 const req = request(url)
125 .get(path + '/search/' + search)
130 if (sort) req.query({ sort })
132 return req.set('Accept', 'application/json')
134 .expect('Content-Type', /json/)
137 function searchVideoWithSort (url: string, search: string, sort: string) {
138 const path = '/api/v1/videos'
141 .get(path + '/search/' + search)
143 .set('Accept', 'application/json')
145 .expect('Content-Type', /json/)
148 async function testVideoImage (url: string, imageName: string, imagePath: string) {
149 // Don't test images if the node env is not set
150 // Because we need a special ffmpeg version for this test
151 if (process.env['NODE_TEST_IMAGE']) {
152 const res = await request(url)
156 const data = await readFilePromise(join(__dirname, '..', 'api', 'fixtures', imageName + '.jpg'))
158 return data.equals(res.body)
160 console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
165 function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 204) {
166 const path = '/api/v1/videos'
168 // Default attributes
170 name: 'my super video',
175 description: 'my super description',
177 fixture: 'video_short.webm'
179 attributes = Object.assign(attributes, videoAttributesArg)
181 const req = request(url)
183 .set('Accept', 'application/json')
184 .set('Authorization', 'Bearer ' + accessToken)
185 .field('name', attributes.name)
186 .field('category', attributes.category.toString())
187 .field('licence', attributes.licence.toString())
188 .field('nsfw', JSON.stringify(attributes.nsfw))
189 .field('description', attributes.description)
191 if (attributes.language !== undefined) {
192 req.field('language', attributes.language.toString())
195 for (let i = 0; i < attributes.tags.length; i++) {
196 req.field('tags[' + i + ']', attributes.tags[i])
200 if (isAbsolute(attributes.fixture)) {
201 filepath = attributes.fixture
203 filepath = join(__dirname, '..', 'api', 'fixtures', attributes.fixture)
206 return req.attach('videofile', filepath)
207 .expect(specialStatus)
210 function updateVideo (url: string, accessToken: string, id: number, attributes: VideoAttributes, specialStatus = 204) {
211 const path = '/api/v1/videos/' + id
214 if (attributes.name) body['name'] = attributes.name
215 if (attributes.category) body['category'] = attributes.category
216 if (attributes.licence) body['licence'] = attributes.licence
217 if (attributes.language) body['language'] = attributes.language
218 if (attributes.nsfw) body['nsfw'] = attributes.nsfw
219 if (attributes.description) body['description'] = attributes.description
220 if (attributes.tags) body['tags'] = attributes.tags
225 .set('Accept', 'application/json')
226 .set('Authorization', 'Bearer ' + accessToken)
227 .expect(specialStatus)
230 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
231 const path = '/api/v1/videos/' + id + '/rate'
235 .set('Accept', 'application/json')
236 .set('Authorization', 'Bearer ' + accessToken)
238 .expect(specialStatus)
241 function parseTorrentVideo (server: ServerInfo, videoUUID: string) {
242 return new Promise<any>((res, rej) => {
243 const torrentPath = join(__dirname, '..', '..', '..', 'test' + server.serverNumber, 'torrents', videoUUID + '.torrent')
244 readFile(torrentPath, (err, data) => {
245 if (err) return rej(err)
247 return res(parseTorrent(data))
252 // ---------------------------------------------------------------------------
261 getVideosListPagination,
265 searchVideoWithPagination,