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'
10 import { VideoPrivacy } from '../../../shared'
12 type VideoAttributes = {
21 privacy?: VideoPrivacy
25 function getVideoCategories (url: string) {
26 const path = '/api/v1/videos/categories'
28 return makeGetRequest(url, path)
31 function getVideoLicences (url: string) {
32 const path = '/api/v1/videos/licences'
34 return makeGetRequest(url, path)
37 function getVideoLanguages (url: string) {
38 const path = '/api/v1/videos/languages'
40 return makeGetRequest(url, path)
43 function getVideoPrivacies (url: string) {
44 const path = '/api/v1/videos/privacies'
46 return makeGetRequest(url, path)
49 function getVideo (url: string, id: number | string, expectedStatus = 200) {
50 const path = '/api/v1/videos/' + id
54 .set('Accept', 'application/json')
55 .expect(expectedStatus)
58 function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = 200) {
59 const path = '/api/v1/videos/' + id
63 .set('Authorization', 'Bearer ' + token)
64 .set('Accept', 'application/json')
65 .expect(expectedStatus)
68 function getVideoDescription (url: string, descriptionPath: string) {
71 .set('Accept', 'application/json')
73 .expect('Content-Type', /json/)
76 function getVideosList (url: string) {
77 const path = '/api/v1/videos'
81 .query({ sort: 'name' })
82 .set('Accept', 'application/json')
84 .expect('Content-Type', /json/)
87 function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string) {
88 const path = '/api/v1/users/me/videos'
90 const req = request(url)
92 .query({ start: start })
93 .query({ count: count })
95 if (sort) req.query({ sort })
97 return req.set('Accept', 'application/json')
98 .set('Authorization', 'Bearer ' + accessToken)
100 .expect('Content-Type', /json/)
103 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
104 const path = '/api/v1/videos'
106 const req = request(url)
108 .query({ start: start })
109 .query({ count: count })
111 if (sort) req.query({ sort })
113 return req.set('Accept', 'application/json')
115 .expect('Content-Type', /json/)
118 function getVideosListSort (url: string, sort: string) {
119 const path = '/api/v1/videos'
123 .query({ sort: sort })
124 .set('Accept', 'application/json')
126 .expect('Content-Type', /json/)
129 function removeVideo (url: string, token: string, id: number, expectedStatus = 204) {
130 const path = '/api/v1/videos'
133 .delete(path + '/' + id)
134 .set('Accept', 'application/json')
135 .set('Authorization', 'Bearer ' + token)
136 .expect(expectedStatus)
139 function searchVideo (url: string, search: string, field?: string) {
140 const path = '/api/v1/videos'
141 const req = request(url)
142 .get(path + '/search/' + search)
143 .set('Accept', 'application/json')
145 if (field) req.query({ field })
147 return req.expect(200)
148 .expect('Content-Type', /json/)
151 function searchVideoWithPagination (url: string, search: string, field: string, start: number, count: number, sort?: string) {
152 const path = '/api/v1/videos'
154 const req = request(url)
155 .get(path + '/search/' + search)
160 if (sort) req.query({ sort })
162 return req.set('Accept', 'application/json')
164 .expect('Content-Type', /json/)
167 function searchVideoWithSort (url: string, search: string, sort: string) {
168 const path = '/api/v1/videos'
171 .get(path + '/search/' + search)
173 .set('Accept', 'application/json')
175 .expect('Content-Type', /json/)
178 async function testVideoImage (url: string, imageName: string, imagePath: string) {
179 // Don't test images if the node env is not set
180 // Because we need a special ffmpeg version for this test
181 if (process.env['NODE_TEST_IMAGE']) {
182 const res = await request(url)
186 const data = await readFilePromise(join(__dirname, '..', 'api', 'fixtures', imageName + '.jpg'))
188 return data.equals(res.body)
190 console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
195 async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 204) {
196 const path = '/api/v1/videos/upload'
197 let defaultChannelId = '1'
200 const res = await getMyUserInformation(url, accessToken)
201 defaultChannelId = res.body.videoChannels[0].id
202 } catch (e) { /* empty */ }
204 // Default attributes
206 name: 'my super video',
210 channelId: defaultChannelId,
212 description: 'my super description',
214 privacy: VideoPrivacy.PUBLIC,
215 fixture: 'video_short.webm'
217 attributes = Object.assign(attributes, videoAttributesArg)
219 const req = request(url)
221 .set('Accept', 'application/json')
222 .set('Authorization', 'Bearer ' + accessToken)
223 .field('name', attributes.name)
224 .field('category', attributes.category.toString())
225 .field('licence', attributes.licence.toString())
226 .field('nsfw', JSON.stringify(attributes.nsfw))
227 .field('description', attributes.description)
228 .field('privacy', attributes.privacy.toString())
229 .field('channelId', attributes.channelId)
231 if (attributes.language !== undefined) {
232 req.field('language', attributes.language.toString())
235 for (let i = 0; i < attributes.tags.length; i++) {
236 req.field('tags[' + i + ']', attributes.tags[i])
240 if (isAbsolute(attributes.fixture)) {
241 filePath = attributes.fixture
243 filePath = join(__dirname, '..', 'api', 'fixtures', attributes.fixture)
246 return req.attach('videofile', filePath)
247 .expect(specialStatus)
250 function updateVideo (url: string, accessToken: string, id: number, attributes: VideoAttributes, specialStatus = 204) {
251 const path = '/api/v1/videos/' + id
254 if (attributes.name) body['name'] = attributes.name
255 if (attributes.category) body['category'] = attributes.category
256 if (attributes.licence) body['licence'] = attributes.licence
257 if (attributes.language) body['language'] = attributes.language
258 if (attributes.nsfw) body['nsfw'] = attributes.nsfw
259 if (attributes.description) body['description'] = attributes.description
260 if (attributes.tags) body['tags'] = attributes.tags
261 if (attributes.privacy) body['privacy'] = attributes.privacy
266 .set('Accept', 'application/json')
267 .set('Authorization', 'Bearer ' + accessToken)
268 .expect(specialStatus)
271 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
272 const path = '/api/v1/videos/' + id + '/rate'
276 .set('Accept', 'application/json')
277 .set('Authorization', 'Bearer ' + accessToken)
279 .expect(specialStatus)
282 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
283 return new Promise<any>((res, rej) => {
284 const torrentName = videoUUID + '-' + resolution + '.torrent'
285 const torrentPath = join(__dirname, '..', '..', '..', 'test' + server.serverNumber, 'torrents', torrentName)
286 readFile(torrentPath, (err, data) => {
287 if (err) return rej(err)
289 return res(parseTorrent(data))
294 // ---------------------------------------------------------------------------
306 getVideosListPagination,
310 searchVideoWithPagination,