]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/utils/videos.js
Fix video update test
[github/Chocobozzz/PeerTube.git] / server / tests / utils / videos.js
CommitLineData
8d309058
C
1'use strict'
2
3const fs = require('fs')
4const pathUtils = require('path')
5const request = require('supertest')
6
7const videosUtils = {
6e07c3de 8 getVideoCategories,
6f0c39e2 9 getVideoLicences,
3092476e 10 getVideoLanguages,
c4403b29
C
11 getAllVideosListBy,
12 getVideo,
13 getVideosList,
14 getVideosListPagination,
15 getVideosListSort,
16 removeVideo,
17 searchVideo,
18 searchVideoWithPagination,
19 searchVideoWithSort,
20 testVideoImage,
7b1f49de 21 uploadVideo,
d38b8281
C
22 updateVideo,
23 rateVideo
8d309058
C
24}
25
26// ---------------------- Export functions --------------------
27
6e07c3de
C
28function 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
6f0c39e2
C
39function 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
3092476e
C
50function 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
8d309058
C
61function getAllVideosListBy (url, end) {
62 const path = '/api/v1/videos'
63
64 request(url)
65 .get(path)
feb4bdfd 66 .query({ sort: 'createdAt' })
8d309058
C
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
75function 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
86function 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
91cc839a
C
98function getVideosListPagination (url, start, count, sort, end) {
99 if (!end) {
100 end = sort
101 sort = null
102 }
103
8d309058
C
104 const path = '/api/v1/videos'
105
91cc839a
C
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)
8d309058
C
117}
118
119function 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
131function 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
147function 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
91cc839a
C
164function searchVideoWithPagination (url, search, field, start, count, sort, end) {
165 if (!end) {
166 end = sort
167 sort = null
168 }
169
8d309058
C
170 const path = '/api/v1/videos'
171
91cc839a
C
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)
8d309058
C
184}
185
186function 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
198function 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
b4c5ac97 220function uploadVideo (url, accessToken, videoAttributesArg, specialStatus, end) {
8d309058
C
221 if (!end) {
222 end = specialStatus
223 specialStatus = 204
224 }
225
226 const path = '/api/v1/videos'
227
b4c5ac97
C
228 // Default attributes
229 let attributes = {
230 name: 'my super video',
231 category: 5,
6f0c39e2 232 licence: 4,
3092476e 233 language: 3,
31b59b47 234 nsfw: true,
b4c5ac97
C
235 description: 'my super description',
236 tags: [ 'tag' ],
237 fixture: 'video_short.webm'
238 }
239 attributes = Object.assign(attributes, videoAttributesArg)
240
8d309058
C
241 const req = request(url)
242 .post(path)
243 .set('Accept', 'application/json')
244 .set('Authorization', 'Bearer ' + accessToken)
b4c5ac97
C
245 .field('name', attributes.name)
246 .field('category', attributes.category)
6f0c39e2 247 .field('licence', attributes.licence)
3092476e 248 .field('language', attributes.language)
31b59b47 249 .field('nsfw', attributes.nsfw)
b4c5ac97 250 .field('description', attributes.description)
8d309058 251
b4c5ac97
C
252 for (let i = 0; i < attributes.tags.length; i++) {
253 req.field('tags[' + i + ']', attributes.tags[i])
8d309058
C
254 }
255
256 let filepath = ''
b4c5ac97
C
257 if (pathUtils.isAbsolute(attributes.fixture)) {
258 filepath = attributes.fixture
8d309058 259 } else {
b4c5ac97 260 filepath = pathUtils.join(__dirname, '..', 'api', 'fixtures', attributes.fixture)
8d309058
C
261 }
262
263 req.attach('videofile', filepath)
264 .expect(specialStatus)
265 .end(end)
266}
267
b4c5ac97 268function updateVideo (url, accessToken, id, attributes, specialStatus, end) {
7b1f49de
C
269 if (!end) {
270 end = specialStatus
271 specialStatus = 204
272 }
273
274 const path = '/api/v1/videos/' + id
c3d19a49 275 const body = {}
7b1f49de 276
c3d19a49
C
277 if (attributes.name) body.name = attributes.name
278 if (attributes.category) body.category = attributes.category
279 if (attributes.licence) body.licence = attributes.licence
280 if (attributes.language) body.language = attributes.language
281 if (attributes.nsfw) body.nsfw = attributes.nsfw
282 if (attributes.description) body.description = attributes.description
283 if (attributes.tags) body.tags = attributes.tags
7b1f49de 284
c3d19a49
C
285 request(url)
286 .put(path)
287 .send(body)
288 .set('Accept', 'application/json')
289 .set('Authorization', 'Bearer ' + accessToken)
290 .expect(specialStatus)
291 .end(end)
7b1f49de
C
292}
293
d38b8281
C
294function rateVideo (url, accessToken, id, rating, specialStatus, end) {
295 if (!end) {
296 end = specialStatus
297 specialStatus = 204
298 }
299
300 const path = '/api/v1/videos/' + id + '/rate'
301
302 request(url)
303 .put(path)
304 .set('Accept', 'application/json')
305 .set('Authorization', 'Bearer ' + accessToken)
306 .send({ rating })
307 .expect(specialStatus)
308 .end(end)
309}
310
8d309058
C
311// ---------------------------------------------------------------------------
312
313module.exports = videosUtils