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