]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
0e1dc3e7 2
f6d6e7f8 3import 'mocha'
8e7f08b5 4import * as chai from 'chai'
0e1dc3e7 5import {
3cd0734f 6 checkVideoFilesWereRemoved,
7c3b7976 7 cleanupTests,
3cd0734f 8 completeVideoCheck,
254d3579
C
9 createSingleServer,
10 PeerTubeServer,
3cd0734f
C
11 setAccessTokensToServers,
12 testImage,
3cd0734f 13 wait
d23dd9fb
C
14} from '@shared/extra-utils'
15import { Video, VideoPrivacy } from '@shared/models'
0e1dc3e7 16
8e7f08b5
C
17const expect = chai.expect
18
9a27cdc2 19describe('Test a single server', function () {
0e1dc3e7 20
f6d6e7f8 21 function runSuite (mode: 'legacy' | 'resumable') {
254d3579 22 let server: PeerTubeServer = null
d23dd9fb
C
23 let videoId: number | string
24 let videoId2: string
f6d6e7f8 25 let videoUUID = ''
26 let videosListBase: any[] = null
33ff70ba 27
f6d6e7f8 28 const getCheckAttributes = () => ({
0e1dc3e7
C
29 name: 'my super name',
30 category: 2,
0e1dc3e7 31 licence: 6,
f6d6e7f8 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 })
0e1dc3e7 93
f6d6e7f8 94 before(async function () {
95 this.timeout(30000)
0e1dc3e7 96
254d3579 97 server = await createSingleServer(1)
0e1dc3e7 98
f6d6e7f8 99 await setAccessTokensToServers([ server ])
100 })
0e1dc3e7 101
f6d6e7f8 102 it('Should list video categories', async function () {
89d241a7 103 const categories = await server.videos.getCategories()
f6d6e7f8 104 expect(Object.keys(categories)).to.have.length.above(10)
b5c0e955 105
f6d6e7f8 106 expect(categories[11]).to.equal('News & Politics')
107 })
b5c0e955 108
f6d6e7f8 109 it('Should list video licences', async function () {
89d241a7 110 const licences = await server.videos.getLicences()
f6d6e7f8 111 expect(Object.keys(licences)).to.have.length.above(5)
b5c0e955 112
f6d6e7f8 113 expect(licences[3]).to.equal('Attribution - No Derivatives')
114 })
1f3e9fec 115
f6d6e7f8 116 it('Should list video languages', async function () {
89d241a7 117 const languages = await server.videos.getLanguages()
f6d6e7f8 118 expect(Object.keys(languages)).to.have.length.above(5)
0e1dc3e7 119
f6d6e7f8 120 expect(languages['ru']).to.equal('Russian')
121 })
0e1dc3e7 122
f6d6e7f8 123 it('Should list video privacies', async function () {
89d241a7 124 const privacies = await server.videos.getPrivacies()
f6d6e7f8 125 expect(Object.keys(privacies)).to.have.length.at.least(3)
0e1dc3e7 126
f6d6e7f8 127 expect(privacies[3]).to.equal('Private')
128 })
0e1dc3e7 129
f6d6e7f8 130 it('Should not have videos', async function () {
89d241a7 131 const { data, total } = await server.videos.list()
0e1dc3e7 132
d23dd9fb
C
133 expect(total).to.equal(0)
134 expect(data).to.be.an('array')
135 expect(data.length).to.equal(0)
f6d6e7f8 136 })
0e1dc3e7 137
f6d6e7f8 138 it('Should upload the video', async function () {
139 this.timeout(10000)
0e1dc3e7 140
d23dd9fb 141 const attributes = {
f6d6e7f8 142 name: 'my super name',
0e1dc3e7 143 category: 2,
0e1dc3e7 144 nsfw: true,
f6d6e7f8 145 licence: 6,
146 tags: [ 'tag1', 'tag2', 'tag3' ]
0e1dc3e7 147 }
89d241a7 148 const video = await server.videos.upload({ attributes, mode })
d23dd9fb
C
149 expect(video).to.not.be.undefined
150 expect(video.id).to.equal(1)
151 expect(video.uuid).to.have.length.above(5)
0e1dc3e7 152
d23dd9fb
C
153 videoId = video.id
154 videoUUID = video.uuid
f6d6e7f8 155 })
0e1dc3e7 156
f6d6e7f8 157 it('Should get and seed the uploaded video', async function () {
158 this.timeout(5000)
0e1dc3e7 159
89d241a7 160 const { data, total } = await server.videos.list()
0e1dc3e7 161
d23dd9fb
C
162 expect(total).to.equal(1)
163 expect(data).to.be.an('array')
164 expect(data.length).to.equal(1)
0e1dc3e7 165
d23dd9fb
C
166 const video = data[0]
167 await completeVideoCheck(server, video, getCheckAttributes())
f6d6e7f8 168 })
0e1dc3e7 169
f6d6e7f8 170 it('Should get the video by UUID', async function () {
171 this.timeout(5000)
0e1dc3e7 172
89d241a7 173 const video = await server.videos.get({ id: videoUUID })
d23dd9fb 174 await completeVideoCheck(server, video, getCheckAttributes())
f6d6e7f8 175 })
0e1dc3e7 176
f6d6e7f8 177 it('Should have the views updated', async function () {
178 this.timeout(20000)
0e1dc3e7 179
89d241a7
C
180 await server.videos.view({ id: videoId })
181 await server.videos.view({ id: videoId })
182 await server.videos.view({ id: videoId })
0e1dc3e7 183
f6d6e7f8 184 await wait(1500)
0e1dc3e7 185
89d241a7
C
186 await server.videos.view({ id: videoId })
187 await server.videos.view({ id: videoId })
fe987656 188
f6d6e7f8 189 await wait(1500)
fe987656 190
89d241a7
C
191 await server.videos.view({ id: videoId })
192 await server.videos.view({ id: videoId })
0e1dc3e7 193
f6d6e7f8 194 // Wait the repeatable job
195 await wait(8000)
0b74c74a 196
89d241a7 197 const video = await server.videos.get({ id: videoId })
f6d6e7f8 198 expect(video.views).to.equal(3)
199 })
923d3d5a 200
f6d6e7f8 201 it('Should remove the video', async function () {
89d241a7 202 await server.videos.remove({ id: videoId })
923d3d5a 203
6c5065a0 204 await checkVideoFilesWereRemoved(videoUUID, server)
f6d6e7f8 205 })
3d4e112d 206
f6d6e7f8 207 it('Should not have videos', async function () {
89d241a7 208 const { total, data } = await server.videos.list()
3d4e112d 209
d23dd9fb
C
210 expect(total).to.equal(0)
211 expect(data).to.be.an('array')
212 expect(data).to.have.lengthOf(0)
f6d6e7f8 213 })
0e1dc3e7 214
f6d6e7f8 215 it('Should upload 6 videos', async function () {
216 this.timeout(25000)
d525fc39 217
f6d6e7f8 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 ])
d525fc39 222
f6d6e7f8 223 for (const video of videos) {
d23dd9fb 224 const attributes = {
f6d6e7f8 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 }
0e1dc3e7 234
89d241a7 235 await server.videos.upload({ attributes, mode })
f6d6e7f8 236 }
237 })
238
239 it('Should have the correct durations', async function () {
89d241a7 240 const { total, data } = await server.videos.list()
d23dd9fb
C
241
242 expect(total).to.equal(6)
243 expect(data).to.be.an('array')
244 expect(data).to.have.lengthOf(6)
f6d6e7f8 245
d23dd9fb
C
246 const videosByName: { [ name: string ]: Video } = {}
247 data.forEach(v => { videosByName[v.name] = v })
f6d6e7f8 248
f6d6e7f8 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 () {
89d241a7 258 const { data } = await server.videos.list()
f6d6e7f8 259
f6d6e7f8 260 // For the next test
d23dd9fb 261 videosListBase = data
f6d6e7f8 262
d23dd9fb 263 for (const video of data) {
f6d6e7f8 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 () {
89d241a7 270 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: 'name' })
f6d6e7f8 271
d23dd9fb
C
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)
f6d6e7f8 276 })
277
278 it('Should list only the next three videos', async function () {
89d241a7 279 const { total, data } = await server.videos.list({ start: 2, count: 3, sort: 'name' })
f6d6e7f8 280
d23dd9fb
C
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)
f6d6e7f8 286 })
287
288 it('Should list the last video', async function () {
89d241a7 289 const { total, data } = await server.videos.list({ start: 5, count: 6, sort: 'name' })
f6d6e7f8 290
d23dd9fb
C
291 expect(total).to.equal(6)
292 expect(data.length).to.equal(1)
293 expect(data[0].name).to.equal(videosListBase[5].name)
f6d6e7f8 294 })
295
296 it('Should not have the total field', async function () {
89d241a7 297 const { total, data } = await server.videos.list({ start: 5, count: 6, sort: 'name', skipCount: true })
f6d6e7f8 298
d23dd9fb
C
299 expect(total).to.not.exist
300 expect(data.length).to.equal(1)
301 expect(data[0].name).to.equal(videosListBase[5].name)
f6d6e7f8 302 })
303
304 it('Should list and sort by name in descending order', async function () {
89d241a7 305 const { total, data } = await server.videos.list({ sort: '-name' })
f6d6e7f8 306
d23dd9fb
C
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')
f6d6e7f8 315
d23dd9fb
C
316 videoId = data[3].uuid
317 videoId2 = data[5].uuid
f6d6e7f8 318 })
319
320 it('Should list and sort by trending in descending order', async function () {
89d241a7 321 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-trending' })
f6d6e7f8 322
d23dd9fb
C
323 expect(total).to.equal(6)
324 expect(data.length).to.equal(2)
f6d6e7f8 325 })
326
327 it('Should list and sort by hotness in descending order', async function () {
89d241a7 328 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-hot' })
f6d6e7f8 329
d23dd9fb
C
330 expect(total).to.equal(6)
331 expect(data.length).to.equal(2)
f6d6e7f8 332 })
333
334 it('Should list and sort by best in descending order', async function () {
89d241a7 335 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-best' })
f6d6e7f8 336
d23dd9fb
C
337 expect(total).to.equal(6)
338 expect(data.length).to.equal(2)
f6d6e7f8 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 }
89d241a7 353 await server.videos.update({ id: videoId, attributes })
f6d6e7f8 354 })
0e1dc3e7 355
f6d6e7f8 356 it('Should filter by tags and category', async function () {
d23dd9fb 357 {
89d241a7 358 const { data, total } = await server.videos.list({ tagsAllOf: [ 'tagup1', 'tagup2' ], categoryOneOf: [ 4 ] })
d23dd9fb
C
359 expect(total).to.equal(1)
360 expect(data[0].name).to.equal('my super video updated')
361 }
0e1dc3e7 362
d23dd9fb 363 {
89d241a7 364 const { total } = await server.videos.list({ tagsAllOf: [ 'tagup1', 'tagup2' ], categoryOneOf: [ 3 ] })
d23dd9fb
C
365 expect(total).to.equal(0)
366 }
f6d6e7f8 367 })
0e1dc3e7 368
f6d6e7f8 369 it('Should have the video updated', async function () {
370 this.timeout(60000)
0e1dc3e7 371
89d241a7 372 const video = await server.videos.get({ id: videoId })
0e1dc3e7 373
d23dd9fb 374 await completeVideoCheck(server, video, updateCheckAttributes())
f6d6e7f8 375 })
0e1dc3e7 376
f6d6e7f8 377 it('Should update only the tags of a video', async function () {
378 const attributes = {
379 tags: [ 'supertag', 'tag1', 'tag2' ]
380 }
89d241a7 381 await server.videos.update({ id: videoId, attributes })
0e1dc3e7 382
89d241a7 383 const video = await server.videos.get({ id: videoId })
0e1dc3e7 384
d23dd9fb 385 await completeVideoCheck(server, video, Object.assign(updateCheckAttributes(), attributes))
f6d6e7f8 386 })
0e1dc3e7 387
f6d6e7f8 388 it('Should update only the description of a video', async function () {
389 const attributes = {
390 description: 'hello everybody'
391 }
89d241a7 392 await server.videos.update({ id: videoId, attributes })
0e1dc3e7 393
89d241a7 394 const video = await server.videos.get({ id: videoId })
0e1dc3e7 395
f6d6e7f8 396 const expectedAttributes = Object.assign(updateCheckAttributes(), { tags: [ 'supertag', 'tag1', 'tag2' ] }, attributes)
d23dd9fb 397 await completeVideoCheck(server, video, expectedAttributes)
f6d6e7f8 398 })
0e1dc3e7 399
f6d6e7f8 400 it('Should like a video', async function () {
89d241a7 401 await server.videos.rate({ id: videoId, rating: 'like' })
0e1dc3e7 402
89d241a7 403 const video = await server.videos.get({ id: videoId })
0e1dc3e7 404
f6d6e7f8 405 expect(video.likes).to.equal(1)
406 expect(video.dislikes).to.equal(0)
407 })
2fd59d7d 408
f6d6e7f8 409 it('Should dislike the same video', async function () {
89d241a7 410 await server.videos.rate({ id: videoId, rating: 'dislike' })
f6d6e7f8 411
89d241a7 412 const video = await server.videos.get({ id: videoId })
f6d6e7f8 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 () {
2fd59d7d
C
419 {
420 const now = new Date()
421 const attributes = { originallyPublishedAt: now.toISOString() }
89d241a7 422 await server.videos.update({ id: videoId, attributes })
2fd59d7d 423
89d241a7 424 const { data } = await server.videos.list({ sort: '-originallyPublishedAt' })
d23dd9fb 425 const names = data.map(v => v.name)
2fd59d7d
C
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() }
89d241a7 438 await server.videos.update({ id: videoId2, attributes })
2fd59d7d 439
89d241a7 440 const { data } = await server.videos.list({ sort: '-originallyPublishedAt' })
d23dd9fb 441 const names = data.map(v => v.name)
2fd59d7d
C
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 }
f6d6e7f8 450 })
451
452 after(async function () {
453 await cleanupTests([ server ])
454 })
455 }
456
457 describe('Legacy upload', function () {
458 runSuite('legacy')
2fd59d7d
C
459 })
460
f6d6e7f8 461 describe('Resumable upload', function () {
462 runSuite('resumable')
0e1dc3e7
C
463 })
464})