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