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