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