Server: add video language attribute
[oweals/peertube.git] / server / tests / utils / videos.js
1 'use strict'
2
3 const fs = require('fs')
4 const pathUtils = require('path')
5 const request = require('supertest')
6
7 const videosUtils = {
8   getVideoCategories,
9   getVideoLicences,
10   getVideoLanguages,
11   getAllVideosListBy,
12   getVideo,
13   getVideosList,
14   getVideosListPagination,
15   getVideosListSort,
16   removeVideo,
17   searchVideo,
18   searchVideoWithPagination,
19   searchVideoWithSort,
20   testVideoImage,
21   uploadVideo,
22   updateVideo,
23   rateVideo
24 }
25
26 // ---------------------- Export functions --------------------
27
28 function getVideoCategories (url, end) {
29   const path = '/api/v1/videos/categories'
30
31   request(url)
32     .get(path)
33     .set('Accept', 'application/json')
34     .expect(200)
35     .expect('Content-Type', /json/)
36     .end(end)
37 }
38
39 function getVideoLicences (url, end) {
40   const path = '/api/v1/videos/licences'
41
42   request(url)
43     .get(path)
44     .set('Accept', 'application/json')
45     .expect(200)
46     .expect('Content-Type', /json/)
47     .end(end)
48 }
49
50 function getVideoLanguages (url, end) {
51   const path = '/api/v1/videos/languages'
52
53   request(url)
54     .get(path)
55     .set('Accept', 'application/json')
56     .expect(200)
57     .expect('Content-Type', /json/)
58     .end(end)
59 }
60
61 function getAllVideosListBy (url, end) {
62   const path = '/api/v1/videos'
63
64   request(url)
65     .get(path)
66     .query({ sort: 'createdAt' })
67     .query({ start: 0 })
68     .query({ count: 10000 })
69     .set('Accept', 'application/json')
70     .expect(200)
71     .expect('Content-Type', /json/)
72     .end(end)
73 }
74
75 function getVideo (url, id, end) {
76   const path = '/api/v1/videos/' + id
77
78   request(url)
79     .get(path)
80     .set('Accept', 'application/json')
81     .expect(200)
82     .expect('Content-Type', /json/)
83     .end(end)
84 }
85
86 function getVideosList (url, end) {
87   const path = '/api/v1/videos'
88
89   request(url)
90     .get(path)
91     .query({ sort: 'name' })
92     .set('Accept', 'application/json')
93     .expect(200)
94     .expect('Content-Type', /json/)
95     .end(end)
96 }
97
98 function getVideosListPagination (url, start, count, sort, end) {
99   if (!end) {
100     end = sort
101     sort = null
102   }
103
104   const path = '/api/v1/videos'
105
106   const req = request(url)
107               .get(path)
108               .query({ start: start })
109               .query({ count: count })
110
111   if (sort) req.query({ sort })
112
113   req.set('Accept', 'application/json')
114      .expect(200)
115      .expect('Content-Type', /json/)
116      .end(end)
117 }
118
119 function getVideosListSort (url, sort, end) {
120   const path = '/api/v1/videos'
121
122   request(url)
123     .get(path)
124     .query({ sort: sort })
125     .set('Accept', 'application/json')
126     .expect(200)
127     .expect('Content-Type', /json/)
128     .end(end)
129 }
130
131 function removeVideo (url, token, id, expectedStatus, end) {
132   if (!end) {
133     end = expectedStatus
134     expectedStatus = 204
135   }
136
137   const path = '/api/v1/videos'
138
139   request(url)
140     .delete(path + '/' + id)
141     .set('Accept', 'application/json')
142     .set('Authorization', 'Bearer ' + token)
143     .expect(expectedStatus)
144     .end(end)
145 }
146
147 function searchVideo (url, search, field, end) {
148   if (!end) {
149     end = field
150     field = null
151   }
152
153   const path = '/api/v1/videos'
154   const req = request(url)
155               .get(path + '/search/' + search)
156               .set('Accept', 'application/json')
157
158   if (field) req.query({ field: field })
159   req.expect(200)
160      .expect('Content-Type', /json/)
161      .end(end)
162 }
163
164 function searchVideoWithPagination (url, search, field, start, count, sort, end) {
165   if (!end) {
166     end = sort
167     sort = null
168   }
169
170   const path = '/api/v1/videos'
171
172   const req = request(url)
173               .get(path + '/search/' + search)
174               .query({ start: start })
175               .query({ count: count })
176               .query({ field: field })
177
178   if (sort) req.query({ sort })
179
180   req.set('Accept', 'application/json')
181      .expect(200)
182      .expect('Content-Type', /json/)
183      .end(end)
184 }
185
186 function searchVideoWithSort (url, search, sort, end) {
187   const path = '/api/v1/videos'
188
189   request(url)
190     .get(path + '/search/' + search)
191     .query({ sort: sort })
192     .set('Accept', 'application/json')
193     .expect(200)
194     .expect('Content-Type', /json/)
195     .end(end)
196 }
197
198 function testVideoImage (url, videoName, imagePath, callback) {
199   // Don't test images if the node env is not set
200   // Because we need a special ffmpeg version for this test
201   if (process.env.NODE_TEST_IMAGE) {
202     request(url)
203       .get(imagePath)
204       .expect(200)
205       .end(function (err, res) {
206         if (err) return callback(err)
207
208         fs.readFile(pathUtils.join(__dirname, '..', 'api', 'fixtures', videoName + '.jpg'), function (err, data) {
209           if (err) return callback(err)
210
211           callback(null, data.equals(res.body))
212         })
213       })
214   } else {
215     console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
216     callback(null, true)
217   }
218 }
219
220 function uploadVideo (url, accessToken, videoAttributesArg, specialStatus, end) {
221   if (!end) {
222     end = specialStatus
223     specialStatus = 204
224   }
225
226   const path = '/api/v1/videos'
227
228   // Default attributes
229   let attributes = {
230     name: 'my super video',
231     category: 5,
232     licence: 4,
233     language: 3,
234     nsfw: true,
235     description: 'my super description',
236     tags: [ 'tag' ],
237     fixture: 'video_short.webm'
238   }
239   attributes = Object.assign(attributes, videoAttributesArg)
240
241   const req = request(url)
242               .post(path)
243               .set('Accept', 'application/json')
244               .set('Authorization', 'Bearer ' + accessToken)
245               .field('name', attributes.name)
246               .field('category', attributes.category)
247               .field('licence', attributes.licence)
248               .field('language', attributes.language)
249               .field('nsfw', attributes.nsfw)
250               .field('description', attributes.description)
251
252   for (let i = 0; i < attributes.tags.length; i++) {
253     req.field('tags[' + i + ']', attributes.tags[i])
254   }
255
256   let filepath = ''
257   if (pathUtils.isAbsolute(attributes.fixture)) {
258     filepath = attributes.fixture
259   } else {
260     filepath = pathUtils.join(__dirname, '..', 'api', 'fixtures', attributes.fixture)
261   }
262
263   req.attach('videofile', filepath)
264      .expect(specialStatus)
265      .end(end)
266 }
267
268 function updateVideo (url, accessToken, id, attributes, specialStatus, end) {
269   if (!end) {
270     end = specialStatus
271     specialStatus = 204
272   }
273
274   const path = '/api/v1/videos/' + id
275
276   const req = request(url)
277               .put(path)
278               .set('Accept', 'application/json')
279               .set('Authorization', 'Bearer ' + accessToken)
280
281   if (attributes.name) req.field('name', attributes.name)
282   if (attributes.category) req.field('category', attributes.category)
283   if (attributes.licence) req.field('licence', attributes.licence)
284   if (attributes.language) req.field('language', attributes.language)
285   if (attributes.nsfw) req.field('nsfw', attributes.nsfw)
286   if (attributes.description) req.field('description', attributes.description)
287
288   if (attributes.tags) {
289     for (let i = 0; i < attributes.tags.length; i++) {
290       req.field('tags[' + i + ']', attributes.tags[i])
291     }
292   }
293
294   req.expect(specialStatus).end(end)
295 }
296
297 function rateVideo (url, accessToken, id, rating, specialStatus, end) {
298   if (!end) {
299     end = specialStatus
300     specialStatus = 204
301   }
302
303   const path = '/api/v1/videos/' + id + '/rate'
304
305   request(url)
306     .put(path)
307     .set('Accept', 'application/json')
308     .set('Authorization', 'Bearer ' + accessToken)
309     .send({ rating })
310     .expect(specialStatus)
311     .end(end)
312 }
313
314 // ---------------------------------------------------------------------------
315
316 module.exports = videosUtils