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