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 getLocalVideos (url: string) {
127 const path = '/api/v1/videos'
131 .query({ sort: 'name', filter: 'local' })
132 .set('Accept', 'application/json')
134 .expect('Content-Type', /json/)
137 function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string) {
138 const path = '/api/v1/users/me/videos'
140 const req = request(url)
142 .query({ start: start })
143 .query({ count: count })
145 if (sort) req.query({ sort })
147 return req.set('Accept', 'application/json')
148 .set('Authorization', 'Bearer ' + accessToken)
150 .expect('Content-Type', /json/)
153 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
154 const path = '/api/v1/videos'
156 const req = request(url)
158 .query({ start: start })
159 .query({ count: count })
161 if (sort) req.query({ sort })
163 return req.set('Accept', 'application/json')
165 .expect('Content-Type', /json/)
168 function getVideosListSort (url: string, sort: string) {
169 const path = '/api/v1/videos'
173 .query({ sort: sort })
174 .set('Accept', 'application/json')
176 .expect('Content-Type', /json/)
179 function removeVideo (url: string, token: string, id: number | string, expectedStatus = 204) {
180 const path = '/api/v1/videos'
183 .delete(path + '/' + id)
184 .set('Accept', 'application/json')
185 .set('Authorization', 'Bearer ' + token)
186 .expect(expectedStatus)
189 function searchVideo (url: string, search: string) {
190 const path = '/api/v1/videos'
191 const req = request(url)
192 .get(path + '/search')
194 .set('Accept', 'application/json')
196 return req.expect(200)
197 .expect('Content-Type', /json/)
200 function searchVideoWithPagination (url: string, search: string, start: number, count: number, sort?: string) {
201 const path = '/api/v1/videos'
203 const req = request(url)
204 .get(path + '/search')
209 if (sort) req.query({ sort })
211 return req.set('Accept', 'application/json')
213 .expect('Content-Type', /json/)
216 function searchVideoWithSort (url: string, search: string, sort: string) {
217 const path = '/api/v1/videos'
220 .get(path + '/search')
223 .set('Accept', 'application/json')
225 .expect('Content-Type', /json/)
228 async function checkVideoFilesWereRemoved (videoUUID: string, serverNumber: number) {
229 const testDirectory = 'test' + serverNumber
231 for (const directory of [ 'videos', 'thumbnails', 'torrents', 'previews' ]) {
232 const directoryPath = join(root(), testDirectory, directory)
234 const directoryExists = existsSync(directoryPath)
235 expect(directoryExists).to.be.true
237 const files = await readdirPromise(directoryPath)
238 for (const file of files) {
239 expect(file).to.not.contain(videoUUID)
244 async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 200) {
245 const path = '/api/v1/videos/upload'
246 let defaultChannelId = '1'
249 const res = await getMyUserInformation(url, accessToken)
250 defaultChannelId = res.body.videoChannels[0].id
251 } catch (e) { /* empty */ }
253 // Override default attributes
254 const attributes = Object.assign({
255 name: 'my super video',
259 channelId: defaultChannelId,
261 description: 'my super description',
262 support: 'my super support text',
264 privacy: VideoPrivacy.PUBLIC,
265 commentsEnabled: true,
266 fixture: 'video_short.webm'
267 }, videoAttributesArg)
269 const req = request(url)
271 .set('Accept', 'application/json')
272 .set('Authorization', 'Bearer ' + accessToken)
273 .field('name', attributes.name)
274 .field('nsfw', JSON.stringify(attributes.nsfw))
275 .field('commentsEnabled', JSON.stringify(attributes.commentsEnabled))
276 .field('privacy', attributes.privacy.toString())
277 .field('channelId', attributes.channelId)
279 if (attributes.description !== undefined) {
280 req.field('description', attributes.description)
282 if (attributes.language !== undefined) {
283 req.field('language', attributes.language.toString())
285 if (attributes.category !== undefined) {
286 req.field('category', attributes.category.toString())
288 if (attributes.licence !== undefined) {
289 req.field('licence', attributes.licence.toString())
292 for (let i = 0; i < attributes.tags.length; i++) {
293 req.field('tags[' + i + ']', attributes.tags[i])
296 if (attributes.thumbnailfile !== undefined) {
297 req.attach('thumbnailfile', buildAbsoluteFixturePath(attributes.thumbnailfile))
299 if (attributes.previewfile !== undefined) {
300 req.attach('previewfile', buildAbsoluteFixturePath(attributes.previewfile))
303 return req.attach('videofile', buildAbsoluteFixturePath(attributes.fixture))
304 .expect(specialStatus)
307 function updateVideo (url: string, accessToken: string, id: number | string, attributes: VideoAttributes, statusCodeExpected = 204) {
308 const path = '/api/v1/videos/' + id
311 if (attributes.name) body['name'] = attributes.name
312 if (attributes.category) body['category'] = attributes.category
313 if (attributes.licence) body['licence'] = attributes.licence
314 if (attributes.language) body['language'] = attributes.language
315 if (attributes.nsfw !== undefined) body['nsfw'] = JSON.stringify(attributes.nsfw)
316 if (attributes.commentsEnabled !== undefined) body['commentsEnabled'] = JSON.stringify(attributes.commentsEnabled)
317 if (attributes.description) body['description'] = attributes.description
318 if (attributes.tags) body['tags'] = attributes.tags
319 if (attributes.privacy) body['privacy'] = attributes.privacy
322 if (attributes.thumbnailfile || attributes.previewfile) {
323 const attaches: any = {}
324 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
325 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
327 return makeUploadRequest({
338 return makePutBodyRequest({
347 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
348 const path = '/api/v1/videos/' + id + '/rate'
352 .set('Accept', 'application/json')
353 .set('Authorization', 'Bearer ' + accessToken)
355 .expect(specialStatus)
358 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
359 return new Promise<any>((res, rej) => {
360 const torrentName = videoUUID + '-' + resolution + '.torrent'
361 const torrentPath = join(__dirname, '..', '..', '..', '..', 'test' + server.serverNumber, 'torrents', torrentName)
362 readFile(torrentPath, (err, data) => {
363 if (err) return rej(err)
365 return res(parseTorrent(data))
370 async function completeVideoCheck (
379 commentsEnabled: boolean
402 thumbnailfile?: string
406 if (!attributes.likes) attributes.likes = 0
407 if (!attributes.dislikes) attributes.dislikes = 0
409 expect(video.name).to.equal(attributes.name)
410 expect(video.category.id).to.equal(attributes.category)
411 expect(video.category.label).to.equal(VIDEO_CATEGORIES[attributes.category] || 'Misc')
412 expect(video.licence.id).to.equal(attributes.licence)
413 expect(video.licence.label).to.equal(VIDEO_LICENCES[attributes.licence] || 'Unknown')
414 expect(video.language.id).to.equal(attributes.language)
415 expect(video.language.label).to.equal(VIDEO_LANGUAGES[attributes.language] || 'Unknown')
416 expect(video.nsfw).to.equal(attributes.nsfw)
417 expect(video.description).to.equal(attributes.description)
418 expect(video.account.host).to.equal(attributes.account.host)
419 expect(video.account.name).to.equal(attributes.account.name)
420 expect(video.likes).to.equal(attributes.likes)
421 expect(video.dislikes).to.equal(attributes.dislikes)
422 expect(video.isLocal).to.equal(attributes.isLocal)
423 expect(video.duration).to.equal(attributes.duration)
424 expect(dateIsValid(video.createdAt)).to.be.true
425 expect(dateIsValid(video.updatedAt)).to.be.true
427 const res = await getVideo(url, video.uuid)
428 const videoDetails = res.body
430 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
431 expect(videoDetails.tags).to.deep.equal(attributes.tags)
432 expect(videoDetails.privacy.id).to.deep.equal(attributes.privacy)
433 expect(videoDetails.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
434 expect(videoDetails.account.name).to.equal(attributes.account.name)
435 expect(videoDetails.account.host).to.equal(attributes.account.host)
436 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
438 expect(videoDetails.channel.displayName).to.equal(attributes.channel.name)
439 expect(videoDetails.channel.name).to.have.lengthOf(36)
440 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
441 expect(dateIsValid(videoDetails.channel.createdAt)).to.be.true
442 expect(dateIsValid(videoDetails.channel.updatedAt)).to.be.true
444 for (const attributeFile of attributes.files) {
445 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
446 expect(file).not.to.be.undefined
448 let extension = extname(attributes.fixture)
449 // Transcoding enabled on server 2, extension will always be .mp4
450 if (attributes.account.host === 'localhost:9002') extension = '.mp4'
452 const magnetUri = file.magnetUri
453 expect(file.magnetUri).to.have.lengthOf.above(2)
454 expect(file.torrentUrl).to.equal(`http://${attributes.account.host}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
455 expect(file.fileUrl).to.equal(`http://${attributes.account.host}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
456 expect(file.resolution.id).to.equal(attributeFile.resolution)
457 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
459 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
460 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
461 expect(file.size).to.be.above(minSize).and.below(maxSize)
464 await testImage(url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
467 if (attributes.previewfile) {
468 await testImage(url, attributes.previewfile, videoDetails.previewPath)
471 const torrent = await webtorrentAdd(magnetUri, true)
472 expect(torrent.files).to.be.an('array')
473 expect(torrent.files.length).to.equal(1)
474 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
478 // ---------------------------------------------------------------------------
490 getVideosListPagination,
494 searchVideoWithPagination,
503 checkVideoFilesWereRemoved