]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/single-server.ts
Fix getting avatars in videos list
[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'
c55e3d72
C
5import { checkVideoFilesWereRemoved, completeVideoCheck, testImage } from '@server/tests/shared'
6import { wait } from '@shared/core-utils'
d23dd9fb 7import { Video, VideoPrivacy } from '@shared/models'
d0800f76 8import {
9 cleanupTests,
10 createSingleServer,
11 PeerTubeServer,
12 setAccessTokensToServers,
13 setDefaultAccountAvatar,
14 setDefaultChannelAvatar
15} from '@shared/server-commands'
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 ])
d0800f76 100 await setDefaultChannelAvatar(server)
101 await setDefaultAccountAvatar(server)
f6d6e7f8 102 })
0e1dc3e7 103
f6d6e7f8 104 it('Should list video categories', async function () {
89d241a7 105 const categories = await server.videos.getCategories()
f6d6e7f8 106 expect(Object.keys(categories)).to.have.length.above(10)
b5c0e955 107
f6d6e7f8 108 expect(categories[11]).to.equal('News & Politics')
109 })
b5c0e955 110
f6d6e7f8 111 it('Should list video licences', async function () {
89d241a7 112 const licences = await server.videos.getLicences()
f6d6e7f8 113 expect(Object.keys(licences)).to.have.length.above(5)
b5c0e955 114
f6d6e7f8 115 expect(licences[3]).to.equal('Attribution - No Derivatives')
116 })
1f3e9fec 117
f6d6e7f8 118 it('Should list video languages', async function () {
89d241a7 119 const languages = await server.videos.getLanguages()
f6d6e7f8 120 expect(Object.keys(languages)).to.have.length.above(5)
0e1dc3e7 121
f6d6e7f8 122 expect(languages['ru']).to.equal('Russian')
123 })
0e1dc3e7 124
f6d6e7f8 125 it('Should list video privacies', async function () {
89d241a7 126 const privacies = await server.videos.getPrivacies()
f6d6e7f8 127 expect(Object.keys(privacies)).to.have.length.at.least(3)
0e1dc3e7 128
f6d6e7f8 129 expect(privacies[3]).to.equal('Private')
130 })
0e1dc3e7 131
f6d6e7f8 132 it('Should not have videos', async function () {
89d241a7 133 const { data, total } = await server.videos.list()
0e1dc3e7 134
d23dd9fb
C
135 expect(total).to.equal(0)
136 expect(data).to.be.an('array')
137 expect(data.length).to.equal(0)
f6d6e7f8 138 })
0e1dc3e7 139
f6d6e7f8 140 it('Should upload the video', async function () {
141 this.timeout(10000)
0e1dc3e7 142
d23dd9fb 143 const attributes = {
f6d6e7f8 144 name: 'my super name',
0e1dc3e7 145 category: 2,
0e1dc3e7 146 nsfw: true,
f6d6e7f8 147 licence: 6,
148 tags: [ 'tag1', 'tag2', 'tag3' ]
0e1dc3e7 149 }
89d241a7 150 const video = await server.videos.upload({ attributes, mode })
d23dd9fb
C
151 expect(video).to.not.be.undefined
152 expect(video.id).to.equal(1)
153 expect(video.uuid).to.have.length.above(5)
0e1dc3e7 154
d23dd9fb
C
155 videoId = video.id
156 videoUUID = video.uuid
f6d6e7f8 157 })
0e1dc3e7 158
f6d6e7f8 159 it('Should get and seed the uploaded video', async function () {
160 this.timeout(5000)
0e1dc3e7 161
89d241a7 162 const { data, total } = await server.videos.list()
0e1dc3e7 163
d23dd9fb
C
164 expect(total).to.equal(1)
165 expect(data).to.be.an('array')
166 expect(data.length).to.equal(1)
0e1dc3e7 167
d23dd9fb
C
168 const video = data[0]
169 await completeVideoCheck(server, video, getCheckAttributes())
f6d6e7f8 170 })
0e1dc3e7 171
f6d6e7f8 172 it('Should get the video by UUID', async function () {
173 this.timeout(5000)
0e1dc3e7 174
89d241a7 175 const video = await server.videos.get({ id: videoUUID })
d23dd9fb 176 await completeVideoCheck(server, video, getCheckAttributes())
f6d6e7f8 177 })
0e1dc3e7 178
f6d6e7f8 179 it('Should have the views updated', async function () {
180 this.timeout(20000)
0e1dc3e7 181
89d241a7
C
182 await server.videos.view({ id: videoId })
183 await server.videos.view({ id: videoId })
184 await server.videos.view({ id: videoId })
0e1dc3e7 185
f6d6e7f8 186 await wait(1500)
0e1dc3e7 187
89d241a7
C
188 await server.videos.view({ id: videoId })
189 await server.videos.view({ id: videoId })
fe987656 190
f6d6e7f8 191 await wait(1500)
fe987656 192
89d241a7
C
193 await server.videos.view({ id: videoId })
194 await server.videos.view({ id: videoId })
0e1dc3e7 195
f6d6e7f8 196 // Wait the repeatable job
197 await wait(8000)
0b74c74a 198
89d241a7 199 const video = await server.videos.get({ id: videoId })
f6d6e7f8 200 expect(video.views).to.equal(3)
201 })
923d3d5a 202
f6d6e7f8 203 it('Should remove the video', async function () {
83903cb6 204 const video = await server.videos.get({ id: videoId })
89d241a7 205 await server.videos.remove({ id: videoId })
923d3d5a 206
83903cb6 207 await checkVideoFilesWereRemoved({ video, server })
f6d6e7f8 208 })
3d4e112d 209
f6d6e7f8 210 it('Should not have videos', async function () {
89d241a7 211 const { total, data } = await server.videos.list()
3d4e112d 212
d23dd9fb
C
213 expect(total).to.equal(0)
214 expect(data).to.be.an('array')
215 expect(data).to.have.lengthOf(0)
f6d6e7f8 216 })
0e1dc3e7 217
f6d6e7f8 218 it('Should upload 6 videos', async function () {
219 this.timeout(25000)
d525fc39 220
f6d6e7f8 221 const videos = new Set([
222 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
223 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
224 ])
d525fc39 225
f6d6e7f8 226 for (const video of videos) {
d23dd9fb 227 const attributes = {
f6d6e7f8 228 name: video + ' name',
229 description: video + ' description',
230 category: 2,
231 licence: 1,
232 language: 'en',
233 nsfw: true,
234 tags: [ 'tag1', 'tag2', 'tag3' ],
235 fixture: video
236 }
0e1dc3e7 237
89d241a7 238 await server.videos.upload({ attributes, mode })
f6d6e7f8 239 }
240 })
241
242 it('Should have the correct durations', async function () {
89d241a7 243 const { total, data } = await server.videos.list()
d23dd9fb
C
244
245 expect(total).to.equal(6)
246 expect(data).to.be.an('array')
247 expect(data).to.have.lengthOf(6)
f6d6e7f8 248
d23dd9fb
C
249 const videosByName: { [ name: string ]: Video } = {}
250 data.forEach(v => { videosByName[v.name] = v })
f6d6e7f8 251
f6d6e7f8 252 expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
253 expect(videosByName['video_short.ogv name'].duration).to.equal(5)
254 expect(videosByName['video_short.webm name'].duration).to.equal(5)
255 expect(videosByName['video_short1.webm name'].duration).to.equal(10)
256 expect(videosByName['video_short2.webm name'].duration).to.equal(5)
257 expect(videosByName['video_short3.webm name'].duration).to.equal(5)
258 })
259
260 it('Should have the correct thumbnails', async function () {
89d241a7 261 const { data } = await server.videos.list()
f6d6e7f8 262
f6d6e7f8 263 // For the next test
d23dd9fb 264 videosListBase = data
f6d6e7f8 265
d23dd9fb 266 for (const video of data) {
f6d6e7f8 267 const videoName = video.name.replace(' name', '')
268 await testImage(server.url, videoName, video.thumbnailPath)
269 }
270 })
271
272 it('Should list only the two first videos', async function () {
89d241a7 273 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: 'name' })
f6d6e7f8 274
d23dd9fb
C
275 expect(total).to.equal(6)
276 expect(data.length).to.equal(2)
277 expect(data[0].name).to.equal(videosListBase[0].name)
278 expect(data[1].name).to.equal(videosListBase[1].name)
f6d6e7f8 279 })
280
281 it('Should list only the next three videos', async function () {
89d241a7 282 const { total, data } = await server.videos.list({ start: 2, count: 3, sort: 'name' })
f6d6e7f8 283
d23dd9fb
C
284 expect(total).to.equal(6)
285 expect(data.length).to.equal(3)
286 expect(data[0].name).to.equal(videosListBase[2].name)
287 expect(data[1].name).to.equal(videosListBase[3].name)
288 expect(data[2].name).to.equal(videosListBase[4].name)
f6d6e7f8 289 })
290
291 it('Should list the last video', async function () {
89d241a7 292 const { total, data } = await server.videos.list({ start: 5, count: 6, sort: 'name' })
f6d6e7f8 293
d23dd9fb
C
294 expect(total).to.equal(6)
295 expect(data.length).to.equal(1)
296 expect(data[0].name).to.equal(videosListBase[5].name)
f6d6e7f8 297 })
298
299 it('Should not have the total field', async function () {
89d241a7 300 const { total, data } = await server.videos.list({ start: 5, count: 6, sort: 'name', skipCount: true })
f6d6e7f8 301
d23dd9fb
C
302 expect(total).to.not.exist
303 expect(data.length).to.equal(1)
304 expect(data[0].name).to.equal(videosListBase[5].name)
f6d6e7f8 305 })
306
307 it('Should list and sort by name in descending order', async function () {
89d241a7 308 const { total, data } = await server.videos.list({ sort: '-name' })
f6d6e7f8 309
d23dd9fb
C
310 expect(total).to.equal(6)
311 expect(data.length).to.equal(6)
312 expect(data[0].name).to.equal('video_short.webm name')
313 expect(data[1].name).to.equal('video_short.ogv name')
314 expect(data[2].name).to.equal('video_short.mp4 name')
315 expect(data[3].name).to.equal('video_short3.webm name')
316 expect(data[4].name).to.equal('video_short2.webm name')
317 expect(data[5].name).to.equal('video_short1.webm name')
f6d6e7f8 318
d23dd9fb
C
319 videoId = data[3].uuid
320 videoId2 = data[5].uuid
f6d6e7f8 321 })
322
323 it('Should list and sort by trending in descending order', async function () {
89d241a7 324 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-trending' })
f6d6e7f8 325
d23dd9fb
C
326 expect(total).to.equal(6)
327 expect(data.length).to.equal(2)
f6d6e7f8 328 })
329
330 it('Should list and sort by hotness in descending order', async function () {
89d241a7 331 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-hot' })
f6d6e7f8 332
d23dd9fb
C
333 expect(total).to.equal(6)
334 expect(data.length).to.equal(2)
f6d6e7f8 335 })
336
337 it('Should list and sort by best in descending order', async function () {
89d241a7 338 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-best' })
f6d6e7f8 339
d23dd9fb
C
340 expect(total).to.equal(6)
341 expect(data.length).to.equal(2)
f6d6e7f8 342 })
343
344 it('Should update a video', async function () {
345 const attributes = {
346 name: 'my super video updated',
347 category: 4,
348 licence: 2,
349 language: 'ar',
350 nsfw: false,
351 description: 'my super description updated',
352 commentsEnabled: false,
353 downloadEnabled: false,
354 tags: [ 'tagup1', 'tagup2' ]
355 }
89d241a7 356 await server.videos.update({ id: videoId, attributes })
f6d6e7f8 357 })
0e1dc3e7 358
f6d6e7f8 359 it('Should have the video updated', async function () {
360 this.timeout(60000)
0e1dc3e7 361
89d241a7 362 const video = await server.videos.get({ id: videoId })
0e1dc3e7 363
d23dd9fb 364 await completeVideoCheck(server, video, updateCheckAttributes())
f6d6e7f8 365 })
0e1dc3e7 366
f6d6e7f8 367 it('Should update only the tags of a video', async function () {
368 const attributes = {
369 tags: [ 'supertag', 'tag1', 'tag2' ]
370 }
89d241a7 371 await server.videos.update({ id: videoId, attributes })
0e1dc3e7 372
89d241a7 373 const video = await server.videos.get({ id: videoId })
0e1dc3e7 374
d23dd9fb 375 await completeVideoCheck(server, video, Object.assign(updateCheckAttributes(), attributes))
f6d6e7f8 376 })
0e1dc3e7 377
f6d6e7f8 378 it('Should update only the description of a video', async function () {
379 const attributes = {
380 description: 'hello everybody'
381 }
89d241a7 382 await server.videos.update({ id: videoId, attributes })
0e1dc3e7 383
89d241a7 384 const video = await server.videos.get({ id: videoId })
0e1dc3e7 385
f6d6e7f8 386 const expectedAttributes = Object.assign(updateCheckAttributes(), { tags: [ 'supertag', 'tag1', 'tag2' ] }, attributes)
d23dd9fb 387 await completeVideoCheck(server, video, expectedAttributes)
f6d6e7f8 388 })
0e1dc3e7 389
f6d6e7f8 390 it('Should like a video', async function () {
89d241a7 391 await server.videos.rate({ id: videoId, rating: 'like' })
0e1dc3e7 392
89d241a7 393 const video = await server.videos.get({ id: videoId })
0e1dc3e7 394
f6d6e7f8 395 expect(video.likes).to.equal(1)
396 expect(video.dislikes).to.equal(0)
397 })
2fd59d7d 398
f6d6e7f8 399 it('Should dislike the same video', async function () {
89d241a7 400 await server.videos.rate({ id: videoId, rating: 'dislike' })
f6d6e7f8 401
89d241a7 402 const video = await server.videos.get({ id: videoId })
f6d6e7f8 403
404 expect(video.likes).to.equal(0)
405 expect(video.dislikes).to.equal(1)
406 })
407
408 it('Should sort by originallyPublishedAt', async function () {
2fd59d7d
C
409 {
410 const now = new Date()
411 const attributes = { originallyPublishedAt: now.toISOString() }
89d241a7 412 await server.videos.update({ id: videoId, attributes })
2fd59d7d 413
89d241a7 414 const { data } = await server.videos.list({ sort: '-originallyPublishedAt' })
d23dd9fb 415 const names = data.map(v => v.name)
2fd59d7d
C
416
417 expect(names[0]).to.equal('my super video updated')
418 expect(names[1]).to.equal('video_short2.webm name')
419 expect(names[2]).to.equal('video_short1.webm name')
420 expect(names[3]).to.equal('video_short.webm name')
421 expect(names[4]).to.equal('video_short.ogv name')
422 expect(names[5]).to.equal('video_short.mp4 name')
423 }
424
425 {
426 const now = new Date()
427 const attributes = { originallyPublishedAt: now.toISOString() }
89d241a7 428 await server.videos.update({ id: videoId2, attributes })
2fd59d7d 429
89d241a7 430 const { data } = await server.videos.list({ sort: '-originallyPublishedAt' })
d23dd9fb 431 const names = data.map(v => v.name)
2fd59d7d
C
432
433 expect(names[0]).to.equal('video_short1.webm name')
434 expect(names[1]).to.equal('my super video updated')
435 expect(names[2]).to.equal('video_short2.webm name')
436 expect(names[3]).to.equal('video_short.webm name')
437 expect(names[4]).to.equal('video_short.ogv name')
438 expect(names[5]).to.equal('video_short.mp4 name')
439 }
f6d6e7f8 440 })
441
442 after(async function () {
443 await cleanupTests([ server ])
444 })
445 }
446
447 describe('Legacy upload', function () {
448 runSuite('legacy')
2fd59d7d
C
449 })
450
f6d6e7f8 451 describe('Resumable upload', function () {
452 runSuite('resumable')
0e1dc3e7
C
453 })
454})