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