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 getAllVideosListBy (url: string) {
50 const path = '/api/v1/videos'
54 .query({ sort: 'createdAt' })
56 .query({ count: 10000 })
57 .set('Accept', 'application/json')
59 .expect('Content-Type', /json/)
62 function getVideo (url: string, id: number | string, expectedStatus = 200) {
63 const path = '/api/v1/videos/' + id
67 .set('Accept', 'application/json')
68 .expect(expectedStatus)
71 function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = 200) {
72 const path = '/api/v1/videos/' + id
76 .set('Authorization', 'Bearer ' + token)
77 .set('Accept', 'application/json')
78 .expect(expectedStatus)
81 function getVideoDescription (url: string, descriptionPath: string) {
84 .set('Accept', 'application/json')
86 .expect('Content-Type', /json/)
89 function getVideosList (url: string) {
90 const path = '/api/v1/videos'
94 .query({ sort: 'name' })
95 .set('Accept', 'application/json')
97 .expect('Content-Type', /json/)
100 function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string) {
101 const path = '/api/v1/users/me/videos'
103 const req = request(url)
105 .query({ start: start })
106 .query({ count: count })
108 if (sort) req.query({ sort })
110 return req.set('Accept', 'application/json')
111 .set('Authorization', 'Bearer ' + accessToken)
113 .expect('Content-Type', /json/)
116 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
117 const path = '/api/v1/videos'
119 const req = request(url)
121 .query({ start: start })
122 .query({ count: count })
124 if (sort) req.query({ sort })
126 return req.set('Accept', 'application/json')
128 .expect('Content-Type', /json/)
131 function getVideosListSort (url: string, sort: string) {
132 const path = '/api/v1/videos'
136 .query({ sort: sort })
137 .set('Accept', 'application/json')
139 .expect('Content-Type', /json/)
142 function removeVideo (url: string, token: string, id: number, expectedStatus = 204) {
143 const path = '/api/v1/videos'
146 .delete(path + '/' + id)
147 .set('Accept', 'application/json')
148 .set('Authorization', 'Bearer ' + token)
149 .expect(expectedStatus)
152 function searchVideo (url: string, search: string, field?: string) {
153 const path = '/api/v1/videos'
154 const req = request(url)
155 .get(path + '/search/' + search)
156 .set('Accept', 'application/json')
158 if (field) req.query({ field })
160 return req.expect(200)
161 .expect('Content-Type', /json/)
164 function searchVideoWithPagination (url: string, search: string, field: string, start: number, count: number, sort?: string) {
165 const path = '/api/v1/videos'
167 const req = request(url)
168 .get(path + '/search/' + search)
173 if (sort) req.query({ sort })
175 return req.set('Accept', 'application/json')
177 .expect('Content-Type', /json/)
180 function searchVideoWithSort (url: string, search: string, sort: string) {
181 const path = '/api/v1/videos'
184 .get(path + '/search/' + search)
186 .set('Accept', 'application/json')
188 .expect('Content-Type', /json/)
191 async function testVideoImage (url: string, imageName: string, imagePath: string) {
192 // Don't test images if the node env is not set
193 // Because we need a special ffmpeg version for this test
194 if (process.env['NODE_TEST_IMAGE']) {
195 const res = await request(url)
199 const data = await readFilePromise(join(__dirname, '..', 'api', 'fixtures', imageName + '.jpg'))
201 return data.equals(res.body)
203 console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
208 async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 204) {
209 const path = '/api/v1/videos/upload'
210 let defaultChannelId = '1'
213 const res = await getMyUserInformation(url, accessToken)
214 defaultChannelId = res.body.videoChannels[0].id
215 } catch (e) { /* empty */ }
217 // Default attributes
219 name: 'my super video',
223 channelId: defaultChannelId,
225 description: 'my super description',
227 privacy: VideoPrivacy.PUBLIC,
228 fixture: 'video_short.webm'
230 attributes = Object.assign(attributes, videoAttributesArg)
232 const req = request(url)
234 .set('Accept', 'application/json')
235 .set('Authorization', 'Bearer ' + accessToken)
236 .field('name', attributes.name)
237 .field('category', attributes.category.toString())
238 .field('licence', attributes.licence.toString())
239 .field('nsfw', JSON.stringify(attributes.nsfw))
240 .field('description', attributes.description)
241 .field('privacy', attributes.privacy.toString())
242 .field('channelId', attributes.channelId)
244 if (attributes.language !== undefined) {
245 req.field('language', attributes.language.toString())
248 for (let i = 0; i < attributes.tags.length; i++) {
249 req.field('tags[' + i + ']', attributes.tags[i])
253 if (isAbsolute(attributes.fixture)) {
254 filePath = attributes.fixture
256 filePath = join(__dirname, '..', 'api', 'fixtures', attributes.fixture)
259 return req.attach('videofile', filePath)
260 .expect(specialStatus)
263 function updateVideo (url: string, accessToken: string, id: number, attributes: VideoAttributes, specialStatus = 204) {
264 const path = '/api/v1/videos/' + id
267 if (attributes.name) body['name'] = attributes.name
268 if (attributes.category) body['category'] = attributes.category
269 if (attributes.licence) body['licence'] = attributes.licence
270 if (attributes.language) body['language'] = attributes.language
271 if (attributes.nsfw) body['nsfw'] = attributes.nsfw
272 if (attributes.description) body['description'] = attributes.description
273 if (attributes.tags) body['tags'] = attributes.tags
274 if (attributes.privacy) body['privacy'] = attributes.privacy
279 .set('Accept', 'application/json')
280 .set('Authorization', 'Bearer ' + accessToken)
281 .expect(specialStatus)
284 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
285 const path = '/api/v1/videos/' + id + '/rate'
289 .set('Accept', 'application/json')
290 .set('Authorization', 'Bearer ' + accessToken)
292 .expect(specialStatus)
295 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
296 return new Promise<any>((res, rej) => {
297 const torrentName = videoUUID + '-' + resolution + '.torrent'
298 const torrentPath = join(__dirname, '..', '..', '..', 'test' + server.serverNumber, 'torrents', torrentName)
299 readFile(torrentPath, (err, data) => {
300 if (err) return rej(err)
302 return res(parseTorrent(data))
307 // ---------------------------------------------------------------------------
320 getVideosListPagination,
324 searchVideoWithPagination,