allow limiting video-comments rss feeds to an account or video channel
[oweals/peertube.git] / server / tests / api / videos / video-channels.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 {
6   cleanupTests,
7   createUser,
8   doubleFollow,
9   flushAndRunMultipleServers,
10   getVideo,
11   getVideoChannelVideos,
12   testImage,
13   updateVideo,
14   updateVideoChannelAvatar,
15   uploadVideo,
16   userLogin,
17   wait
18 } from '../../../../shared/extra-utils'
19 import {
20   addVideoChannel,
21   deleteVideoChannel,
22   getAccountVideoChannelsList,
23   getMyUserInformation,
24   getVideoChannel,
25   getVideoChannelsList,
26   ServerInfo,
27   setAccessTokensToServers,
28   updateVideoChannel,
29   viewVideo
30 } from '../../../../shared/extra-utils/index'
31 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
32 import { User, Video, VideoChannel, VideoDetails } from '../../../../shared/index'
33
34 const expect = chai.expect
35
36 describe('Test video channels', function () {
37   let servers: ServerInfo[]
38   let userInfo: User
39   let firstVideoChannelId: number
40   let secondVideoChannelId: number
41   let videoUUID: string
42
43   before(async function () {
44     this.timeout(60000)
45
46     servers = await flushAndRunMultipleServers(2)
47
48     await setAccessTokensToServers(servers)
49     await doubleFollow(servers[0], servers[1])
50
51     {
52       const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
53       const user: User = res.body
54
55       firstVideoChannelId = user.videoChannels[0].id
56     }
57
58     await waitJobs(servers)
59   })
60
61   it('Should have one video channel (created with root)', async () => {
62     const res = await getVideoChannelsList(servers[0].url, 0, 2)
63
64     expect(res.body.total).to.equal(1)
65     expect(res.body.data).to.be.an('array')
66     expect(res.body.data).to.have.lengthOf(1)
67   })
68
69   it('Should create another video channel', async function () {
70     this.timeout(10000)
71
72     {
73       const videoChannel = {
74         name: 'second_video_channel',
75         displayName: 'second video channel',
76         description: 'super video channel description',
77         support: 'super video channel support text'
78       }
79       const res = await addVideoChannel(servers[0].url, servers[0].accessToken, videoChannel)
80       secondVideoChannelId = res.body.videoChannel.id
81     }
82
83     // The channel is 1 is propagated to servers 2
84     {
85       const videoAttributesArg = { name: 'my video name', channelId: secondVideoChannelId, support: 'video support field' }
86       const res = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributesArg)
87       videoUUID = res.body.video.uuid
88     }
89
90     await waitJobs(servers)
91   })
92
93   it('Should have two video channels when getting my information', async () => {
94     const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
95     userInfo = res.body
96
97     expect(userInfo.videoChannels).to.be.an('array')
98     expect(userInfo.videoChannels).to.have.lengthOf(2)
99
100     const videoChannels = userInfo.videoChannels
101     expect(videoChannels[0].name).to.equal('root_channel')
102     expect(videoChannels[0].displayName).to.equal('Main root channel')
103
104     expect(videoChannels[1].name).to.equal('second_video_channel')
105     expect(videoChannels[1].displayName).to.equal('second video channel')
106     expect(videoChannels[1].description).to.equal('super video channel description')
107     expect(videoChannels[1].support).to.equal('super video channel support text')
108   })
109
110   it('Should have two video channels when getting account channels on server 1', async function () {
111     const res = await getAccountVideoChannelsList({
112       url: servers[0].url,
113       accountName: userInfo.account.name + '@' + userInfo.account.host
114     })
115
116     expect(res.body.total).to.equal(2)
117     expect(res.body.data).to.be.an('array')
118     expect(res.body.data).to.have.lengthOf(2)
119
120     const videoChannels = res.body.data
121     expect(videoChannels[0].name).to.equal('root_channel')
122     expect(videoChannels[0].displayName).to.equal('Main root channel')
123
124     expect(videoChannels[1].name).to.equal('second_video_channel')
125     expect(videoChannels[1].displayName).to.equal('second video channel')
126     expect(videoChannels[1].description).to.equal('super video channel description')
127     expect(videoChannels[1].support).to.equal('super video channel support text')
128   })
129
130   it('Should paginate and sort account channels', async function () {
131     {
132       const res = await getAccountVideoChannelsList({
133         url: servers[0].url,
134         accountName: userInfo.account.name + '@' + userInfo.account.host,
135         start: 0,
136         count: 1,
137         sort: 'createdAt'
138       })
139
140       expect(res.body.total).to.equal(2)
141       expect(res.body.data).to.have.lengthOf(1)
142
143       const videoChannel: VideoChannel = res.body.data[0]
144       expect(videoChannel.name).to.equal('root_channel')
145     }
146
147     {
148       const res = await getAccountVideoChannelsList({
149         url: servers[0].url,
150         accountName: userInfo.account.name + '@' + userInfo.account.host,
151         start: 0,
152         count: 1,
153         sort: '-createdAt'
154       })
155
156       expect(res.body.total).to.equal(2)
157       expect(res.body.data).to.have.lengthOf(1)
158
159       const videoChannel: VideoChannel = res.body.data[0]
160       expect(videoChannel.name).to.equal('second_video_channel')
161     }
162
163     {
164       const res = await getAccountVideoChannelsList({
165         url: servers[0].url,
166         accountName: userInfo.account.name + '@' + userInfo.account.host,
167         start: 1,
168         count: 1,
169         sort: '-createdAt'
170       })
171
172       expect(res.body.total).to.equal(2)
173       expect(res.body.data).to.have.lengthOf(1)
174
175       const videoChannel: VideoChannel = res.body.data[0]
176       expect(videoChannel.name).to.equal('root_channel')
177     }
178   })
179
180   it('Should have one video channel when getting account channels on server 2', async function () {
181     const res = await getAccountVideoChannelsList({
182       url: servers[1].url,
183       accountName: userInfo.account.name + '@' + userInfo.account.host
184     })
185
186     expect(res.body.total).to.equal(1)
187     expect(res.body.data).to.be.an('array')
188     expect(res.body.data).to.have.lengthOf(1)
189
190     const videoChannels = res.body.data
191     expect(videoChannels[0].name).to.equal('second_video_channel')
192     expect(videoChannels[0].displayName).to.equal('second video channel')
193     expect(videoChannels[0].description).to.equal('super video channel description')
194     expect(videoChannels[0].support).to.equal('super video channel support text')
195   })
196
197   it('Should list video channels', async function () {
198     const res = await getVideoChannelsList(servers[0].url, 1, 1, '-name')
199
200     expect(res.body.total).to.equal(2)
201     expect(res.body.data).to.be.an('array')
202     expect(res.body.data).to.have.lengthOf(1)
203     expect(res.body.data[0].name).to.equal('root_channel')
204     expect(res.body.data[0].displayName).to.equal('Main root channel')
205   })
206
207   it('Should update video channel', async function () {
208     this.timeout(15000)
209
210     const videoChannelAttributes = {
211       displayName: 'video channel updated',
212       description: 'video channel description updated',
213       support: 'support updated'
214     }
215
216     await updateVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel', videoChannelAttributes)
217
218     await waitJobs(servers)
219   })
220
221   it('Should have video channel updated', async function () {
222     for (const server of servers) {
223       const res = await getVideoChannelsList(server.url, 0, 1, '-name')
224
225       expect(res.body.total).to.equal(2)
226       expect(res.body.data).to.be.an('array')
227       expect(res.body.data).to.have.lengthOf(1)
228       expect(res.body.data[0].name).to.equal('second_video_channel')
229       expect(res.body.data[0].displayName).to.equal('video channel updated')
230       expect(res.body.data[0].description).to.equal('video channel description updated')
231       expect(res.body.data[0].support).to.equal('support updated')
232     }
233   })
234
235   it('Should not have updated the video support field', async function () {
236     for (const server of servers) {
237       const res = await getVideo(server.url, videoUUID)
238       const video: VideoDetails = res.body
239
240       expect(video.support).to.equal('video support field')
241     }
242   })
243
244   it('Should update the channel support field and update videos too', async function () {
245     this.timeout(35000)
246
247     const videoChannelAttributes = {
248       support: 'video channel support text updated',
249       bulkVideosSupportUpdate: true
250     }
251
252     await updateVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel', videoChannelAttributes)
253
254     await waitJobs(servers)
255
256     for (const server of servers) {
257       const res = await getVideo(server.url, videoUUID)
258       const video: VideoDetails = res.body
259
260       expect(video.support).to.equal(videoChannelAttributes.support)
261     }
262   })
263
264   it('Should update video channel avatar', async function () {
265     this.timeout(5000)
266
267     const fixture = 'avatar.png'
268
269     await updateVideoChannelAvatar({
270       url: servers[0].url,
271       accessToken: servers[0].accessToken,
272       videoChannelName: 'second_video_channel',
273       fixture
274     })
275
276     await waitJobs(servers)
277   })
278
279   it('Should have video channel avatar updated', async function () {
280     for (const server of servers) {
281       const res = await getVideoChannelsList(server.url, 0, 1, '-name')
282
283       const videoChannel = res.body.data.find(c => c.id === secondVideoChannelId)
284
285       await testImage(server.url, 'avatar-resized', videoChannel.avatar.path, '.png')
286     }
287   })
288
289   it('Should get video channel', async function () {
290     const res = await getVideoChannel(servers[0].url, 'second_video_channel')
291
292     const videoChannel = res.body
293     expect(videoChannel.name).to.equal('second_video_channel')
294     expect(videoChannel.displayName).to.equal('video channel updated')
295     expect(videoChannel.description).to.equal('video channel description updated')
296     expect(videoChannel.support).to.equal('video channel support text updated')
297   })
298
299   it('Should list the second video channel videos', async function () {
300     this.timeout(10000)
301
302     for (const server of servers) {
303       const channelURI = 'second_video_channel@localhost:' + servers[0].port
304       const res1 = await getVideoChannelVideos(server.url, server.accessToken, channelURI, 0, 5)
305       expect(res1.body.total).to.equal(1)
306       expect(res1.body.data).to.be.an('array')
307       expect(res1.body.data).to.have.lengthOf(1)
308       expect(res1.body.data[0].name).to.equal('my video name')
309     }
310   })
311
312   it('Should change the video channel of a video', async function () {
313     this.timeout(10000)
314
315     await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, { channelId: firstVideoChannelId })
316
317     await waitJobs(servers)
318   })
319
320   it('Should list the first video channel videos', async function () {
321     this.timeout(10000)
322
323     for (const server of servers) {
324       const secondChannelURI = 'second_video_channel@localhost:' + servers[0].port
325       const res1 = await getVideoChannelVideos(server.url, server.accessToken, secondChannelURI, 0, 5)
326       expect(res1.body.total).to.equal(0)
327
328       const channelURI = 'root_channel@localhost:' + servers[0].port
329       const res2 = await getVideoChannelVideos(server.url, server.accessToken, channelURI, 0, 5)
330       expect(res2.body.total).to.equal(1)
331
332       const videos: Video[] = res2.body.data
333       expect(videos).to.be.an('array')
334       expect(videos).to.have.lengthOf(1)
335       expect(videos[0].name).to.equal('my video name')
336     }
337   })
338
339   it('Should delete video channel', async function () {
340     await deleteVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel')
341   })
342
343   it('Should have video channel deleted', async function () {
344     const res = await getVideoChannelsList(servers[0].url, 0, 10)
345
346     expect(res.body.total).to.equal(1)
347     expect(res.body.data).to.be.an('array')
348     expect(res.body.data).to.have.lengthOf(1)
349     expect(res.body.data[0].displayName).to.equal('Main root channel')
350   })
351
352   it('Should create the main channel with an uuid if there is a conflict', async function () {
353     {
354       const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
355       await addVideoChannel(servers[0].url, servers[0].accessToken, videoChannel)
356     }
357
358     {
359       await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: 'toto', password: 'password' })
360       const accessToken = await userLogin(servers[0], { username: 'toto', password: 'password' })
361
362       const res = await getMyUserInformation(servers[0].url, accessToken)
363       const videoChannel = res.body.videoChannels[0]
364       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}/)
365     }
366   })
367
368   it('Should report correct channel views per days', async function () {
369     this.timeout(10000)
370
371     {
372       const res = await getAccountVideoChannelsList({
373         url: servers[0].url,
374         accountName: userInfo.account.name + '@' + userInfo.account.host,
375         withStats: true
376       })
377
378       const channels: VideoChannel[] = res.body.data
379
380       for (const channel of channels) {
381         expect(channel).to.haveOwnProperty('viewsPerDay')
382         expect(channel.viewsPerDay).to.have.length(30 + 1) // daysPrior + today
383
384         for (const v of channel.viewsPerDay) {
385           expect(v.date).to.be.an('string')
386           expect(v.views).to.equal(0)
387         }
388       }
389     }
390
391     {
392       // video has been posted on channel firstVideoChannelId since last update
393       await viewVideo(servers[0].url, videoUUID, 204, '0.0.0.1,127.0.0.1')
394       await viewVideo(servers[0].url, videoUUID, 204, '0.0.0.2,127.0.0.1')
395
396       // Wait the repeatable job
397       await wait(8000)
398
399       const res = await getAccountVideoChannelsList({
400         url: servers[0].url,
401         accountName: userInfo.account.name + '@' + userInfo.account.host,
402         withStats: true
403       })
404       const channelWithView = res.body.data.find((channel: VideoChannel) => channel.id === firstVideoChannelId)
405       expect(channelWithView.viewsPerDay.slice(-1)[0].views).to.equal(2)
406     }
407   })
408
409   it('Should report correct videos count', async function () {
410     const res = await getAccountVideoChannelsList({
411       url: servers[0].url,
412       accountName: userInfo.account.name + '@' + userInfo.account.host,
413       withStats: true
414     })
415     const channels: VideoChannel[] = res.body.data
416
417     const totoChannel = channels.find(c => c.name === 'toto_channel')
418     const rootChannel = channels.find(c => c.name === 'root_channel')
419
420     expect(rootChannel.videosCount).to.equal(1)
421     expect(totoChannel.videosCount).to.equal(0)
422   })
423
424   after(async function () {
425     await cleanupTests(servers)
426   })
427 })