]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/tests/api/videos/single-server.ts
Generate random uuid for video files
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / single-server.ts
... / ...
CommitLineData
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import 'mocha'
4import * as chai from 'chai'
5import {
6 checkVideoFilesWereRemoved,
7 cleanupTests,
8 completeVideoCheck,
9 createSingleServer,
10 PeerTubeServer,
11 setAccessTokensToServers,
12 testImage,
13 wait
14} from '@shared/extra-utils'
15import { Video, VideoPrivacy } from '@shared/models'
16
17const expect = chai.expect
18
19describe('Test a single server', function () {
20
21 function runSuite (mode: 'legacy' | 'resumable') {
22 let server: PeerTubeServer = null
23 let videoId: number | string
24 let videoId2: string
25 let videoUUID = ''
26 let videosListBase: any[] = null
27
28 const getCheckAttributes = () => ({
29 name: 'my super name',
30 category: 2,
31 licence: 6,
32 language: 'zh',
33 nsfw: true,
34 description: 'my super description',
35 support: 'my super support text',
36 account: {
37 name: 'root',
38 host: 'localhost:' + server.port
39 },
40 isLocal: true,
41 duration: 5,
42 tags: [ 'tag1', 'tag2', 'tag3' ],
43 privacy: VideoPrivacy.PUBLIC,
44 commentsEnabled: true,
45 downloadEnabled: true,
46 channel: {
47 displayName: 'Main root channel',
48 name: 'root_channel',
49 description: '',
50 isLocal: true
51 },
52 fixture: 'video_short.webm',
53 files: [
54 {
55 resolution: 720,
56 size: 218910
57 }
58 ]
59 })
60
61 const updateCheckAttributes = () => ({
62 name: 'my super video updated',
63 category: 4,
64 licence: 2,
65 language: 'ar',
66 nsfw: false,
67 description: 'my super description updated',
68 support: 'my super support text updated',
69 account: {
70 name: 'root',
71 host: 'localhost:' + server.port
72 },
73 isLocal: true,
74 tags: [ 'tagup1', 'tagup2' ],
75 privacy: VideoPrivacy.PUBLIC,
76 duration: 5,
77 commentsEnabled: false,
78 downloadEnabled: false,
79 channel: {
80 name: 'root_channel',
81 displayName: 'Main root channel',
82 description: '',
83 isLocal: true
84 },
85 fixture: 'video_short3.webm',
86 files: [
87 {
88 resolution: 720,
89 size: 292677
90 }
91 ]
92 })
93
94 before(async function () {
95 this.timeout(30000)
96
97 server = await createSingleServer(1)
98
99 await setAccessTokensToServers([ server ])
100 })
101
102 it('Should list video categories', async function () {
103 const categories = await server.videos.getCategories()
104 expect(Object.keys(categories)).to.have.length.above(10)
105
106 expect(categories[11]).to.equal('News & Politics')
107 })
108
109 it('Should list video licences', async function () {
110 const licences = await server.videos.getLicences()
111 expect(Object.keys(licences)).to.have.length.above(5)
112
113 expect(licences[3]).to.equal('Attribution - No Derivatives')
114 })
115
116 it('Should list video languages', async function () {
117 const languages = await server.videos.getLanguages()
118 expect(Object.keys(languages)).to.have.length.above(5)
119
120 expect(languages['ru']).to.equal('Russian')
121 })
122
123 it('Should list video privacies', async function () {
124 const privacies = await server.videos.getPrivacies()
125 expect(Object.keys(privacies)).to.have.length.at.least(3)
126
127 expect(privacies[3]).to.equal('Private')
128 })
129
130 it('Should not have videos', async function () {
131 const { data, total } = await server.videos.list()
132
133 expect(total).to.equal(0)
134 expect(data).to.be.an('array')
135 expect(data.length).to.equal(0)
136 })
137
138 it('Should upload the video', async function () {
139 this.timeout(10000)
140
141 const attributes = {
142 name: 'my super name',
143 category: 2,
144 nsfw: true,
145 licence: 6,
146 tags: [ 'tag1', 'tag2', 'tag3' ]
147 }
148 const video = await server.videos.upload({ attributes, mode })
149 expect(video).to.not.be.undefined
150 expect(video.id).to.equal(1)
151 expect(video.uuid).to.have.length.above(5)
152
153 videoId = video.id
154 videoUUID = video.uuid
155 })
156
157 it('Should get and seed the uploaded video', async function () {
158 this.timeout(5000)
159
160 const { data, total } = await server.videos.list()
161
162 expect(total).to.equal(1)
163 expect(data).to.be.an('array')
164 expect(data.length).to.equal(1)
165
166 const video = data[0]
167 await completeVideoCheck(server, video, getCheckAttributes())
168 })
169
170 it('Should get the video by UUID', async function () {
171 this.timeout(5000)
172
173 const video = await server.videos.get({ id: videoUUID })
174 await completeVideoCheck(server, video, getCheckAttributes())
175 })
176
177 it('Should have the views updated', async function () {
178 this.timeout(20000)
179
180 await server.videos.view({ id: videoId })
181 await server.videos.view({ id: videoId })
182 await server.videos.view({ id: videoId })
183
184 await wait(1500)
185
186 await server.videos.view({ id: videoId })
187 await server.videos.view({ id: videoId })
188
189 await wait(1500)
190
191 await server.videos.view({ id: videoId })
192 await server.videos.view({ id: videoId })
193
194 // Wait the repeatable job
195 await wait(8000)
196
197 const video = await server.videos.get({ id: videoId })
198 expect(video.views).to.equal(3)
199 })
200
201 it('Should remove the video', async function () {
202 const video = await server.videos.get({ id: videoId })
203 await server.videos.remove({ id: videoId })
204
205 await checkVideoFilesWereRemoved({ video, server })
206 })
207
208 it('Should not have videos', async function () {
209 const { total, data } = await server.videos.list()
210
211 expect(total).to.equal(0)
212 expect(data).to.be.an('array')
213 expect(data).to.have.lengthOf(0)
214 })
215
216 it('Should upload 6 videos', async function () {
217 this.timeout(25000)
218
219 const videos = new Set([
220 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
221 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
222 ])
223
224 for (const video of videos) {
225 const attributes = {
226 name: video + ' name',
227 description: video + ' description',
228 category: 2,
229 licence: 1,
230 language: 'en',
231 nsfw: true,
232 tags: [ 'tag1', 'tag2', 'tag3' ],
233 fixture: video
234 }
235
236 await server.videos.upload({ attributes, mode })
237 }
238 })
239
240 it('Should have the correct durations', async function () {
241 const { total, data } = await server.videos.list()
242
243 expect(total).to.equal(6)
244 expect(data).to.be.an('array')
245 expect(data).to.have.lengthOf(6)
246
247 const videosByName: { [ name: string ]: Video } = {}
248 data.forEach(v => { videosByName[v.name] = v })
249
250 expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
251 expect(videosByName['video_short.ogv name'].duration).to.equal(5)
252 expect(videosByName['video_short.webm name'].duration).to.equal(5)
253 expect(videosByName['video_short1.webm name'].duration).to.equal(10)
254 expect(videosByName['video_short2.webm name'].duration).to.equal(5)
255 expect(videosByName['video_short3.webm name'].duration).to.equal(5)
256 })
257
258 it('Should have the correct thumbnails', async function () {
259 const { data } = await server.videos.list()
260
261 // For the next test
262 videosListBase = data
263
264 for (const video of data) {
265 const videoName = video.name.replace(' name', '')
266 await testImage(server.url, videoName, video.thumbnailPath)
267 }
268 })
269
270 it('Should list only the two first videos', async function () {
271 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: 'name' })
272
273 expect(total).to.equal(6)
274 expect(data.length).to.equal(2)
275 expect(data[0].name).to.equal(videosListBase[0].name)
276 expect(data[1].name).to.equal(videosListBase[1].name)
277 })
278
279 it('Should list only the next three videos', async function () {
280 const { total, data } = await server.videos.list({ start: 2, count: 3, sort: 'name' })
281
282 expect(total).to.equal(6)
283 expect(data.length).to.equal(3)
284 expect(data[0].name).to.equal(videosListBase[2].name)
285 expect(data[1].name).to.equal(videosListBase[3].name)
286 expect(data[2].name).to.equal(videosListBase[4].name)
287 })
288
289 it('Should list the last video', async function () {
290 const { total, data } = await server.videos.list({ start: 5, count: 6, sort: 'name' })
291
292 expect(total).to.equal(6)
293 expect(data.length).to.equal(1)
294 expect(data[0].name).to.equal(videosListBase[5].name)
295 })
296
297 it('Should not have the total field', async function () {
298 const { total, data } = await server.videos.list({ start: 5, count: 6, sort: 'name', skipCount: true })
299
300 expect(total).to.not.exist
301 expect(data.length).to.equal(1)
302 expect(data[0].name).to.equal(videosListBase[5].name)
303 })
304
305 it('Should list and sort by name in descending order', async function () {
306 const { total, data } = await server.videos.list({ sort: '-name' })
307
308 expect(total).to.equal(6)
309 expect(data.length).to.equal(6)
310 expect(data[0].name).to.equal('video_short.webm name')
311 expect(data[1].name).to.equal('video_short.ogv name')
312 expect(data[2].name).to.equal('video_short.mp4 name')
313 expect(data[3].name).to.equal('video_short3.webm name')
314 expect(data[4].name).to.equal('video_short2.webm name')
315 expect(data[5].name).to.equal('video_short1.webm name')
316
317 videoId = data[3].uuid
318 videoId2 = data[5].uuid
319 })
320
321 it('Should list and sort by trending in descending order', async function () {
322 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-trending' })
323
324 expect(total).to.equal(6)
325 expect(data.length).to.equal(2)
326 })
327
328 it('Should list and sort by hotness in descending order', async function () {
329 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-hot' })
330
331 expect(total).to.equal(6)
332 expect(data.length).to.equal(2)
333 })
334
335 it('Should list and sort by best in descending order', async function () {
336 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-best' })
337
338 expect(total).to.equal(6)
339 expect(data.length).to.equal(2)
340 })
341
342 it('Should update a video', async function () {
343 const attributes = {
344 name: 'my super video updated',
345 category: 4,
346 licence: 2,
347 language: 'ar',
348 nsfw: false,
349 description: 'my super description updated',
350 commentsEnabled: false,
351 downloadEnabled: false,
352 tags: [ 'tagup1', 'tagup2' ]
353 }
354 await server.videos.update({ id: videoId, attributes })
355 })
356
357 it('Should filter by tags and category', async function () {
358 {
359 const { data, total } = await server.videos.list({ tagsAllOf: [ 'tagup1', 'tagup2' ], categoryOneOf: [ 4 ] })
360 expect(total).to.equal(1)
361 expect(data[0].name).to.equal('my super video updated')
362 }
363
364 {
365 const { total } = await server.videos.list({ tagsAllOf: [ 'tagup1', 'tagup2' ], categoryOneOf: [ 3 ] })
366 expect(total).to.equal(0)
367 }
368 })
369
370 it('Should have the video updated', async function () {
371 this.timeout(60000)
372
373 const video = await server.videos.get({ id: videoId })
374
375 await completeVideoCheck(server, video, updateCheckAttributes())
376 })
377
378 it('Should update only the tags of a video', async function () {
379 const attributes = {
380 tags: [ 'supertag', 'tag1', 'tag2' ]
381 }
382 await server.videos.update({ id: videoId, attributes })
383
384 const video = await server.videos.get({ id: videoId })
385
386 await completeVideoCheck(server, video, Object.assign(updateCheckAttributes(), attributes))
387 })
388
389 it('Should update only the description of a video', async function () {
390 const attributes = {
391 description: 'hello everybody'
392 }
393 await server.videos.update({ id: videoId, attributes })
394
395 const video = await server.videos.get({ id: videoId })
396
397 const expectedAttributes = Object.assign(updateCheckAttributes(), { tags: [ 'supertag', 'tag1', 'tag2' ] }, attributes)
398 await completeVideoCheck(server, video, expectedAttributes)
399 })
400
401 it('Should like a video', async function () {
402 await server.videos.rate({ id: videoId, rating: 'like' })
403
404 const video = await server.videos.get({ id: videoId })
405
406 expect(video.likes).to.equal(1)
407 expect(video.dislikes).to.equal(0)
408 })
409
410 it('Should dislike the same video', async function () {
411 await server.videos.rate({ id: videoId, rating: 'dislike' })
412
413 const video = await server.videos.get({ id: videoId })
414
415 expect(video.likes).to.equal(0)
416 expect(video.dislikes).to.equal(1)
417 })
418
419 it('Should sort by originallyPublishedAt', async function () {
420 {
421 const now = new Date()
422 const attributes = { originallyPublishedAt: now.toISOString() }
423 await server.videos.update({ id: videoId, attributes })
424
425 const { data } = await server.videos.list({ sort: '-originallyPublishedAt' })
426 const names = data.map(v => v.name)
427
428 expect(names[0]).to.equal('my super video updated')
429 expect(names[1]).to.equal('video_short2.webm name')
430 expect(names[2]).to.equal('video_short1.webm name')
431 expect(names[3]).to.equal('video_short.webm name')
432 expect(names[4]).to.equal('video_short.ogv name')
433 expect(names[5]).to.equal('video_short.mp4 name')
434 }
435
436 {
437 const now = new Date()
438 const attributes = { originallyPublishedAt: now.toISOString() }
439 await server.videos.update({ id: videoId2, attributes })
440
441 const { data } = await server.videos.list({ sort: '-originallyPublishedAt' })
442 const names = data.map(v => v.name)
443
444 expect(names[0]).to.equal('video_short1.webm name')
445 expect(names[1]).to.equal('my super video updated')
446 expect(names[2]).to.equal('video_short2.webm name')
447 expect(names[3]).to.equal('video_short.webm name')
448 expect(names[4]).to.equal('video_short.ogv name')
449 expect(names[5]).to.equal('video_short.mp4 name')
450 }
451 })
452
453 after(async function () {
454 await cleanupTests([ server ])
455 })
456 }
457
458 describe('Legacy upload', function () {
459 runSuite('legacy')
460 })
461
462 describe('Resumable upload', function () {
463 runSuite('resumable')
464 })
465})