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 getVideoDescription (url: string, descriptionPath: string) {
67 .set('Accept', 'application/json')
69 .expect('Content-Type', /json/)
72 function getVideosList (url: string) {
73 const path = '/api/v1/videos'
77 .query({ sort: 'name' })
78 .set('Accept', 'application/json')
80 .expect('Content-Type', /json/)
83 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
84 const path = '/api/v1/videos'
86 const req = request(url)
88 .query({ start: start })
89 .query({ count: count })
91 if (sort) req.query({ sort })
93 return req.set('Accept', 'application/json')
95 .expect('Content-Type', /json/)
98 function getVideosListSort (url: string, sort: string) {
99 const path = '/api/v1/videos'
103 .query({ sort: sort })
104 .set('Accept', 'application/json')
106 .expect('Content-Type', /json/)
109 function removeVideo (url: string, token: string, id: number, expectedStatus = 204) {
110 const path = '/api/v1/videos'
113 .delete(path + '/' + id)
114 .set('Accept', 'application/json')
115 .set('Authorization', 'Bearer ' + token)
116 .expect(expectedStatus)
119 function searchVideo (url: string, search: string, field?: string) {
120 const path = '/api/v1/videos'
121 const req = request(url)
122 .get(path + '/search/' + search)
123 .set('Accept', 'application/json')
125 if (field) req.query({ field })
127 return req.expect(200)
128 .expect('Content-Type', /json/)
131 function searchVideoWithPagination (url: string, search: string, field: string, start: number, count: number, sort?: string) {
132 const path = '/api/v1/videos'
134 const req = request(url)
135 .get(path + '/search/' + search)
140 if (sort) req.query({ sort })
142 return req.set('Accept', 'application/json')
144 .expect('Content-Type', /json/)
147 function searchVideoWithSort (url: string, search: string, sort: string) {
148 const path = '/api/v1/videos'
151 .get(path + '/search/' + search)
153 .set('Accept', 'application/json')
155 .expect('Content-Type', /json/)
158 async function testVideoImage (url: string, imageName: string, imagePath: string) {
159 // Don't test images if the node env is not set
160 // Because we need a special ffmpeg version for this test
161 if (process.env['NODE_TEST_IMAGE']) {
162 const res = await request(url)
166 const data = await readFilePromise(join(__dirname, '..', 'api', 'fixtures', imageName + '.jpg'))
168 return data.equals(res.body)
170 console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
175 async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 204) {
176 const path = '/api/v1/videos/upload'
177 let defaultChannelId = '1'
180 const res = await getMyUserInformation(url, accessToken)
181 defaultChannelId = res.body.videoChannels[0].id
182 } catch (e) { /* empty */ }
184 // Default attributes
186 name: 'my super video',
190 channelId: defaultChannelId,
192 description: 'my super description',
194 fixture: 'video_short.webm'
196 attributes = Object.assign(attributes, videoAttributesArg)
198 const req = request(url)
200 .set('Accept', 'application/json')
201 .set('Authorization', 'Bearer ' + accessToken)
202 .field('name', attributes.name)
203 .field('category', attributes.category.toString())
204 .field('licence', attributes.licence.toString())
205 .field('nsfw', JSON.stringify(attributes.nsfw))
206 .field('description', attributes.description)
207 .field('channelId', attributes.channelId)
209 if (attributes.language !== undefined) {
210 req.field('language', attributes.language.toString())
213 for (let i = 0; i < attributes.tags.length; i++) {
214 req.field('tags[' + i + ']', attributes.tags[i])
218 if (isAbsolute(attributes.fixture)) {
219 filePath = attributes.fixture
221 filePath = join(__dirname, '..', 'api', 'fixtures', attributes.fixture)
224 return req.attach('videofile', filePath)
225 .expect(specialStatus)
228 function updateVideo (url: string, accessToken: string, id: number, attributes: VideoAttributes, specialStatus = 204) {
229 const path = '/api/v1/videos/' + id
232 if (attributes.name) body['name'] = attributes.name
233 if (attributes.category) body['category'] = attributes.category
234 if (attributes.licence) body['licence'] = attributes.licence
235 if (attributes.language) body['language'] = attributes.language
236 if (attributes.nsfw) body['nsfw'] = attributes.nsfw
237 if (attributes.description) body['description'] = attributes.description
238 if (attributes.tags) body['tags'] = attributes.tags
243 .set('Accept', 'application/json')
244 .set('Authorization', 'Bearer ' + accessToken)
245 .expect(specialStatus)
248 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
249 const path = '/api/v1/videos/' + id + '/rate'
253 .set('Accept', 'application/json')
254 .set('Authorization', 'Bearer ' + accessToken)
256 .expect(specialStatus)
259 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
260 return new Promise<any>((res, rej) => {
261 const torrentName = videoUUID + '-' + resolution + '.torrent'
262 const torrentPath = join(__dirname, '..', '..', '..', 'test' + server.serverNumber, 'torrents', torrentName)
263 readFile(torrentPath, (err, data) => {
264 if (err) return rej(err)
266 return res(parseTorrent(data))
271 // ---------------------------------------------------------------------------
281 getVideosListPagination,
285 searchVideoWithPagination,