2016-08-07 15:09:59 -05:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const fs = require('fs')
|
|
|
|
const pathUtils = require('path')
|
|
|
|
const request = require('supertest')
|
|
|
|
|
|
|
|
const videosUtils = {
|
2017-03-22 15:15:55 -05:00
|
|
|
getVideoCategories,
|
2017-03-27 13:53:11 -05:00
|
|
|
getVideoLicences,
|
2016-10-02 05:19:02 -05:00
|
|
|
getAllVideosListBy,
|
|
|
|
getVideo,
|
|
|
|
getVideosList,
|
|
|
|
getVideosListPagination,
|
|
|
|
getVideosListSort,
|
|
|
|
removeVideo,
|
|
|
|
searchVideo,
|
|
|
|
searchVideoWithPagination,
|
|
|
|
searchVideoWithSort,
|
|
|
|
testVideoImage,
|
2016-12-29 12:07:05 -06:00
|
|
|
uploadVideo,
|
2017-03-08 14:35:43 -06:00
|
|
|
updateVideo,
|
|
|
|
rateVideo
|
2016-08-07 15:09:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------- Export functions --------------------
|
|
|
|
|
2017-03-22 15:15:55 -05:00
|
|
|
function getVideoCategories (url, end) {
|
|
|
|
const path = '/api/v1/videos/categories'
|
|
|
|
|
|
|
|
request(url)
|
|
|
|
.get(path)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.end(end)
|
|
|
|
}
|
|
|
|
|
2017-03-27 13:53:11 -05:00
|
|
|
function getVideoLicences (url, end) {
|
|
|
|
const path = '/api/v1/videos/licences'
|
|
|
|
|
|
|
|
request(url)
|
|
|
|
.get(path)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.end(end)
|
|
|
|
}
|
|
|
|
|
2016-08-07 15:09:59 -05:00
|
|
|
function getAllVideosListBy (url, end) {
|
|
|
|
const path = '/api/v1/videos'
|
|
|
|
|
|
|
|
request(url)
|
|
|
|
.get(path)
|
2016-12-11 14:50:51 -06:00
|
|
|
.query({ sort: 'createdAt' })
|
2016-08-07 15:09:59 -05:00
|
|
|
.query({ start: 0 })
|
|
|
|
.query({ count: 10000 })
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.end(end)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getVideo (url, id, end) {
|
|
|
|
const path = '/api/v1/videos/' + id
|
|
|
|
|
|
|
|
request(url)
|
|
|
|
.get(path)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.end(end)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getVideosList (url, end) {
|
|
|
|
const path = '/api/v1/videos'
|
|
|
|
|
|
|
|
request(url)
|
|
|
|
.get(path)
|
|
|
|
.query({ sort: 'name' })
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.end(end)
|
|
|
|
}
|
|
|
|
|
2017-01-12 06:08:47 -06:00
|
|
|
function getVideosListPagination (url, start, count, sort, end) {
|
|
|
|
if (!end) {
|
|
|
|
end = sort
|
|
|
|
sort = null
|
|
|
|
}
|
|
|
|
|
2016-08-07 15:09:59 -05:00
|
|
|
const path = '/api/v1/videos'
|
|
|
|
|
2017-01-12 06:08:47 -06:00
|
|
|
const req = request(url)
|
|
|
|
.get(path)
|
|
|
|
.query({ start: start })
|
|
|
|
.query({ count: count })
|
|
|
|
|
|
|
|
if (sort) req.query({ sort })
|
|
|
|
|
|
|
|
req.set('Accept', 'application/json')
|
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.end(end)
|
2016-08-07 15:09:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function getVideosListSort (url, sort, end) {
|
|
|
|
const path = '/api/v1/videos'
|
|
|
|
|
|
|
|
request(url)
|
|
|
|
.get(path)
|
|
|
|
.query({ sort: sort })
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.end(end)
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeVideo (url, token, id, expectedStatus, end) {
|
|
|
|
if (!end) {
|
|
|
|
end = expectedStatus
|
|
|
|
expectedStatus = 204
|
|
|
|
}
|
|
|
|
|
|
|
|
const path = '/api/v1/videos'
|
|
|
|
|
|
|
|
request(url)
|
|
|
|
.delete(path + '/' + id)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', 'Bearer ' + token)
|
|
|
|
.expect(expectedStatus)
|
|
|
|
.end(end)
|
|
|
|
}
|
|
|
|
|
|
|
|
function searchVideo (url, search, field, end) {
|
|
|
|
if (!end) {
|
|
|
|
end = field
|
|
|
|
field = null
|
|
|
|
}
|
|
|
|
|
|
|
|
const path = '/api/v1/videos'
|
|
|
|
const req = request(url)
|
|
|
|
.get(path + '/search/' + search)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
|
|
|
|
if (field) req.query({ field: field })
|
|
|
|
req.expect(200)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.end(end)
|
|
|
|
}
|
|
|
|
|
2017-01-12 06:08:47 -06:00
|
|
|
function searchVideoWithPagination (url, search, field, start, count, sort, end) {
|
|
|
|
if (!end) {
|
|
|
|
end = sort
|
|
|
|
sort = null
|
|
|
|
}
|
|
|
|
|
2016-08-07 15:09:59 -05:00
|
|
|
const path = '/api/v1/videos'
|
|
|
|
|
2017-01-12 06:08:47 -06:00
|
|
|
const req = request(url)
|
|
|
|
.get(path + '/search/' + search)
|
|
|
|
.query({ start: start })
|
|
|
|
.query({ count: count })
|
|
|
|
.query({ field: field })
|
|
|
|
|
|
|
|
if (sort) req.query({ sort })
|
|
|
|
|
|
|
|
req.set('Accept', 'application/json')
|
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.end(end)
|
2016-08-07 15:09:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function searchVideoWithSort (url, search, sort, end) {
|
|
|
|
const path = '/api/v1/videos'
|
|
|
|
|
|
|
|
request(url)
|
|
|
|
.get(path + '/search/' + search)
|
|
|
|
.query({ sort: sort })
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(200)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.end(end)
|
|
|
|
}
|
|
|
|
|
|
|
|
function testVideoImage (url, videoName, imagePath, callback) {
|
|
|
|
// Don't test images if the node env is not set
|
|
|
|
// Because we need a special ffmpeg version for this test
|
|
|
|
if (process.env.NODE_TEST_IMAGE) {
|
|
|
|
request(url)
|
|
|
|
.get(imagePath)
|
|
|
|
.expect(200)
|
|
|
|
.end(function (err, res) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
fs.readFile(pathUtils.join(__dirname, '..', 'api', 'fixtures', videoName + '.jpg'), function (err, data) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
callback(null, data.equals(res.body))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
|
|
|
|
callback(null, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 11:39:01 -05:00
|
|
|
function uploadVideo (url, accessToken, videoAttributesArg, specialStatus, end) {
|
2016-08-07 15:09:59 -05:00
|
|
|
if (!end) {
|
|
|
|
end = specialStatus
|
|
|
|
specialStatus = 204
|
|
|
|
}
|
|
|
|
|
|
|
|
const path = '/api/v1/videos'
|
|
|
|
|
2017-03-26 11:39:01 -05:00
|
|
|
// Default attributes
|
|
|
|
let attributes = {
|
|
|
|
name: 'my super video',
|
|
|
|
category: 5,
|
2017-03-27 13:53:11 -05:00
|
|
|
licence: 4,
|
2017-03-26 11:39:01 -05:00
|
|
|
description: 'my super description',
|
|
|
|
tags: [ 'tag' ],
|
|
|
|
fixture: 'video_short.webm'
|
|
|
|
}
|
|
|
|
attributes = Object.assign(attributes, videoAttributesArg)
|
|
|
|
|
2016-08-07 15:09:59 -05:00
|
|
|
const req = request(url)
|
|
|
|
.post(path)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', 'Bearer ' + accessToken)
|
2017-03-26 11:39:01 -05:00
|
|
|
.field('name', attributes.name)
|
|
|
|
.field('category', attributes.category)
|
2017-03-27 13:53:11 -05:00
|
|
|
.field('licence', attributes.licence)
|
2017-03-26 11:39:01 -05:00
|
|
|
.field('description', attributes.description)
|
2016-08-07 15:09:59 -05:00
|
|
|
|
2017-03-26 11:39:01 -05:00
|
|
|
for (let i = 0; i < attributes.tags.length; i++) {
|
|
|
|
req.field('tags[' + i + ']', attributes.tags[i])
|
2016-08-07 15:09:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let filepath = ''
|
2017-03-26 11:39:01 -05:00
|
|
|
if (pathUtils.isAbsolute(attributes.fixture)) {
|
|
|
|
filepath = attributes.fixture
|
2016-08-07 15:09:59 -05:00
|
|
|
} else {
|
2017-03-26 11:39:01 -05:00
|
|
|
filepath = pathUtils.join(__dirname, '..', 'api', 'fixtures', attributes.fixture)
|
2016-08-07 15:09:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
req.attach('videofile', filepath)
|
|
|
|
.expect(specialStatus)
|
|
|
|
.end(end)
|
|
|
|
}
|
|
|
|
|
2017-03-26 11:39:01 -05:00
|
|
|
function updateVideo (url, accessToken, id, attributes, specialStatus, end) {
|
2016-12-29 12:07:05 -06:00
|
|
|
if (!end) {
|
|
|
|
end = specialStatus
|
|
|
|
specialStatus = 204
|
|
|
|
}
|
|
|
|
|
|
|
|
const path = '/api/v1/videos/' + id
|
|
|
|
|
|
|
|
const req = request(url)
|
|
|
|
.put(path)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', 'Bearer ' + accessToken)
|
|
|
|
|
2017-03-26 11:39:01 -05:00
|
|
|
if (attributes.name) req.field('name', attributes.name)
|
|
|
|
if (attributes.category) req.field('category', attributes.category)
|
2017-03-27 13:53:11 -05:00
|
|
|
if (attributes.licence) req.field('licence', attributes.licence)
|
2017-03-26 11:39:01 -05:00
|
|
|
if (attributes.description) req.field('description', attributes.description)
|
2016-12-29 12:07:05 -06:00
|
|
|
|
2017-03-26 11:39:01 -05:00
|
|
|
if (attributes.tags) {
|
|
|
|
for (let i = 0; i < attributes.tags.length; i++) {
|
|
|
|
req.field('tags[' + i + ']', attributes.tags[i])
|
2016-12-29 12:07:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req.expect(specialStatus).end(end)
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:35:43 -06:00
|
|
|
function rateVideo (url, accessToken, id, rating, specialStatus, end) {
|
|
|
|
if (!end) {
|
|
|
|
end = specialStatus
|
|
|
|
specialStatus = 204
|
|
|
|
}
|
|
|
|
|
|
|
|
const path = '/api/v1/videos/' + id + '/rate'
|
|
|
|
|
|
|
|
request(url)
|
|
|
|
.put(path)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', 'Bearer ' + accessToken)
|
|
|
|
.send({ rating })
|
|
|
|
.expect(specialStatus)
|
|
|
|
.end(end)
|
|
|
|
}
|
|
|
|
|
2016-08-07 15:09:59 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
module.exports = videosUtils
|