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