]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/utils/videos/videos.ts
Improve check services parameters tests
[github/Chocobozzz/PeerTube.git] / server / tests / utils / videos / videos.ts
1 import { readFile } from 'fs'
2 import * as parseTorrent from 'parse-torrent'
3 import { isAbsolute, join } from 'path'
4 import * as request from 'supertest'
5 import { getMyUserInformation, makeGetRequest, readFilePromise, ServerInfo } from '../'
6 import { VideoPrivacy } from '../../../../shared/models/videos'
7
8 type VideoAttributes = {
9 name?: string
10 category?: number
11 licence?: number
12 language?: number
13 nsfw?: boolean
14 description?: string
15 tags?: string[]
16 channelId?: number
17 privacy?: VideoPrivacy
18 fixture?: string
19 }
20
21 function getVideoCategories (url: string) {
22 const path = '/api/v1/videos/categories'
23
24 return makeGetRequest({
25 url,
26 path
27 })
28 }
29
30 function getVideoLicences (url: string) {
31 const path = '/api/v1/videos/licences'
32
33 return makeGetRequest({
34 url,
35 path
36 })
37 }
38
39 function getVideoLanguages (url: string) {
40 const path = '/api/v1/videos/languages'
41
42 return makeGetRequest({
43 url,
44 path
45 })
46 }
47
48 function getVideoPrivacies (url: string) {
49 const path = '/api/v1/videos/privacies'
50
51 return makeGetRequest({
52 url,
53 path
54 })
55 }
56
57 function getVideo (url: string, id: number | string, expectedStatus = 200) {
58 const path = '/api/v1/videos/' + id
59
60 return request(url)
61 .get(path)
62 .set('Accept', 'application/json')
63 .expect(expectedStatus)
64 }
65
66 function viewVideo (url: string, id: number | string, expectedStatus = 204) {
67 const path = '/api/v1/videos/' + id + '/views'
68
69 return request(url)
70 .post(path)
71 .set('Accept', 'application/json')
72 .expect(expectedStatus)
73 }
74
75 function getVideoWithToken (url: string, token: string, id: number | string, expectedStatus = 200) {
76 const path = '/api/v1/videos/' + id
77
78 return request(url)
79 .get(path)
80 .set('Authorization', 'Bearer ' + token)
81 .set('Accept', 'application/json')
82 .expect(expectedStatus)
83 }
84
85 function getVideoDescription (url: string, descriptionPath: string) {
86 return request(url)
87 .get(descriptionPath)
88 .set('Accept', 'application/json')
89 .expect(200)
90 .expect('Content-Type', /json/)
91 }
92
93 function getVideosList (url: string) {
94 const path = '/api/v1/videos'
95
96 return request(url)
97 .get(path)
98 .query({ sort: 'name' })
99 .set('Accept', 'application/json')
100 .expect(200)
101 .expect('Content-Type', /json/)
102 }
103
104 function getMyVideos (url: string, accessToken: string, start: number, count: number, sort?: string) {
105 const path = '/api/v1/users/me/videos'
106
107 const req = request(url)
108 .get(path)
109 .query({ start: start })
110 .query({ count: count })
111
112 if (sort) req.query({ sort })
113
114 return req.set('Accept', 'application/json')
115 .set('Authorization', 'Bearer ' + accessToken)
116 .expect(200)
117 .expect('Content-Type', /json/)
118 }
119
120 function getVideosListPagination (url: string, start: number, count: number, sort?: string) {
121 const path = '/api/v1/videos'
122
123 const req = request(url)
124 .get(path)
125 .query({ start: start })
126 .query({ count: count })
127
128 if (sort) req.query({ sort })
129
130 return req.set('Accept', 'application/json')
131 .expect(200)
132 .expect('Content-Type', /json/)
133 }
134
135 function getVideosListSort (url: string, sort: string) {
136 const path = '/api/v1/videos'
137
138 return request(url)
139 .get(path)
140 .query({ sort: sort })
141 .set('Accept', 'application/json')
142 .expect(200)
143 .expect('Content-Type', /json/)
144 }
145
146 function removeVideo (url: string, token: string, id: number, expectedStatus = 204) {
147 const path = '/api/v1/videos'
148
149 return request(url)
150 .delete(path + '/' + id)
151 .set('Accept', 'application/json')
152 .set('Authorization', 'Bearer ' + token)
153 .expect(expectedStatus)
154 }
155
156 function searchVideo (url: string, search: string) {
157 const path = '/api/v1/videos'
158 const req = request(url)
159 .get(path + '/search')
160 .query({ search })
161 .set('Accept', 'application/json')
162
163 return req.expect(200)
164 .expect('Content-Type', /json/)
165 }
166
167 function searchVideoWithPagination (url: string, search: string, start: number, count: number, sort?: string) {
168 const path = '/api/v1/videos'
169
170 const req = request(url)
171 .get(path + '/search')
172 .query({ start })
173 .query({ search })
174 .query({ count })
175
176 if (sort) req.query({ sort })
177
178 return req.set('Accept', 'application/json')
179 .expect(200)
180 .expect('Content-Type', /json/)
181 }
182
183 function searchVideoWithSort (url: string, search: string, sort: string) {
184 const path = '/api/v1/videos'
185
186 return request(url)
187 .get(path + '/search')
188 .query({ search })
189 .query({ sort })
190 .set('Accept', 'application/json')
191 .expect(200)
192 .expect('Content-Type', /json/)
193 }
194
195 async function testVideoImage (url: string, imageName: string, imagePath: string) {
196 // Don't test images if the node env is not set
197 // Because we need a special ffmpeg version for this test
198 if (process.env['NODE_TEST_IMAGE']) {
199 const res = await request(url)
200 .get(imagePath)
201 .expect(200)
202
203 const data = await readFilePromise(join(__dirname, '..', '..', 'api', 'fixtures', imageName + '.jpg'))
204
205 return data.equals(res.body)
206 } else {
207 console.log('Do not test images. Enable it by setting NODE_TEST_IMAGE env variable.')
208 return true
209 }
210 }
211
212 async function uploadVideo (url: string, accessToken: string, videoAttributesArg: VideoAttributes, specialStatus = 200) {
213 const path = '/api/v1/videos/upload'
214 let defaultChannelId = '1'
215
216 try {
217 const res = await getMyUserInformation(url, accessToken)
218 defaultChannelId = res.body.videoChannels[0].id
219 } catch (e) { /* empty */ }
220
221 // Default attributes
222 let attributes = {
223 name: 'my super video',
224 category: 5,
225 licence: 4,
226 language: 3,
227 channelId: defaultChannelId,
228 nsfw: true,
229 description: 'my super description',
230 tags: [ 'tag' ],
231 privacy: VideoPrivacy.PUBLIC,
232 fixture: 'video_short.webm'
233 }
234 attributes = Object.assign(attributes, videoAttributesArg)
235
236 const req = request(url)
237 .post(path)
238 .set('Accept', 'application/json')
239 .set('Authorization', 'Bearer ' + accessToken)
240 .field('name', attributes.name)
241 .field('category', attributes.category.toString())
242 .field('licence', attributes.licence.toString())
243 .field('nsfw', JSON.stringify(attributes.nsfw))
244 .field('description', attributes.description)
245 .field('privacy', attributes.privacy.toString())
246 .field('channelId', attributes.channelId)
247
248 if (attributes.language !== undefined) {
249 req.field('language', attributes.language.toString())
250 }
251
252 for (let i = 0; i < attributes.tags.length; i++) {
253 req.field('tags[' + i + ']', attributes.tags[i])
254 }
255
256 let filePath = ''
257 if (isAbsolute(attributes.fixture)) {
258 filePath = attributes.fixture
259 } else {
260 filePath = join(__dirname, '..', '..', 'api', 'fixtures', attributes.fixture)
261 }
262
263 return req.attach('videofile', filePath)
264 .expect(specialStatus)
265 }
266
267 function updateVideo (url: string, accessToken: string, id: number, attributes: VideoAttributes, specialStatus = 204) {
268 const path = '/api/v1/videos/' + id
269 const body = {}
270
271 if (attributes.name) body['name'] = attributes.name
272 if (attributes.category) body['category'] = attributes.category
273 if (attributes.licence) body['licence'] = attributes.licence
274 if (attributes.language) body['language'] = attributes.language
275 if (attributes.nsfw) body['nsfw'] = attributes.nsfw
276 if (attributes.description) body['description'] = attributes.description
277 if (attributes.tags) body['tags'] = attributes.tags
278 if (attributes.privacy) body['privacy'] = attributes.privacy
279
280 return request(url)
281 .put(path)
282 .send(body)
283 .set('Accept', 'application/json')
284 .set('Authorization', 'Bearer ' + accessToken)
285 .expect(specialStatus)
286 }
287
288 function rateVideo (url: string, accessToken: string, id: number, rating: string, specialStatus = 204) {
289 const path = '/api/v1/videos/' + id + '/rate'
290
291 return request(url)
292 .put(path)
293 .set('Accept', 'application/json')
294 .set('Authorization', 'Bearer ' + accessToken)
295 .send({ rating })
296 .expect(specialStatus)
297 }
298
299 function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
300 return new Promise<any>((res, rej) => {
301 const torrentName = videoUUID + '-' + resolution + '.torrent'
302 const torrentPath = join(__dirname, '..', '..', '..', '..', 'test' + server.serverNumber, 'torrents', torrentName)
303 readFile(torrentPath, (err, data) => {
304 if (err) return rej(err)
305
306 return res(parseTorrent(data))
307 })
308 })
309 }
310
311 // ---------------------------------------------------------------------------
312
313 export {
314 getVideoDescription,
315 getVideoCategories,
316 getVideoLicences,
317 getVideoPrivacies,
318 getVideoLanguages,
319 getMyVideos,
320 getVideo,
321 getVideoWithToken,
322 getVideosList,
323 getVideosListPagination,
324 getVideosListSort,
325 removeVideo,
326 searchVideo,
327 searchVideoWithPagination,
328 searchVideoWithSort,
329 testVideoImage,
330 uploadVideo,
331 updateVideo,
332 rateVideo,
333 viewVideo,
334 parseTorrentVideo
335 }