]>
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 delete the video channel avatar', async function () { | |
329 | this.timeout(15000) | |
330 | await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'avatar' }) | |
331 | ||
332 | await waitJobs(servers) | |
333 | ||
334 | for (const server of servers) { | |
335 | const videoChannel = await findChannel(server, secondVideoChannelId) | |
336 | await testFileExistsOrNot(server, 'avatars', basename(avatarPaths[server.port]), false) | |
337 | ||
338 | expect(videoChannel.avatars).to.be.empty | |
339 | } | |
340 | }) | |
341 | ||
342 | it('Should delete the video channel banner', async function () { | |
343 | this.timeout(15000) | |
344 | ||
345 | await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'banner' }) | |
346 | ||
347 | await waitJobs(servers) | |
348 | ||
349 | for (const server of servers) { | |
350 | const videoChannel = await findChannel(server, secondVideoChannelId) | |
351 | await testFileExistsOrNot(server, 'avatars', basename(bannerPaths[server.port]), false) | |
352 | ||
353 | expect(videoChannel.banners).to.be.empty | |
354 | } | |
355 | }) | |
356 | ||
357 | it('Should list the second video channel videos', async function () { | |
358 | this.timeout(10000) | |
359 | ||
360 | for (const server of servers) { | |
361 | const channelURI = 'second_video_channel@localhost:' + servers[0].port | |
362 | const { total, data } = await server.videos.listByChannel({ handle: channelURI }) | |
363 | ||
364 | expect(total).to.equal(1) | |
365 | expect(data).to.be.an('array') | |
366 | expect(data).to.have.lengthOf(1) | |
367 | expect(data[0].name).to.equal('my video name') | |
368 | } | |
369 | }) | |
370 | ||
371 | it('Should change the video channel of a video', async function () { | |
372 | this.timeout(10000) | |
373 | ||
374 | await servers[0].videos.update({ id: videoUUID, attributes: { channelId: servers[0].store.channel.id } }) | |
375 | ||
376 | await waitJobs(servers) | |
377 | }) | |
378 | ||
379 | it('Should list the first video channel videos', async function () { | |
380 | this.timeout(10000) | |
381 | ||
382 | for (const server of servers) { | |
383 | { | |
384 | const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port | |
385 | const { total } = await server.videos.listByChannel({ handle: secondChannelURI }) | |
386 | expect(total).to.equal(0) | |
387 | } | |
388 | ||
389 | { | |
390 | const channelURI = 'root_channel@localhost:' + servers[0].port | |
391 | const { total, data } = await server.videos.listByChannel({ handle: channelURI }) | |
392 | expect(total).to.equal(1) | |
393 | ||
394 | expect(data).to.be.an('array') | |
395 | expect(data).to.have.lengthOf(1) | |
396 | expect(data[0].name).to.equal('my video name') | |
397 | } | |
398 | } | |
399 | }) | |
400 | ||
401 | it('Should delete video channel', async function () { | |
402 | await servers[0].channels.delete({ channelName: 'second_video_channel' }) | |
403 | }) | |
404 | ||
405 | it('Should have video channel deleted', async function () { | |
406 | const body = await servers[0].channels.list({ start: 0, count: 10, sort: 'createdAt' }) | |
407 | ||
408 | expect(body.total).to.equal(2) | |
409 | expect(body.data).to.be.an('array') | |
410 | expect(body.data).to.have.lengthOf(2) | |
411 | expect(body.data[0].displayName).to.equal('Main root channel') | |
412 | expect(body.data[1].displayName).to.equal('video channel updated') | |
413 | }) | |
414 | ||
415 | it('Should create the main channel with an uuid if there is a conflict', async function () { | |
416 | { | |
417 | const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' } | |
418 | const created = await servers[0].channels.create({ attributes: videoChannel }) | |
419 | totoChannel = created.id | |
420 | } | |
421 | ||
422 | { | |
423 | await servers[0].users.create({ username: 'toto', password: 'password' }) | |
424 | const accessToken = await servers[0].login.getAccessToken({ username: 'toto', password: 'password' }) | |
425 | ||
426 | const { videoChannels } = await servers[0].users.getMyInfo({ token: accessToken }) | |
427 | const videoChannel = videoChannels[0] | |
428 | 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}/) | |
429 | } | |
430 | }) | |
431 | ||
432 | it('Should report correct channel views per days', async function () { | |
433 | this.timeout(10000) | |
434 | ||
435 | { | |
436 | const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true }) | |
437 | ||
438 | for (const channel of data) { | |
439 | expect(channel).to.haveOwnProperty('viewsPerDay') | |
440 | expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today | |
441 | ||
442 | for (const v of channel.viewsPerDay) { | |
443 | expect(v.date).to.be.an('string') | |
444 | expect(v.views).to.equal(0) | |
445 | } | |
446 | } | |
447 | } | |
448 | ||
449 | { | |
450 | // video has been posted on channel servers[0].store.videoChannel.id since last update | |
451 | await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.1,127.0.0.1' }) | |
452 | await servers[0].videos.view({ id: videoUUID, xForwardedFor: '0.0.0.2,127.0.0.1' }) | |
453 | ||
454 | // Wait the repeatable job | |
455 | await wait(8000) | |
456 | ||
457 | const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true }) | |
458 | const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id) | |
459 | expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2) | |
460 | } | |
461 | }) | |
462 | ||
463 | it('Should report correct videos count', async function () { | |
464 | const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true }) | |
465 | ||
466 | const totoChannel = data.find(c => c.name === 'toto_channel') | |
467 | const rootChannel = data.find(c => c.name === 'root_channel') | |
468 | ||
469 | expect(rootChannel.videosCount).to.equal(1) | |
470 | expect(totoChannel.videosCount).to.equal(0) | |
471 | }) | |
472 | ||
473 | it('Should search among account video channels', async function () { | |
474 | { | |
475 | const body = await servers[0].channels.listByAccount({ accountName, search: 'root' }) | |
476 | expect(body.total).to.equal(1) | |
477 | ||
478 | const channels = body.data | |
479 | expect(channels).to.have.lengthOf(1) | |
480 | } | |
481 | ||
482 | { | |
483 | const body = await servers[0].channels.listByAccount({ accountName, search: 'does not exist' }) | |
484 | expect(body.total).to.equal(0) | |
485 | ||
486 | const channels = body.data | |
487 | expect(channels).to.have.lengthOf(0) | |
488 | } | |
489 | }) | |
490 | ||
491 | it('Should list channels by updatedAt desc if a video has been uploaded', async function () { | |
492 | this.timeout(30000) | |
493 | ||
494 | await servers[0].videos.upload({ attributes: { channelId: totoChannel } }) | |
495 | await waitJobs(servers) | |
496 | ||
497 | for (const server of servers) { | |
498 | const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' }) | |
499 | ||
500 | expect(data[0].name).to.equal('toto_channel') | |
501 | expect(data[1].name).to.equal('root_channel') | |
502 | } | |
503 | ||
504 | await servers[0].videos.upload({ attributes: { channelId: servers[0].store.channel.id } }) | |
505 | await waitJobs(servers) | |
506 | ||
507 | for (const server of servers) { | |
508 | const { data } = await server.channels.listByAccount({ accountName, sort: '-updatedAt' }) | |
509 | ||
510 | expect(data[0].name).to.equal('root_channel') | |
511 | expect(data[1].name).to.equal('toto_channel') | |
512 | } | |
513 | }) | |
514 | ||
515 | after(async function () { | |
516 | await cleanupTests(servers) | |
517 | }) | |
518 | }) |