1 import * as request from 'supertest'
2 import { join, isAbsolute } from 'path'
4 import { makeGetRequest } from './requests'
5 import { readFilePromise } from './miscs'
7 type VideoAttributes = {
18 function getVideoCategories (url: string) {
19 const path = '/api/v1/videos/categories'
21 return makeGetRequest(url, path)
24 function getVideoLicences (url: string) {
25 const path = '/api/v1/videos/licences'
27 return makeGetRequest(url, path)
30 function getVideoLanguages (url: string) {
31 const path = '/api/v1/videos/languages'
33 return makeGetRequest(url, path)
36 function getAllVideosListBy (url: string) {
37 const path = '/api/v1/videos'
41 .query({ sort: 'createdAt' })
43 .query({ count: 10000 })
44 .set('Accept', 'application/json')
46 .expect('Content-Type', /json/)
49 function getVideo (url: string, id: number | string) {
50 const path = '/api/v1/videos/' + id
54 .set('Accept', 'application/json')
56 .expect('Content-Type', /json/)
59 function getVideosList (url: string) {
60 const path = '/api/v1/videos'
64 .query({ sort: 'name' })
65 .set('Accept', 'application/json')
67 .expect('Content-Type', /json/)
70 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
71 const path = '/api/v1/videos'
73 const req = request(url)
75 .query({ start: start })
76 .query({ count: count })
78 if (sort) req.query({ sort })
80 return req.set('Accept', 'application/json')
82 .expect('Content-Type', /json/)
85 function getVideosListSort (url: string, sort: string) {
86 const path = '/api/v1/videos'
90 .query({ sort: sort })
91 .set('Accept', 'application/json')
93 .expect('Content-Type', /json/)
96 function removeVideo (url: string, token: string, id: number, expectedStatus = 204) {
97 const path = '/api/v1/videos'
100 .delete(path + '/' + id)
101 .set('Accept', 'application/json')
102 .set('Authorization', 'Bearer ' + token)
103 .expect(expectedStatus)
106 function searchVideo (url: string, search: string, field?: string) {
107 const path = '/api/v1/videos'
108 const req = request(url)
109 .get(path + '/search/' + search)
110 .set('Accept', 'application/json')
112 if (field) req.query({ field })
114 return req.expect(200)
115 .expect('Content-Type', /json/)
118 function searchVideoWithPagination (url: string, search: string, field: string, start: number, count: number, sort?: string) {
119 const path = '/api/v1/videos'
121 const req = request(url)
122 .get(path + '/search/' + search)
127 if (sort) req.query({ sort })
129 return req.set('Accept', 'application/json')
131 .expect('Content-Type', /json/)
134 function searchVideoWithSort (url: string, search: string, sort: string) {
135 const path = '/api/v1/videos'
138 .get(path + '/search/' + search)
140 .set('Accept', 'application/json')
142 .expect('Content-Type', /json/)
145 async function testVideoImage (url: string, imageName: string, imagePath: string) {
146 // Don't test images if the node env is not set
147 // Because we need a special ffmpeg version for this test
148 if (process.env['NODE_TEST_IMAGE']) {
149 const res = await request(url)
153 const data = await readFilePromise(join(__dirname, '..', 'api', 'fixtures', imageName + '.jpg'))
155 return data.equals(res.body)
157 console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
162 function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 204) {
163 const path = '/api/v1/videos'
165 // Default attributes
167 name: 'my super video',
172 description: 'my super description',
174 fixture: 'video_short.webm'
176 attributes = Object.assign(attributes, videoAttributesArg)
178 const req = request(url)
180 .set('Accept', 'application/json')
181 .set('Authorization', 'Bearer ' + accessToken)
182 .field('name', attributes.name)
183 .field('category', attributes.category.toString())
184 .field('licence', attributes.licence.toString())
185 .field('language', attributes.language.toString())
186 .field('nsfw', JSON.stringify(attributes.nsfw))
187 .field('description', attributes.description)
189 for (let i = 0; i < attributes.tags.length; i++) {
190 req.field('tags[' + i + ']', attributes.tags[i])
194 if (isAbsolute(attributes.fixture)) {
195 filepath = attributes.fixture
197 filepath = join(__dirname, '..', 'api', 'fixtures', attributes.fixture)
200 return req.attach('videofile', filepath)
201 .expect(specialStatus)
204 function updateVideo (url: string, accessToken: string, id: number, attributes: VideoAttributes, specialStatus = 204) {
205 const path = '/api/v1/videos/' + id
208 if (attributes.name) body['name'] = attributes.name
209 if (attributes.category) body['category'] = attributes.category
210 if (attributes.licence) body['licence'] = attributes.licence
211 if (attributes.language) body['language'] = attributes.language
212 if (attributes.nsfw) body['nsfw'] = attributes.nsfw
213 if (attributes.description) body['description'] = attributes.description
214 if (attributes.tags) body['tags'] = attributes.tags
219 .set('Accept', 'application/json')
220 .set('Authorization', 'Bearer ' + accessToken)
221 .expect(specialStatus)
224 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
225 const path = '/api/v1/videos/' + id + '/rate'
229 .set('Accept', 'application/json')
230 .set('Authorization', 'Bearer ' + accessToken)
232 .expect(specialStatus)
235 // ---------------------------------------------------------------------------
244 getVideosListPagination,
248 searchVideoWithPagination,