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