]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/tests/api/videos/video-channels.ts
Move test functions outside extra-utils
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-channels.ts
... / ...
CommitLineData
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import 'mocha'
4import * as chai from 'chai'
5import { basename } from 'path'
6import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants'
7import { testFileExistsOrNot, testImage } from '@server/tests/shared'
8import { wait } from '@shared/core-utils'
9import { User, VideoChannel } from '@shared/models'
10import {
11 cleanupTests,
12 createMultipleServers,
13 doubleFollow,
14 PeerTubeServer,
15 setAccessTokensToServers,
16 setDefaultVideoChannel,
17 waitJobs
18} from '@shared/server-commands'
19
20const expect = chai.expect
21
22async function findChannel (server: PeerTubeServer, channelId: number) {
23 const body = await server.channels.list({ sort: '-name' })
24
25 return body.data.find(c => c.id === channelId)
26}
27
28describe('Test video channels', function () {
29 let servers: PeerTubeServer[]
30 let userInfo: User
31 let secondVideoChannelId: number
32 let totoChannel: number
33 let videoUUID: string
34 let accountName: string
35 let secondUserChannelName: string
36
37 const avatarPaths: { [ port: number ]: string } = {}
38 const bannerPaths: { [ port: number ]: string } = {}
39
40 before(async function () {
41 this.timeout(60000)
42
43 servers = await createMultipleServers(2)
44
45 await setAccessTokensToServers(servers)
46 await setDefaultVideoChannel(servers)
47
48 await doubleFollow(servers[0], servers[1])
49 })
50
51 it('Should have one video channel (created with root)', async () => {
52 const body = await servers[0].channels.list({ start: 0, count: 2 })
53
54 expect(body.total).to.equal(1)
55 expect(body.data).to.be.an('array')
56 expect(body.data).to.have.lengthOf(1)
57 })
58
59 it('Should create another video channel', async function () {
60 this.timeout(10000)
61
62 {
63 const videoChannel = {
64 name: 'second_video_channel',
65 displayName: 'second video channel',
66 description: 'super video channel description',
67 support: 'super video channel support text'
68 }
69 const created = await servers[0].channels.create({ attributes: videoChannel })
70 secondVideoChannelId = created.id
71 }
72
73 // The channel is 1 is propagated to servers 2
74 {
75 const attributes = { name: 'my video name', channelId: secondVideoChannelId, support: 'video support field' }
76 const { uuid } = await servers[0].videos.upload({ attributes })
77 videoUUID = uuid
78 }
79
80 await waitJobs(servers)
81 })
82
83 it('Should have two video channels when getting my information', async () => {
84 userInfo = await servers[0].users.getMyInfo()
85
86 expect(userInfo.videoChannels).to.be.an('array')
87 expect(userInfo.videoChannels).to.have.lengthOf(2)
88
89 const videoChannels = userInfo.videoChannels
90 expect(videoChannels[0].name).to.equal('root_channel')
91 expect(videoChannels[0].displayName).to.equal('Main root channel')
92
93 expect(videoChannels[1].name).to.equal('second_video_channel')
94 expect(videoChannels[1].displayName).to.equal('second video channel')
95 expect(videoChannels[1].description).to.equal('super video channel description')
96 expect(videoChannels[1].support).to.equal('super video channel support text')
97
98 accountName = userInfo.account.name + '@' + userInfo.account.host
99 })
100
101 it('Should have two video channels when getting account channels on server 1', async function () {
102 const body = await servers[0].channels.listByAccount({ accountName })
103 expect(body.total).to.equal(2)
104
105 const videoChannels = body.data
106
107 expect(videoChannels).to.be.an('array')
108 expect(videoChannels).to.have.lengthOf(2)
109
110 expect(videoChannels[0].name).to.equal('root_channel')
111 expect(videoChannels[0].displayName).to.equal('Main root channel')
112
113 expect(videoChannels[1].name).to.equal('second_video_channel')
114 expect(videoChannels[1].displayName).to.equal('second video channel')
115 expect(videoChannels[1].description).to.equal('super video channel description')
116 expect(videoChannels[1].support).to.equal('super video channel support text')
117 })
118
119 it('Should paginate and sort account channels', async function () {
120 {
121 const body = await servers[0].channels.listByAccount({
122 accountName,
123 start: 0,
124 count: 1,
125 sort: 'createdAt'
126 })
127
128 expect(body.total).to.equal(2)
129 expect(body.data).to.have.lengthOf(1)
130
131 const videoChannel: VideoChannel = body.data[0]
132 expect(videoChannel.name).to.equal('root_channel')
133 }
134
135 {
136 const body = await servers[0].channels.listByAccount({
137 accountName,
138 start: 0,
139 count: 1,
140 sort: '-createdAt'
141 })
142
143 expect(body.total).to.equal(2)
144 expect(body.data).to.have.lengthOf(1)
145 expect(body.data[0].name).to.equal('second_video_channel')
146 }
147
148 {
149 const body = await servers[0].channels.listByAccount({
150 accountName,
151 start: 1,
152 count: 1,
153 sort: '-createdAt'
154 })
155
156 expect(body.total).to.equal(2)
157 expect(body.data).to.have.lengthOf(1)
158 expect(body.data[0].name).to.equal('root_channel')
159 }
160 })
161
162 it('Should have one video channel when getting account channels on server 2', async function () {
163 const body = await servers[1].channels.listByAccount({ accountName })
164
165 expect(body.total).to.equal(1)
166 expect(body.data).to.be.an('array')
167 expect(body.data).to.have.lengthOf(1)
168
169 const videoChannel = body.data[0]
170 expect(videoChannel.name).to.equal('second_video_channel')
171 expect(videoChannel.displayName).to.equal('second video channel')
172 expect(videoChannel.description).to.equal('super video channel description')
173 expect(videoChannel.support).to.equal('super video channel support text')
174 })
175
176 it('Should list video channels', async function () {
177 const body = await servers[0].channels.list({ start: 1, count: 1, sort: '-name' })
178
179 expect(body.total).to.equal(2)
180 expect(body.data).to.be.an('array')
181 expect(body.data).to.have.lengthOf(1)
182 expect(body.data[0].name).to.equal('root_channel')
183 expect(body.data[0].displayName).to.equal('Main root channel')
184 })
185
186 it('Should update video channel', async function () {
187 this.timeout(15000)
188
189 const videoChannelAttributes = {
190 displayName: 'video channel updated',
191 description: 'video channel description updated',
192 support: 'support updated'
193 }
194
195 await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
196
197 await waitJobs(servers)
198 })
199
200 it('Should have video channel updated', async function () {
201 for (const server of servers) {
202 const body = await server.channels.list({ start: 0, count: 1, sort: '-name' })
203
204 expect(body.total).to.equal(2)
205 expect(body.data).to.be.an('array')
206 expect(body.data).to.have.lengthOf(1)
207
208 expect(body.data[0].name).to.equal('second_video_channel')
209 expect(body.data[0].displayName).to.equal('video channel updated')
210 expect(body.data[0].description).to.equal('video channel description updated')
211 expect(body.data[0].support).to.equal('support updated')
212 }
213 })
214
215 it('Should not have updated the video support field', async function () {
216 for (const server of servers) {
217 const video = await server.videos.get({ id: videoUUID })
218 expect(video.support).to.equal('video support field')
219 }
220 })
221
222 it('Should update another accounts video channel', async function () {
223 this.timeout(15000)
224
225 const result = await servers[0].users.generate('second_user')
226 secondUserChannelName = result.userChannelName
227
228 await servers[0].videos.quickUpload({ name: 'video', token: result.token })
229
230 const videoChannelAttributes = {
231 displayName: 'video channel updated',
232 description: 'video channel description updated',
233 support: 'support updated'
234 }
235
236 await servers[0].channels.update({ channelName: secondUserChannelName, attributes: videoChannelAttributes })
237
238 await waitJobs(servers)
239 })
240
241 it('Should have another accounts video channel updated', async function () {
242 for (const server of servers) {
243 const body = await server.channels.get({ channelName: `${secondUserChannelName}@${servers[0].host}` })
244
245 expect(body.displayName).to.equal('video channel updated')
246 expect(body.description).to.equal('video channel description updated')
247 expect(body.support).to.equal('support updated')
248 }
249 })
250
251 it('Should update the channel support field and update videos too', async function () {
252 this.timeout(35000)
253
254 const videoChannelAttributes = {
255 support: 'video channel support text updated',
256 bulkVideosSupportUpdate: true
257 }
258
259 await servers[0].channels.update({ channelName: 'second_video_channel', attributes: videoChannelAttributes })
260
261 await waitJobs(servers)
262
263 for (const server of servers) {
264 const video = await server.videos.get({ id: videoUUID })
265 expect(video.support).to.equal(videoChannelAttributes.support)
266 }
267 })
268
269 it('Should update video channel avatar', async function () {
270 this.timeout(15000)
271
272 const fixture = 'avatar.png'
273
274 await servers[0].channels.updateImage({
275 channelName: 'second_video_channel',
276 fixture,
277 type: 'avatar'
278 })
279
280 await waitJobs(servers)
281
282 for (const server of servers) {
283 const videoChannel = await findChannel(server, secondVideoChannelId)
284
285 avatarPaths[server.port] = videoChannel.avatar.path
286 await testImage(server.url, 'avatar-resized', avatarPaths[server.port], '.png')
287 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), true)
288
289 const row = await server.sql.getActorImage(basename(avatarPaths[server.port]))
290 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.AVATARS.height)
291 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.AVATARS.width)
292 }
293 })
294
295 it('Should update video channel banner', async function () {
296 this.timeout(15000)
297
298 const fixture = 'banner.jpg'
299
300 await servers[0].channels.updateImage({
301 channelName: 'second_video_channel',
302 fixture,
303 type: 'banner'
304 })
305
306 await waitJobs(servers)
307
308 for (const server of servers) {
309 const videoChannel = await server.channels.get({ channelName: 'second_video_channel@' + servers[0].host })
310
311 bannerPaths[server.port] = videoChannel.banner.path
312 await testImage(server.url, 'banner-resized', bannerPaths[server.port])
313 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), true)
314
315 const row = await server.sql.getActorImage(basename(bannerPaths[server.port]))
316 expect(row.height).to.equal(ACTOR_IMAGES_SIZE.BANNERS.height)
317 expect(row.width).to.equal(ACTOR_IMAGES_SIZE.BANNERS.width)
318 }
319 })
320
321 it('Should delete the video channel avatar', async function () {
322 this.timeout(15000)
323
324 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'avatar' })
325
326 await waitJobs(servers)
327
328 for (const server of servers) {
329 const videoChannel = await findChannel(server, secondVideoChannelId)
330 await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), false)
331
332 expect(videoChannel.avatar).to.be.null
333 }
334 })
335
336 it('Should delete the video channel banner', async function () {
337 this.timeout(15000)
338
339 await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'banner' })
340
341 await waitJobs(servers)
342
343 for (const server of servers) {
344 const videoChannel = await findChannel(server, secondVideoChannelId)
345 await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), false)
346
347 expect(videoChannel.banner).to.be.null
348 }
349 })
350
351 it('Should list the second video channel videos', async function () {
352 this.timeout(10000)
353
354 for (const server of servers) {
355 const channelURI = 'second_video_channel@localhost:' + servers[0].port
356 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
357
358 expect(total).to.equal(1)
359 expect(data).to.be.an('array')
360 expect(data).to.have.lengthOf(1)
361 expect(data[0].name).to.equal('my video name')
362 }
363 })
364
365 it('Should change the video channel of a video', async function () {
366 this.timeout(10000)
367
368 await servers[0].videos.update({ id: videoUUID, attributes: { channelId: servers[0].store.channel.id } })
369
370 await waitJobs(servers)
371 })
372
373 it('Should list the first video channel videos', async function () {
374 this.timeout(10000)
375
376 for (const server of servers) {
377 {
378 const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port
379 const { total } = await server.videos.listByChannel({ handle: secondChannelURI })
380 expect(total).to.equal(0)
381 }
382
383 {
384 const channelURI = 'root_channel@localhost:' + servers[0].port
385 const { total, data } = await server.videos.listByChannel({ handle: channelURI })
386 expect(total).to.equal(1)
387
388 expect(data).to.be.an('array')
389 expect(data).to.have.lengthOf(1)
390 expect(data[0].name).to.equal('my video name')
391 }
392 }
393 })
394
395 it('Should delete video channel', async function () {
396 await servers[0].channels.delete({ channelName: 'second_video_channel' })
397 })
398
399 it('Should have video channel deleted', async function () {
400 const body = await servers[0].channels.list({ start: 0, count: 10, sort: 'createdAt' })
401
402 expect(body.total).to.equal(2)
403 expect(body.data).to.be.an('array')
404 expect(body.data).to.have.lengthOf(2)
405 expect(body.data[0].displayName).to.equal('Main root channel')
406 expect(body.data[1].displayName).to.equal('video channel updated')
407 })
408
409 it('Should create the main channel with an uuid if there is a conflict', async function () {
410 {
411 const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
412 const created = await servers[0].channels.create({ attributes: videoChannel })
413 totoChannel = created.id
414 }
415
416 {
417 await servers[0].users.create({ username: 'toto', password: 'password' })
418 const accessToken = await servers[0].login.getAccessToken({ username: 'toto', password: 'password' })
419
420 const { videoChannels } = await servers[0].users.getMyInfo({ token: accessToken })
421 const videoChannel = videoChannels[0]
422 expect(videoChannel.name).to.match(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/)
423 }
424 })
425
426 it('Should report correct channel views per days', async function () {
427 this.timeout(10000)
428
429 {
430 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
431
432 for (const channel of data) {
433 expect(channel).to.haveOwnProperty('viewsPerDay')
434 expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
435
436 for (const v of channel.viewsPerDay) {
437 expect(v.date).to.be.an('string')
438 expect(v.views).to.equal(0)
439 }
440 }
441 }
442
443 {
444 // video has been posted on channel servers[0].store.videoChannel.id since last update
445 await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.1,127.0.0.1' })
446 await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.2,127.0.0.1' })
447
448 // Wait the repeatable job
449 await wait(8000)
450
451 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
452 const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id)
453 expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
454 }
455 })
456
457 it('Should report correct videos count', async function () {
458 const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true })
459
460 const totoChannel = data.find(c => c.name === 'toto_channel')
461 const rootChannel = data.find(c => c.name === 'root_channel')
462
463 expect(rootChannel.videosCount).to.equal(1)
464 expect(totoChannel.videosCount).to.equal(0)
465 })
466
467 it('Should search among account video channels', async function () {
468 {
469 const body = await servers[0].channels.listByAccount({ accountName, search: 'root' })
470 expect(body.total).to.equal(1)
471
472 const channels = body.data
473 expect(channels).to.have.lengthOf(1)
474 }
475
476 {
477 const body = await servers[0].channels.listByAccount({ accountName, search: 'does not exist' })
478 expect(body.total).to.equal(0)
479
480 const channels = body.data
481 expect(channels).to.have.lengthOf(0)
482 }
483 })
484
485 it('Should list channels by updatedAt desc if a video has been uploaded', async function () {
486 this.timeout(30000)
487
488 await servers[0].videos.upload({ attributes: { channelId: totoChannel } })
489 await waitJobs(servers)
490
491 for (const server of servers) {
492 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
493
494 expect(data[0].name).to.equal('toto_channel')
495 expect(data[1].name).to.equal('root_channel')
496 }
497
498 await servers[0].videos.upload({ attributes: { channelId: servers[0].store.channel.id } })
499 await waitJobs(servers)
500
501 for (const server of servers) {
502 const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' })
503
504 expect(data[0].name).to.equal('root_channel')
505 expect(data[1].name).to.equal('toto_channel')
506 }
507 })
508
509 after(async function () {
510 await cleanupTests(servers)
511 })
512})