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