1 /* tslint:disable:no-unused-expression */
3 import { expect } from 'chai'
4 import { existsSync, readFile } from 'fs'
5 import * as parseTorrent from 'parse-torrent'
6 import { extname, join } from 'path'
7 import * as request from 'supertest'
9 buildAbsoluteFixturePath,
18 import { VideoPrivacy } from '../../../../shared/models/videos'
19 import { readdirPromise } from '../../../helpers/core-utils'
20 import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../initializers'
21 import { dateIsValid, webtorrentAdd } from '../index'
23 type VideoAttributes = {
29 commentsEnabled?: boolean
33 privacy?: VideoPrivacy
35 thumbnailfile?: string
39 function getVideoCategories (url: string) {
40 const path = '/api/v1/videos/categories'
42 return makeGetRequest({
45 statusCodeExpected: 200
49 function getVideoLicences (url: string) {
50 const path = '/api/v1/videos/licences'
52 return makeGetRequest({
55 statusCodeExpected: 200
59 function getVideoLanguages (url: string) {
60 const path = '/api/v1/videos/languages'
62 return makeGetRequest({
65 statusCodeExpected: 200
69 function getVideoPrivacies (url: string) {
70 const path = '/api/v1/videos/privacies'
72 return makeGetRequest({
75 statusCodeExpected: 200
79 function getVideo (url: string, id: number | string, expectedStatus = 200) {
80 const path = '/api/v1/videos/' + id
84 .set('Accept', 'application/json')
85 .expect(expectedStatus)
88 function viewVideo (url: string, id: number | string, expectedStatus = 204, xForwardedFor?: string) {
89 const path = '/api/v1/videos/' + id + '/views'
91 const req = request(url)
93 .set('Accept', 'application/json')
96 req.set('X-Forwarded-For', xForwardedFor)
99 return req.expect(expectedStatus)
102 function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = 200) {
103 const path = '/api/v1/videos/' + id
107 .set('Authorization', 'Bearer ' + token)
108 .set('Accept', 'application/json')
109 .expect(expectedStatus)
112 function getVideoDescription (url: string, descriptionPath: string) {
114 .get(descriptionPath)
115 .set('Accept', 'application/json')
117 .expect('Content-Type', /json/)
120 function getVideosList (url: string) {
121 const path = '/api/v1/videos'
125 .query({ sort: 'name' })
126 .set('Accept', 'application/json')
128 .expect('Content-Type', /json/)
131 function getVideosListWithToken (url: string, token: string) {
132 const path = '/api/v1/videos'
136 .set('Authorization', 'Bearer ' + token)
137 .query({ sort: 'name' })
138 .set('Accept', 'application/json')
140 .expect('Content-Type', /json/)
143 function getLocalVideos (url: string) {
144 const path = '/api/v1/videos'
148 .query({ sort: 'name', filter: 'local' })
149 .set('Accept', 'application/json')
151 .expect('Content-Type', /json/)
154 function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string) {
155 const path = '/api/v1/users/me/videos'
157 const req = request(url)
159 .query({ start: start })
160 .query({ count: count })
162 if (sort) req.query({ sort })
164 return req.set('Accept', 'application/json')
165 .set('Authorization', 'Bearer ' + accessToken)
167 .expect('Content-Type', /json/)
170 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
171 const path = '/api/v1/videos'
173 const req = request(url)
175 .query({ start: start })
176 .query({ count: count })
178 if (sort) req.query({ sort })
180 return req.set('Accept', 'application/json')
182 .expect('Content-Type', /json/)
185 function getVideosListSort (url: string, sort: string) {
186 const path = '/api/v1/videos'
190 .query({ sort: sort })
191 .set('Accept', 'application/json')
193 .expect('Content-Type', /json/)
196 function removeVideo (url: string, token: string, id: number | string, expectedStatus = 204) {
197 const path = '/api/v1/videos'
200 .delete(path + '/' + id)
201 .set('Accept', 'application/json')
202 .set('Authorization', 'Bearer ' + token)
203 .expect(expectedStatus)
206 function searchVideo (url: string, search: string) {
207 const path = '/api/v1/videos'
208 const req = request(url)
209 .get(path + '/search')
211 .set('Accept', 'application/json')
213 return req.expect(200)
214 .expect('Content-Type', /json/)
217 function searchVideoWithToken (url: string, search: string, token: string) {
218 const path = '/api/v1/videos'
219 const req = request(url)
220 .get(path + '/search')
221 .set('Authorization', 'Bearer ' + token)
223 .set('Accept', 'application/json')
225 return req.expect(200)
226 .expect('Content-Type', /json/)
229 function searchVideoWithPagination (url: string, search: string, start: number, count: number, sort?: string) {
230 const path = '/api/v1/videos'
232 const req = request(url)
233 .get(path + '/search')
238 if (sort) req.query({ sort })
240 return req.set('Accept', 'application/json')
242 .expect('Content-Type', /json/)
245 function searchVideoWithSort (url: string, search: string, sort: string) {
246 const path = '/api/v1/videos'
249 .get(path + '/search')
252 .set('Accept', 'application/json')
254 .expect('Content-Type', /json/)
257 async function checkVideoFilesWereRemoved (videoUUID: string, serverNumber: number) {
258 const testDirectory = 'test' + serverNumber
260 for (const directory of [ 'videos', 'thumbnails', 'torrents', 'previews' ]) {
261 const directoryPath = join(root(), testDirectory, directory)
263 const directoryExists = existsSync(directoryPath)
264 expect(directoryExists).to.be.true
266 const files = await readdirPromise(directoryPath)
267 for (const file of files) {
268 expect(file).to.not.contain(videoUUID)
273 async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 200) {
274 const path = '/api/v1/videos/upload'
275 let defaultChannelId = '1'
278 const res = await getMyUserInformation(url, accessToken)
279 defaultChannelId = res.body.videoChannels[0].id
280 } catch (e) { /* empty */ }
282 // Override default attributes
283 const attributes = Object.assign({
284 name: 'my super video',
288 channelId: defaultChannelId,
290 description: 'my super description',
291 support: 'my super support text',
293 privacy: VideoPrivacy.PUBLIC,
294 commentsEnabled: true,
295 fixture: 'video_short.webm'
296 }, videoAttributesArg)
298 const req = request(url)
300 .set('Accept', 'application/json')
301 .set('Authorization', 'Bearer ' + accessToken)
302 .field('name', attributes.name)
303 .field('nsfw', JSON.stringify(attributes.nsfw))
304 .field('commentsEnabled', JSON.stringify(attributes.commentsEnabled))
305 .field('privacy', attributes.privacy.toString())
306 .field('channelId', attributes.channelId)
308 if (attributes.description !== undefined) {
309 req.field('description', attributes.description)
311 if (attributes.language !== undefined) {
312 req.field('language', attributes.language.toString())
314 if (attributes.category !== undefined) {
315 req.field('category', attributes.category.toString())
317 if (attributes.licence !== undefined) {
318 req.field('licence', attributes.licence.toString())
321 for (let i = 0; i < attributes.tags.length; i++) {
322 req.field('tags[' + i + ']', attributes.tags[i])
325 if (attributes.thumbnailfile !== undefined) {
326 req.attach('thumbnailfile', buildAbsoluteFixturePath(attributes.thumbnailfile))
328 if (attributes.previewfile !== undefined) {
329 req.attach('previewfile', buildAbsoluteFixturePath(attributes.previewfile))
332 return req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture))
333 .expect(specialStatus)
336 function updateVideo (url: string, accessToken: string, id: number | string, attributes: VideoAttributes, statusCodeExpected = 204) {
337 const path = '/api/v1/videos/' + id
340 if (attributes.name) body['name'] = attributes.name
341 if (attributes.category) body['category'] = attributes.category
342 if (attributes.licence) body['licence'] = attributes.licence
343 if (attributes.language) body['language'] = attributes.language
344 if (attributes.nsfw !== undefined) body['nsfw'] = JSON.stringify(attributes.nsfw)
345 if (attributes.commentsEnabled !== undefined) body['commentsEnabled'] = JSON.stringify(attributes.commentsEnabled)
346 if (attributes.description) body['description'] = attributes.description
347 if (attributes.tags) body['tags'] = attributes.tags
348 if (attributes.privacy) body['privacy'] = attributes.privacy
351 if (attributes.thumbnailfile || attributes.previewfile) {
352 const attaches: any = {}
353 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
354 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
356 return makeUploadRequest({
367 return makePutBodyRequest({
376 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
377 const path = '/api/v1/videos/' + id + '/rate'
381 .set('Accept', 'application/json')
382 .set('Authorization', 'Bearer ' + accessToken)
384 .expect(specialStatus)
387 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
388 return new Promise<any>((res, rej) => {
389 const torrentName = videoUUID + '-' + resolution + '.torrent'
390 const torrentPath = join(__dirname, '..', '..', '..', '..', 'test' + server.serverNumber, 'torrents', torrentName)
391 readFile(torrentPath, (err, data) => {
392 if (err) return rej(err)
394 return res(parseTorrent(data))
399 async function completeVideoCheck (
408 commentsEnabled: boolean
431 thumbnailfile?: string
435 if (!attributes.likes) attributes.likes = 0
436 if (!attributes.dislikes) attributes.dislikes = 0
438 expect(video.name).to.equal(attributes.name)
439 expect(video.category.id).to.equal(attributes.category)
440 expect(video.category.label).to.equal(VIDEO_CATEGORIES[attributes.category] || 'Misc')
441 expect(video.licence.id).to.equal(attributes.licence)
442 expect(video.licence.label).to.equal(VIDEO_LICENCES[attributes.licence] || 'Unknown')
443 expect(video.language.id).to.equal(attributes.language)
444 expect(video.language.label).to.equal(VIDEO_LANGUAGES[attributes.language] || 'Unknown')
445 expect(video.nsfw).to.equal(attributes.nsfw)
446 expect(video.description).to.equal(attributes.description)
447 expect(video.account.host).to.equal(attributes.account.host)
448 expect(video.account.name).to.equal(attributes.account.name)
449 expect(video.likes).to.equal(attributes.likes)
450 expect(video.dislikes).to.equal(attributes.dislikes)
451 expect(video.isLocal).to.equal(attributes.isLocal)
452 expect(video.duration).to.equal(attributes.duration)
453 expect(dateIsValid(video.createdAt)).to.be.true
454 expect(dateIsValid(video.publishedAt)).to.be.true
455 expect(dateIsValid(video.updatedAt)).to.be.true
457 const res = await getVideo(url, video.uuid)
458 const videoDetails = res.body
460 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
461 expect(videoDetails.tags).to.deep.equal(attributes.tags)
462 expect(videoDetails.privacy.id).to.deep.equal(attributes.privacy)
463 expect(videoDetails.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
464 expect(videoDetails.account.name).to.equal(attributes.account.name)
465 expect(videoDetails.account.host).to.equal(attributes.account.host)
466 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
468 expect(videoDetails.channel.displayName).to.equal(attributes.channel.name)
469 expect(videoDetails.channel.name).to.have.lengthOf(36)
470 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
471 expect(dateIsValid(videoDetails.channel.createdAt)).to.be.true
472 expect(dateIsValid(videoDetails.channel.updatedAt)).to.be.true
474 for (const attributeFile of attributes.files) {
475 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
476 expect(file).not.to.be.undefined
478 let extension = extname(attributes.fixture)
479 // Transcoding enabled on server 2, extension will always be .mp4
480 if (attributes.account.host === 'localhost:9002') extension = '.mp4'
482 const magnetUri = file.magnetUri
483 expect(file.magnetUri).to.have.lengthOf.above(2)
484 expect(file.torrentUrl).to.equal(`http://${attributes.account.host}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
485 expect(file.fileUrl).to.equal(`http://${attributes.account.host}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
486 expect(file.resolution.id).to.equal(attributeFile.resolution)
487 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
489 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
490 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
491 expect(file.size).to.be.above(minSize).and.below(maxSize)
494 await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
497 if (attributes.previewfile) {
498 await testImage(url, attributes.previewfile, videoDetails.previewPath)
501 const torrent = await webtorrentAdd(magnetUri, true)
502 expect(torrent.files).to.be.an('array')
503 expect(torrent.files.length).to.equal(1)
504 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
508 // ---------------------------------------------------------------------------
517 searchVideoWithToken,
521 getVideosListPagination,
525 searchVideoWithPagination,
527 getVideosListWithToken,
535 checkVideoFilesWereRemoved