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