]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/single-server.ts
Merge branch 'release/3.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / single-server.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import {
6 checkVideoFilesWereRemoved,
7 cleanupTests,
8 completeVideoCheck,
9 createSingleServer,
10 PeerTubeServer,
11 setAccessTokensToServers,
12 testImage,
13 wait
14 } from '@shared/extra-utils'
15 import { Video, VideoPrivacy } from '@shared/models'
16
17 const expect = chai.expect
18
19 describe('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 await server.videos.remove({ id: videoId })
203
204 await checkVideoFilesWereRemoved(videoUUID, server)
205 })
206
207 it('Should not have videos', async function () {
208 const { total, data } = await server.videos.list()
209
210 expect(total).to.equal(0)
211 expect(data).to.be.an('array')
212 expect(data).to.have.lengthOf(0)
213 })
214
215 it('Should upload 6 videos', async function () {
216 this.timeout(25000)
217
218 const videos = new Set([
219 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
220 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
221 ])
222
223 for (const video of videos) {
224 const attributes = {
225 name: video + ' name',
226 description: video + ' description',
227 category: 2,
228 licence: 1,
229 language: 'en',
230 nsfw: true,
231 tags: [ 'tag1', 'tag2', 'tag3' ],
232 fixture: video
233 }
234
235 await server.videos.upload({ attributes, mode })
236 }
237 })
238
239 it('Should have the correct durations', async function () {
240 const { total, data } = await server.videos.list()
241
242 expect(total).to.equal(6)
243 expect(data).to.be.an('array')
244 expect(data).to.have.lengthOf(6)
245
246 const videosByName: { [ name: string ]: Video } = {}
247 data.forEach(v => { videosByName[v.name] = v })
248
249 expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
250 expect(videosByName['video_short.ogv name'].duration).to.equal(5)
251 expect(videosByName['video_short.webm name'].duration).to.equal(5)
252 expect(videosByName['video_short1.webm name'].duration).to.equal(10)
253 expect(videosByName['video_short2.webm name'].duration).to.equal(5)
254 expect(videosByName['video_short3.webm name'].duration).to.equal(5)
255 })
256
257 it('Should have the correct thumbnails', async function () {
258 const { data } = await server.videos.list()
259
260 // For the next test
261 videosListBase = data
262
263 for (const video of data) {
264 const videoName = video.name.replace(' name', '')
265 await testImage(server.url, videoName, video.thumbnailPath)
266 }
267 })
268
269 it('Should list only the two first videos', async function () {
270 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: 'name' })
271
272 expect(total).to.equal(6)
273 expect(data.length).to.equal(2)
274 expect(data[0].name).to.equal(videosListBase[0].name)
275 expect(data[1].name).to.equal(videosListBase[1].name)
276 })
277
278 it('Should list only the next three videos', async function () {
279 const { total, data } = await server.videos.list({ start: 2, count: 3, sort: 'name' })
280
281 expect(total).to.equal(6)
282 expect(data.length).to.equal(3)
283 expect(data[0].name).to.equal(videosListBase[2].name)
284 expect(data[1].name).to.equal(videosListBase[3].name)
285 expect(data[2].name).to.equal(videosListBase[4].name)
286 })
287
288 it('Should list the last video', async function () {
289 const { total, data } = await server.videos.list({ start: 5, count: 6, sort: 'name' })
290
291 expect(total).to.equal(6)
292 expect(data.length).to.equal(1)
293 expect(data[0].name).to.equal(videosListBase[5].name)
294 })
295
296 it('Should not have the total field', async function () {
297 const { total, data } = await server.videos.list({ start: 5, count: 6, sort: 'name', skipCount: true })
298
299 expect(total).to.not.exist
300 expect(data.length).to.equal(1)
301 expect(data[0].name).to.equal(videosListBase[5].name)
302 })
303
304 it('Should list and sort by name in descending order', async function () {
305 const { total, data } = await server.videos.list({ sort: '-name' })
306
307 expect(total).to.equal(6)
308 expect(data.length).to.equal(6)
309 expect(data[0].name).to.equal('video_short.webm name')
310 expect(data[1].name).to.equal('video_short.ogv name')
311 expect(data[2].name).to.equal('video_short.mp4 name')
312 expect(data[3].name).to.equal('video_short3.webm name')
313 expect(data[4].name).to.equal('video_short2.webm name')
314 expect(data[5].name).to.equal('video_short1.webm name')
315
316 videoId = data[3].uuid
317 videoId2 = data[5].uuid
318 })
319
320 it('Should list and sort by trending in descending order', async function () {
321 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-trending' })
322
323 expect(total).to.equal(6)
324 expect(data.length).to.equal(2)
325 })
326
327 it('Should list and sort by hotness in descending order', async function () {
328 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-hot' })
329
330 expect(total).to.equal(6)
331 expect(data.length).to.equal(2)
332 })
333
334 it('Should list and sort by best in descending order', async function () {
335 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-best' })
336
337 expect(total).to.equal(6)
338 expect(data.length).to.equal(2)
339 })
340
341 it('Should update a video', async function () {
342 const attributes = {
343 name: 'my super video updated',
344 category: 4,
345 licence: 2,
346 language: 'ar',
347 nsfw: false,
348 description: 'my super description updated',
349 commentsEnabled: false,
350 downloadEnabled: false,
351 tags: [ 'tagup1', 'tagup2' ]
352 }
353 await server.videos.update({ id: videoId, attributes })
354 })
355
356 it('Should filter by tags and category', async function () {
357 {
358 const { data, total } = await server.videos.list({ tagsAllOf: [ 'tagup1', 'tagup2' ], categoryOneOf: [ 4 ] })
359 expect(total).to.equal(1)
360 expect(data[0].name).to.equal('my super video updated')
361 }
362
363 {
364 const { total } = await server.videos.list({ tagsAllOf: [ 'tagup1', 'tagup2' ], categoryOneOf: [ 3 ] })
365 expect(total).to.equal(0)
366 }
367 })
368
369 it('Should have the video updated', async function () {
370 this.timeout(60000)
371
372 const video = await server.videos.get({ id: videoId })
373
374 await completeVideoCheck(server, video, updateCheckAttributes())
375 })
376
377 it('Should update only the tags of a video', async function () {
378 const attributes = {
379 tags: [ 'supertag', 'tag1', 'tag2' ]
380 }
381 await server.videos.update({ id: videoId, attributes })
382
383 const video = await server.videos.get({ id: videoId })
384
385 await completeVideoCheck(server, video, Object.assign(updateCheckAttributes(), attributes))
386 })
387
388 it('Should update only the description of a video', async function () {
389 const attributes = {
390 description: 'hello everybody'
391 }
392 await server.videos.update({ id: videoId, attributes })
393
394 const video = await server.videos.get({ id: videoId })
395
396 const expectedAttributes = Object.assign(updateCheckAttributes(), { tags: [ 'supertag', 'tag1', 'tag2' ] }, attributes)
397 await completeVideoCheck(server, video, expectedAttributes)
398 })
399
400 it('Should like a video', async function () {
401 await server.videos.rate({ id: videoId, rating: 'like' })
402
403 const video = await server.videos.get({ id: videoId })
404
405 expect(video.likes).to.equal(1)
406 expect(video.dislikes).to.equal(0)
407 })
408
409 it('Should dislike the same video', async function () {
410 await server.videos.rate({ id: videoId, rating: 'dislike' })
411
412 const video = await server.videos.get({ id: videoId })
413
414 expect(video.likes).to.equal(0)
415 expect(video.dislikes).to.equal(1)
416 })
417
418 it('Should sort by originallyPublishedAt', async function () {
419 {
420 const now = new Date()
421 const attributes = { originallyPublishedAt: now.toISOString() }
422 await server.videos.update({ id: videoId, attributes })
423
424 const { data } = await server.videos.list({ sort: '-originallyPublishedAt' })
425 const names = data.map(v => v.name)
426
427 expect(names[0]).to.equal('my super video updated')
428 expect(names[1]).to.equal('video_short2.webm name')
429 expect(names[2]).to.equal('video_short1.webm name')
430 expect(names[3]).to.equal('video_short.webm name')
431 expect(names[4]).to.equal('video_short.ogv name')
432 expect(names[5]).to.equal('video_short.mp4 name')
433 }
434
435 {
436 const now = new Date()
437 const attributes = { originallyPublishedAt: now.toISOString() }
438 await server.videos.update({ id: videoId2, attributes })
439
440 const { data } = await server.videos.list({ sort: '-originallyPublishedAt' })
441 const names = data.map(v => v.name)
442
443 expect(names[0]).to.equal('video_short1.webm name')
444 expect(names[1]).to.equal('my super video updated')
445 expect(names[2]).to.equal('video_short2.webm name')
446 expect(names[3]).to.equal('video_short.webm name')
447 expect(names[4]).to.equal('video_short.ogv name')
448 expect(names[5]).to.equal('video_short.mp4 name')
449 }
450 })
451
452 after(async function () {
453 await cleanupTests([ server ])
454 })
455 }
456
457 describe('Legacy upload', function () {
458 runSuite('legacy')
459 })
460
461 describe('Resumable upload', function () {
462 runSuite('resumable')
463 })
464 })