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) {
89 const path = '/api/v1/videos/' + id + '/views'
93 .set('Accept', 'application/json')
94 .expect(expectedStatus)
97 function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = 200) {
98 const path = '/api/v1/videos/' + id
102 .set('Authorization', 'Bearer ' + token)
103 .set('Accept', 'application/json')
104 .expect(expectedStatus)
107 function getVideoDescription (url: string, descriptionPath: string) {
109 .get(descriptionPath)
110 .set('Accept', 'application/json')
112 .expect('Content-Type', /json/)
115 function getVideosList (url: string) {
116 const path = '/api/v1/videos'
120 .query({ sort: 'name' })
121 .set('Accept', 'application/json')
123 .expect('Content-Type', /json/)
126 function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string) {
127 const path = '/api/v1/users/me/videos'
129 const req = request(url)
131 .query({ start: start })
132 .query({ count: count })
134 if (sort) req.query({ sort })
136 return req.set('Accept', 'application/json')
137 .set('Authorization', 'Bearer ' + accessToken)
139 .expect('Content-Type', /json/)
142 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
143 const path = '/api/v1/videos'
145 const req = request(url)
147 .query({ start: start })
148 .query({ count: count })
150 if (sort) req.query({ sort })
152 return req.set('Accept', 'application/json')
154 .expect('Content-Type', /json/)
157 function getVideosListSort (url: string, sort: string) {
158 const path = '/api/v1/videos'
162 .query({ sort: sort })
163 .set('Accept', 'application/json')
165 .expect('Content-Type', /json/)
168 function removeVideo (url: string, token: string, id: number | string, expectedStatus = 204) {
169 const path = '/api/v1/videos'
172 .delete(path + '/' + id)
173 .set('Accept', 'application/json')
174 .set('Authorization', 'Bearer ' + token)
175 .expect(expectedStatus)
178 function searchVideo (url: string, search: string) {
179 const path = '/api/v1/videos'
180 const req = request(url)
181 .get(path + '/search')
183 .set('Accept', 'application/json')
185 return req.expect(200)
186 .expect('Content-Type', /json/)
189 function searchVideoWithPagination (url: string, search: string, start: number, count: number, sort?: string) {
190 const path = '/api/v1/videos'
192 const req = request(url)
193 .get(path + '/search')
198 if (sort) req.query({ sort })
200 return req.set('Accept', 'application/json')
202 .expect('Content-Type', /json/)
205 function searchVideoWithSort (url: string, search: string, sort: string) {
206 const path = '/api/v1/videos'
209 .get(path + '/search')
212 .set('Accept', 'application/json')
214 .expect('Content-Type', /json/)
217 async function checkVideoFilesWereRemoved (videoUUID: string, serverNumber: number) {
218 const testDirectory = 'test' + serverNumber
220 for (const directory of [ 'videos', 'thumbnails', 'torrents', 'previews' ]) {
221 const directoryPath = join(root(), testDirectory, directory)
223 const directoryExists = existsSync(directoryPath)
224 expect(directoryExists).to.be.true
226 const files = await readdirPromise(directoryPath)
227 for (const file of files) {
228 expect(file).to.not.contain(videoUUID)
233 async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 200) {
234 const path = '/api/v1/videos/upload'
235 let defaultChannelId = '1'
238 const res = await getMyUserInformation(url, accessToken)
239 defaultChannelId = res.body.videoChannels[0].id
240 } catch (e) { /* empty */ }
242 // Override default attributes
243 const attributes = Object.assign({
244 name: 'my super video',
248 channelId: defaultChannelId,
250 description: 'my super description',
252 privacy: VideoPrivacy.PUBLIC,
253 commentsEnabled: true,
254 fixture: 'video_short.webm'
255 }, videoAttributesArg)
257 const req = request(url)
259 .set('Accept', 'application/json')
260 .set('Authorization', 'Bearer ' + accessToken)
261 .field('name', attributes.name)
262 .field('nsfw', JSON.stringify(attributes.nsfw))
263 .field('commentsEnabled', JSON.stringify(attributes.commentsEnabled))
264 .field('privacy', attributes.privacy.toString())
265 .field('channelId', attributes.channelId)
267 if (attributes.description !== undefined) {
268 req.field('description', attributes.description)
270 if (attributes.language !== undefined) {
271 req.field('language', attributes.language.toString())
273 if (attributes.category !== undefined) {
274 req.field('category', attributes.category.toString())
276 if (attributes.licence !== undefined) {
277 req.field('licence', attributes.licence.toString())
280 if (attributes.thumbnailfile !== undefined) {
281 req.attach('thumbnailfile', buildAbsoluteFixturePath(attributes.thumbnailfile))
283 if (attributes.previewfile !== undefined) {
284 req.attach('previewfile', buildAbsoluteFixturePath(attributes.previewfile))
287 for (let i = 0; i < attributes.tags.length; i++) {
288 req.field('tags[' + i + ']', attributes.tags[i])
291 return req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture))
292 .expect(specialStatus)
295 function updateVideo (url: string, accessToken: string, id: number | string, attributes: VideoAttributes, statusCodeExpected = 204) {
296 const path = '/api/v1/videos/' + id
299 if (attributes.name) body['name'] = attributes.name
300 if (attributes.category) body['category'] = attributes.category
301 if (attributes.licence) body['licence'] = attributes.licence
302 if (attributes.language) body['language'] = attributes.language
303 if (attributes.nsfw !== undefined) body['nsfw'] = JSON.stringify(attributes.nsfw)
304 if (attributes.commentsEnabled !== undefined) body['commentsEnabled'] = JSON.stringify(attributes.commentsEnabled)
305 if (attributes.description) body['description'] = attributes.description
306 if (attributes.tags) body['tags'] = attributes.tags
307 if (attributes.privacy) body['privacy'] = attributes.privacy
310 if (attributes.thumbnailfile || attributes.previewfile) {
311 const attaches: any = {}
312 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
313 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
315 return makeUploadRequest({
326 return makePutBodyRequest({
335 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
336 const path = '/api/v1/videos/' + id + '/rate'
340 .set('Accept', 'application/json')
341 .set('Authorization', 'Bearer ' + accessToken)
343 .expect(specialStatus)
346 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
347 return new Promise<any>((res, rej) => {
348 const torrentName = videoUUID + '-' + resolution + '.torrent'
349 const torrentPath = join(__dirname, '..', '..', '..', '..', 'test' + server.serverNumber, 'torrents', torrentName)
350 readFile(torrentPath, (err, data) => {
351 if (err) return rej(err)
353 return res(parseTorrent(data))
358 async function completeVideoCheck (
367 commentsEnabled: boolean
387 thumbnailfile?: string
391 if (!attributes.likes) attributes.likes = 0
392 if (!attributes.dislikes) attributes.dislikes = 0
394 expect(video.name).to.equal(attributes.name)
395 expect(video.category).to.equal(attributes.category)
396 expect(video.categoryLabel).to.equal(VIDEO_CATEGORIES[attributes.category] || 'Misc')
397 expect(video.licence).to.equal(attributes.licence)
398 expect(video.licenceLabel).to.equal(VIDEO_LICENCES[attributes.licence] || 'Unknown')
399 expect(video.language).to.equal(attributes.language)
400 expect(video.languageLabel).to.equal(VIDEO_LANGUAGES[attributes.language] || 'Unknown')
401 expect(video.nsfw).to.equal(attributes.nsfw)
402 expect(video.description).to.equal(attributes.description)
403 expect(video.serverHost).to.equal(attributes.host)
404 expect(video.accountName).to.equal(attributes.account)
405 expect(video.likes).to.equal(attributes.likes)
406 expect(video.dislikes).to.equal(attributes.dislikes)
407 expect(video.isLocal).to.equal(attributes.isLocal)
408 expect(video.duration).to.equal(attributes.duration)
409 expect(dateIsValid(video.createdAt)).to.be.true
410 expect(dateIsValid(video.updatedAt)).to.be.true
412 const res = await getVideo(url, video.uuid)
413 const videoDetails = res.body
415 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
416 expect(videoDetails.tags).to.deep.equal(attributes.tags)
417 expect(videoDetails.privacy).to.deep.equal(attributes.privacy)
418 expect(videoDetails.privacyLabel).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
419 expect(videoDetails.account.name).to.equal(attributes.account)
420 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
422 expect(videoDetails.channel.displayName).to.equal(attributes.channel.name)
423 expect(videoDetails.channel.name).to.have.lengthOf(36)
424 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
425 expect(dateIsValid(videoDetails.channel.createdAt)).to.be.true
426 expect(dateIsValid(videoDetails.channel.updatedAt)).to.be.true
428 for (const attributeFile of attributes.files) {
429 const file = videoDetails.files.find(f => f.resolution === attributeFile.resolution)
430 expect(file).not.to.be.undefined
432 let extension = extname(attributes.fixture)
433 // Transcoding enabled on server 2, extension will always be .mp4
434 if (attributes.host === 'localhost:9002') extension = '.mp4'
436 const magnetUri = file.magnetUri
437 expect(file.magnetUri).to.have.lengthOf.above(2)
438 expect(file.torrentUrl).to.equal(`http://${attributes.host}/static/torrents/${videoDetails.uuid}-${file.resolution}.torrent`)
439 expect(file.fileUrl).to.equal(`http://${attributes.host}/static/webseed/${videoDetails.uuid}-${file.resolution}${extension}`)
440 expect(file.resolution).to.equal(attributeFile.resolution)
441 expect(file.resolutionLabel).to.equal(attributeFile.resolution + 'p')
443 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
444 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
445 expect(file.size).to.be.above(minSize).and.below(maxSize)
448 await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
451 if (attributes.previewfile) {
452 await testImage(url, attributes.previewfile, videoDetails.previewPath)
455 const torrent = await webtorrentAdd(magnetUri, true)
456 expect(torrent.files).to.be.an('array')
457 expect(torrent.files.length).to.equal(1)
458 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
462 // ---------------------------------------------------------------------------
474 getVideosListPagination,
478 searchVideoWithPagination,
486 checkVideoFilesWereRemoved