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