]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/utils/videos.js
Server: add licence video attribute
[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,
c4403b29
C
10 getAllVideosListBy,
11 getVideo,
12 getVideosList,
13 getVideosListPagination,
14 getVideosListSort,
15 removeVideo,
16 searchVideo,
17 searchVideoWithPagination,
18 searchVideoWithSort,
19 testVideoImage,
7b1f49de 20 uploadVideo,
d38b8281
C
21 updateVideo,
22 rateVideo
8d309058
C
23}
24
25// ---------------------- Export functions --------------------
26
6e07c3de
C
27function 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
6f0c39e2
C
38function 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
8d309058
C
49function getAllVideosListBy (url, end) {
50 const path = '/api/v1/videos'
51
52 request(url)
53 .get(path)
feb4bdfd 54 .query({ sort: 'createdAt' })
8d309058
C
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
63function 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
74function 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
91cc839a
C
86function getVideosListPagination (url, start, count, sort, end) {
87 if (!end) {
88 end = sort
89 sort = null
90 }
91
8d309058
C
92 const path = '/api/v1/videos'
93
91cc839a
C
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)
8d309058
C
105}
106
107function 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
119function 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
135function 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
91cc839a
C
152function searchVideoWithPagination (url, search, field, start, count, sort, end) {
153 if (!end) {
154 end = sort
155 sort = null
156 }
157
8d309058
C
158 const path = '/api/v1/videos'
159
91cc839a
C
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)
8d309058
C
172}
173
174function 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
186function 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
b4c5ac97 208function uploadVideo (url, accessToken, videoAttributesArg, specialStatus, end) {
8d309058
C
209 if (!end) {
210 end = specialStatus
211 specialStatus = 204
212 }
213
214 const path = '/api/v1/videos'
215
b4c5ac97
C
216 // Default attributes
217 let attributes = {
218 name: 'my super video',
219 category: 5,
6f0c39e2 220 licence: 4,
b4c5ac97
C
221 description: 'my super description',
222 tags: [ 'tag' ],
223 fixture: 'video_short.webm'
224 }
225 attributes = Object.assign(attributes, videoAttributesArg)
226
8d309058
C
227 const req = request(url)
228 .post(path)
229 .set('Accept', 'application/json')
230 .set('Authorization', 'Bearer ' + accessToken)
b4c5ac97
C
231 .field('name', attributes.name)
232 .field('category', attributes.category)
6f0c39e2 233 .field('licence', attributes.licence)
b4c5ac97 234 .field('description', attributes.description)
8d309058 235
b4c5ac97
C
236 for (let i = 0; i < attributes.tags.length; i++) {
237 req.field('tags[' + i + ']', attributes.tags[i])
8d309058
C
238 }
239
240 let filepath = ''
b4c5ac97
C
241 if (pathUtils.isAbsolute(attributes.fixture)) {
242 filepath = attributes.fixture
8d309058 243 } else {
b4c5ac97 244 filepath = pathUtils.join(__dirname, '..', 'api', 'fixtures', attributes.fixture)
8d309058
C
245 }
246
247 req.attach('videofile', filepath)
248 .expect(specialStatus)
249 .end(end)
250}
251
b4c5ac97 252function updateVideo (url, accessToken, id, attributes, specialStatus, end) {
7b1f49de
C
253 if (!end) {
254 end = specialStatus
255 specialStatus = 204
256 }
257
258 const path = '/api/v1/videos/' + id
259
260 const req = request(url)
261 .put(path)
262 .set('Accept', 'application/json')
263 .set('Authorization', 'Bearer ' + accessToken)
264
b4c5ac97
C
265 if (attributes.name) req.field('name', attributes.name)
266 if (attributes.category) req.field('category', attributes.category)
6f0c39e2 267 if (attributes.licence) req.field('licence', attributes.licence)
b4c5ac97 268 if (attributes.description) req.field('description', attributes.description)
7b1f49de 269
b4c5ac97
C
270 if (attributes.tags) {
271 for (let i = 0; i < attributes.tags.length; i++) {
272 req.field('tags[' + i + ']', attributes.tags[i])
7b1f49de
C
273 }
274 }
275
276 req.expect(specialStatus).end(end)
277}
278
d38b8281
C
279function rateVideo (url, accessToken, id, rating, specialStatus, end) {
280 if (!end) {
281 end = specialStatus
282 specialStatus = 204
283 }
284
285 const path = '/api/v1/videos/' + id + '/rate'
286
287 request(url)
288 .put(path)
289 .set('Accept', 'application/json')
290 .set('Authorization', 'Bearer ' + accessToken)
291 .send({ rating })
292 .expect(specialStatus)
293 .end(end)
294}
295
8d309058
C
296// ---------------------------------------------------------------------------
297
298module.exports = videosUtils