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'
9 import { getMyUserInformation } from './users'
11 type VideoAttributes = {
23 function getVideoCategories (url: string) {
24 const path = '/api/v1/videos/categories'
26 return makeGetRequest(url, path)
29 function getVideoLicences (url: string) {
30 const path = '/api/v1/videos/licences'
32 return makeGetRequest(url, path)
35 function getVideoLanguages (url: string) {
36 const path = '/api/v1/videos/languages'
38 return makeGetRequest(url, path)
41 function getAllVideosListBy (url: string) {
42 const path = '/api/v1/videos'
46 .query({ sort: 'createdAt' })
48 .query({ count: 10000 })
49 .set('Accept', 'application/json')
51 .expect('Content-Type', /json/)
54 function getVideo (url: string, id: number | string) {
55 const path = '/api/v1/videos/' + id
59 .set('Accept', 'application/json')
61 .expect('Content-Type', /json/)
64 function getVideosList (url: string) {
65 const path = '/api/v1/videos'
69 .query({ sort: 'name' })
70 .set('Accept', 'application/json')
72 .expect('Content-Type', /json/)
75 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
76 const path = '/api/v1/videos'
78 const req = request(url)
80 .query({ start: start })
81 .query({ count: count })
83 if (sort) req.query({ sort })
85 return req.set('Accept', 'application/json')
87 .expect('Content-Type', /json/)
90 function getVideosListSort (url: string, sort: string) {
91 const path = '/api/v1/videos'
95 .query({ sort: sort })
96 .set('Accept', 'application/json')
98 .expect('Content-Type', /json/)
101 function removeVideo (url: string, token: string, id: number, expectedStatus = 204) {
102 const path = '/api/v1/videos'
105 .delete(path + '/' + id)
106 .set('Accept', 'application/json')
107 .set('Authorization', 'Bearer ' + token)
108 .expect(expectedStatus)
111 function searchVideo (url: string, search: string, field?: string) {
112 const path = '/api/v1/videos'
113 const req = request(url)
114 .get(path + '/search/' + search)
115 .set('Accept', 'application/json')
117 if (field) req.query({ field })
119 return req.expect(200)
120 .expect('Content-Type', /json/)
123 function searchVideoWithPagination (url: string, search: string, field: string, start: number, count: number, sort?: string) {
124 const path = '/api/v1/videos'
126 const req = request(url)
127 .get(path + '/search/' + search)
132 if (sort) req.query({ sort })
134 return req.set('Accept', 'application/json')
136 .expect('Content-Type', /json/)
139 function searchVideoWithSort (url: string, search: string, sort: string) {
140 const path = '/api/v1/videos'
143 .get(path + '/search/' + search)
145 .set('Accept', 'application/json')
147 .expect('Content-Type', /json/)
150 async function testVideoImage (url: string, imageName: string, imagePath: string) {
151 // Don't test images if the node env is not set
152 // Because we need a special ffmpeg version for this test
153 if (process.env['NODE_TEST_IMAGE']) {
154 const res = await request(url)
158 const data = await readFilePromise(join(__dirname, '..', 'api', 'fixtures', imageName + '.jpg'))
160 return data.equals(res.body)
162 console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
167 async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 204) {
168 const path = '/api/v1/videos/upload'
169 let defaultChannelId = '1'
172 const res = await getMyUserInformation(url, accessToken)
173 defaultChannelId = res.body.videoChannels[0].id
174 } catch (e) { /* empty */ }
176 // Default attributes
178 name: 'my super video',
182 channelId: defaultChannelId,
184 description: 'my super description',
186 fixture: 'video_short.webm'
188 attributes = Object.assign(attributes, videoAttributesArg)
190 const req = request(url)
192 .set('Accept', 'application/json')
193 .set('Authorization', 'Bearer ' + accessToken)
194 .field('name', attributes.name)
195 .field('category', attributes.category.toString())
196 .field('licence', attributes.licence.toString())
197 .field('nsfw', JSON.stringify(attributes.nsfw))
198 .field('description', attributes.description)
199 .field('channelId', attributes.channelId)
201 if (attributes.language !== undefined) {
202 req.field('language', attributes.language.toString())
205 for (let i = 0; i < attributes.tags.length; i++) {
206 req.field('tags[' + i + ']', attributes.tags[i])
210 if (isAbsolute(attributes.fixture)) {
211 filePath = attributes.fixture
213 filePath = join(__dirname, '..', 'api', 'fixtures', attributes.fixture)
216 return req.attach('videofile', filePath)
217 .expect(specialStatus)
220 function updateVideo (url: string, accessToken: string, id: number, attributes: VideoAttributes, specialStatus = 204) {
221 const path = '/api/v1/videos/' + id
224 if (attributes.name) body['name'] = attributes.name
225 if (attributes.category) body['category'] = attributes.category
226 if (attributes.licence) body['licence'] = attributes.licence
227 if (attributes.language) body['language'] = attributes.language
228 if (attributes.nsfw) body['nsfw'] = attributes.nsfw
229 if (attributes.description) body['description'] = attributes.description
230 if (attributes.tags) body['tags'] = attributes.tags
235 .set('Accept', 'application/json')
236 .set('Authorization', 'Bearer ' + accessToken)
237 .expect(specialStatus)
240 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
241 const path = '/api/v1/videos/' + id + '/rate'
245 .set('Accept', 'application/json')
246 .set('Authorization', 'Bearer ' + accessToken)
248 .expect(specialStatus)
251 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
252 return new Promise<any>((res, rej) => {
253 const torrentName = videoUUID + '-' + resolution + '.torrent'
254 const torrentPath = join(__dirname, '..', '..', '..', 'test' + server.serverNumber, 'torrents', torrentName)
255 readFile(torrentPath, (err, data) => {
256 if (err) return rej(err)
258 return res(parseTorrent(data))
263 // ---------------------------------------------------------------------------
272 getVideosListPagination,
276 searchVideoWithPagination,