Merge from upstream
[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 } from '../../../../shared/index'
6 import {
7   createUser,
8   doubleFollow,
9   flushAndRunMultipleServers,
10   getVideoChannelVideos, serverLogin, testImage,
11   updateVideo,
12   updateVideoChannelAvatar,
13   uploadVideo, wait, userLogin
14 } from '../../../../shared/utils'
15 import {
16   addVideoChannel,
17   deleteVideoChannel,
18   flushTests,
19   getAccountVideoChannelsList,
20   getMyUserInformation,
21   getVideoChannel,
22   getVideoChannelsList,
23   killallServers,
24   ServerInfo,
25   setAccessTokensToServers,
26   updateVideoChannel
27 } from '../../../../shared/utils/index'
28 import { waitJobs } from '../../../../shared/utils/server/jobs'
29
30 const expect = chai.expect
31
32 describe('Test video channels', function () {
33   let servers: ServerInfo[]
34   let userInfo: User
35   let accountUUID: string
36   let firstVideoChannelId: number
37   let secondVideoChannelId: number
38   let videoUUID: string
39
40   before(async function () {
41     this.timeout(30000)
42
43     await flushTests()
44
45     servers = await flushAndRunMultipleServers(2)
46
47     await setAccessTokensToServers(servers)
48     await doubleFollow(servers[0], servers[1])
49
50     {
51       const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
52       const user: User = res.body
53       accountUUID = user.account.uuid
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 res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'my video name', channelId: secondVideoChannelId })
86       videoUUID = res.body.video.uuid
87     }
88
89     await waitJobs(servers)
90   })
91
92   it('Should have two video channels when getting my information', async () => {
93     const res = await getMyUserInformation(servers[0].url, servers[0].accessToken)
94     userInfo = res.body
95
96     expect(userInfo.videoChannels).to.be.an('array')
97     expect(userInfo.videoChannels).to.have.lengthOf(2)
98
99     const videoChannels = userInfo.videoChannels
100     expect(videoChannels[0].name).to.equal('root_channel')
101     expect(videoChannels[0].displayName).to.equal('Main root channel')
102
103     expect(videoChannels[1].name).to.equal('second_video_channel')
104     expect(videoChannels[1].displayName).to.equal('second video channel')
105     expect(videoChannels[1].description).to.equal('super video channel description')
106     expect(videoChannels[1].support).to.equal('super video channel support text')
107   })
108
109   it('Should have two video channels when getting account channels on server 1', async function () {
110     const res = await getAccountVideoChannelsList(servers[0].url, userInfo.account.name + '@' + userInfo.account.host)
111     expect(res.body.total).to.equal(2)
112     expect(res.body.data).to.be.an('array')
113     expect(res.body.data).to.have.lengthOf(2)
114
115     const videoChannels = res.body.data
116     expect(videoChannels[0].name).to.equal('root_channel')
117     expect(videoChannels[0].displayName).to.equal('Main root channel')
118
119     expect(videoChannels[1].name).to.equal('second_video_channel')
120     expect(videoChannels[1].displayName).to.equal('second video channel')
121     expect(videoChannels[1].description).to.equal('super video channel description')
122     expect(videoChannels[1].support).to.equal('super video channel support text')
123   })
124
125   it('Should have one video channel when getting account channels on server 2', async function () {
126     const res = await getAccountVideoChannelsList(servers[1].url, userInfo.account.name + '@' + userInfo.account.host)
127     expect(res.body.total).to.equal(1)
128     expect(res.body.data).to.be.an('array')
129     expect(res.body.data).to.have.lengthOf(1)
130
131     const videoChannels = res.body.data
132     expect(videoChannels[0].name).to.equal('second_video_channel')
133     expect(videoChannels[0].displayName).to.equal('second video channel')
134     expect(videoChannels[0].description).to.equal('super video channel description')
135     expect(videoChannels[0].support).to.equal('super video channel support text')
136   })
137
138   it('Should list video channels', async function () {
139     const res = await getVideoChannelsList(servers[0].url, 1, 1, '-name')
140
141     expect(res.body.total).to.equal(2)
142     expect(res.body.data).to.be.an('array')
143     expect(res.body.data).to.have.lengthOf(1)
144     expect(res.body.data[0].name).to.equal('root_channel')
145     expect(res.body.data[0].displayName).to.equal('Main root channel')
146   })
147
148   it('Should update video channel', async function () {
149     this.timeout(5000)
150
151     const videoChannelAttributes = {
152       displayName: 'video channel updated',
153       description: 'video channel description updated',
154       support: 'video channel support text updated'
155     }
156
157     await updateVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel', videoChannelAttributes)
158
159     await waitJobs(servers)
160   })
161
162   it('Should have video channel updated', async function () {
163     for (const server of servers) {
164       const res = await getVideoChannelsList(server.url, 0, 1, '-name')
165
166       expect(res.body.total).to.equal(2)
167       expect(res.body.data).to.be.an('array')
168       expect(res.body.data).to.have.lengthOf(1)
169       expect(res.body.data[0].name).to.equal('second_video_channel')
170       expect(res.body.data[0].displayName).to.equal('video channel updated')
171       expect(res.body.data[0].description).to.equal('video channel description updated')
172       expect(res.body.data[0].support).to.equal('video channel support text updated')
173     }
174   })
175
176   it('Should update video channel avatar', async function () {
177     this.timeout(5000)
178
179     const fixture = 'avatar.png'
180
181     await updateVideoChannelAvatar({
182       url: servers[0].url,
183       accessToken: servers[0].accessToken,
184       videoChannelName: 'second_video_channel',
185       fixture
186     })
187
188     await waitJobs(servers)
189   })
190
191   it('Should have video channel avatar updated', async function () {
192     for (const server of servers) {
193       const res = await getVideoChannelsList(server.url, 0, 1, '-name')
194
195       const videoChannel = res.body.data.find(c => c.id === secondVideoChannelId)
196
197       await testImage(server.url, 'avatar-resized', videoChannel.avatar.path, '.png')
198     }
199   })
200
201   it('Should get video channel', async function () {
202     const res = await getVideoChannel(servers[0].url, 'second_video_channel')
203
204     const videoChannel = res.body
205     expect(videoChannel.name).to.equal('second_video_channel')
206     expect(videoChannel.displayName).to.equal('video channel updated')
207     expect(videoChannel.description).to.equal('video channel description updated')
208     expect(videoChannel.support).to.equal('video channel support text updated')
209   })
210
211   it('Should list the second video channel videos', async function () {
212     this.timeout(10000)
213
214     for (const server of servers) {
215       const channelURI = 'second_video_channel@localhost:9001'
216       const res1 = await getVideoChannelVideos(server.url, server.accessToken, channelURI, 0, 5)
217       expect(res1.body.total).to.equal(1)
218       expect(res1.body.data).to.be.an('array')
219       expect(res1.body.data).to.have.lengthOf(1)
220       expect(res1.body.data[0].name).to.equal('my video name')
221     }
222   })
223
224   it('Should change the video channel of a video', async function () {
225     this.timeout(10000)
226
227     await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, { channelId: firstVideoChannelId })
228
229     await waitJobs(servers)
230   })
231
232   it('Should list the first video channel videos', async function () {
233     this.timeout(10000)
234
235     for (const server of servers) {
236       const secondChannelURI = 'second_video_channel@localhost:9001'
237       const res1 = await getVideoChannelVideos(server.url, server.accessToken, secondChannelURI, 0, 5)
238       expect(res1.body.total).to.equal(0)
239
240       const channelURI = 'root_channel@localhost:9001'
241       const res2 = await getVideoChannelVideos(server.url, server.accessToken, channelURI, 0, 5)
242       expect(res2.body.total).to.equal(1)
243
244       const videos: Video[] = res2.body.data
245       expect(videos).to.be.an('array')
246       expect(videos).to.have.lengthOf(1)
247       expect(videos[0].name).to.equal('my video name')
248     }
249   })
250
251   it('Should delete video channel', async function () {
252     await deleteVideoChannel(servers[0].url, servers[0].accessToken, 'second_video_channel')
253   })
254
255   it('Should have video channel deleted', async function () {
256     const res = await getVideoChannelsList(servers[0].url, 0, 10)
257
258     expect(res.body.total).to.equal(1)
259     expect(res.body.data).to.be.an('array')
260     expect(res.body.data).to.have.lengthOf(1)
261     expect(res.body.data[0].displayName).to.equal('Main root channel')
262   })
263
264   it('Should create the main channel with an uuid if there is a conflict', async function () {
265     {
266       const videoChannel = { name: 'toto_channel', displayName: 'My toto channel' }
267       await addVideoChannel(servers[ 0 ].url, servers[ 0 ].accessToken, videoChannel)
268     }
269
270     {
271       await createUser(servers[ 0 ].url, servers[ 0 ].accessToken, 'toto', 'password')
272       const accessToken = await userLogin(servers[ 0 ], { username: 'toto', password: 'password' })
273
274       const res = await getMyUserInformation(servers[ 0 ].url, accessToken)
275       const videoChannel = res.body.videoChannels[ 0 ]
276       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}/)
277     }
278   })
279
280   after(async function () {
281     killallServers(servers)
282   })
283 })