API: Add ability to update video channel avatar
[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   doubleFollow,
8   flushAndRunMultipleServers,
9   getVideoChannelVideos, testImage,
10   updateVideo,
11   updateVideoChannelAvatar,
12   uploadVideo, wait
13 } from '../../utils'
14 import {
15   addVideoChannel,
16   deleteVideoChannel,
17   flushTests,
18   getAccountVideoChannelsList,
19   getMyUserInformation,
20   getVideoChannel,
21   getVideoChannelsList,
22   killallServers,
23   ServerInfo,
24   setAccessTokensToServers,
25   updateVideoChannel
26 } from '../../utils/index'
27 import { waitJobs } from '../../utils/server/jobs'
28
29 const expect = chai.expect
30
31 describe('Test video channels', function () {
32   let servers: ServerInfo[]
33   let userInfo: User
34   let accountUUID: string
35   let firstVideoChannelId: number
36   let firstVideoChannelUUID: string
37   let secondVideoChannelId: number
38   let secondVideoChannelUUID: string
39   let videoUUID: string
40
41   before(async function () {
42     this.timeout(30000)
43
44     await flushTests()
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       accountUUID = user.account.uuid
55
56       firstVideoChannelId = user.videoChannels[0].id
57       firstVideoChannelUUID = user.videoChannels[0].uuid
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         displayName: 'second video channel',
77         description: 'super video channel description',
78         support: 'super video channel support text'
79       }
80       const res = await addVideoChannel(servers[ 0 ].url, servers[ 0 ].accessToken, videoChannel)
81       secondVideoChannelId = res.body.videoChannel.id
82       secondVideoChannelUUID = res.body.videoChannel.uuid
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].displayName).to.equal('Default root channel')
103     expect(videoChannels[1].displayName).to.equal('second video channel')
104     expect(videoChannels[1].description).to.equal('super video channel description')
105     expect(videoChannels[1].support).to.equal('super video channel support text')
106   })
107
108   it('Should have two video channels when getting account channels on server 1', async function () {
109     const res = await getAccountVideoChannelsList(servers[0].url, userInfo.account.name + '@' + userInfo.account.host)
110     expect(res.body.total).to.equal(2)
111     expect(res.body.data).to.be.an('array')
112     expect(res.body.data).to.have.lengthOf(2)
113
114     const videoChannels = res.body.data
115     expect(videoChannels[0].displayName).to.equal('Default root 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 have one video channel when getting account channels on server 2', async function () {
122     const res = await getAccountVideoChannelsList(servers[1].url, userInfo.account.name + '@' + userInfo.account.host)
123     expect(res.body.total).to.equal(1)
124     expect(res.body.data).to.be.an('array')
125     expect(res.body.data).to.have.lengthOf(1)
126
127     const videoChannels = res.body.data
128     expect(videoChannels[0].displayName).to.equal('second video channel')
129     expect(videoChannels[0].description).to.equal('super video channel description')
130     expect(videoChannels[0].support).to.equal('super video channel support text')
131   })
132
133   it('Should list video channels', async function () {
134     const res = await getVideoChannelsList(servers[0].url, 1, 1, '-name')
135
136     expect(res.body.total).to.equal(2)
137     expect(res.body.data).to.be.an('array')
138     expect(res.body.data).to.have.lengthOf(1)
139     expect(res.body.data[0].displayName).to.equal('Default root channel')
140   })
141
142   it('Should update video channel', async function () {
143     this.timeout(5000)
144
145     const videoChannelAttributes = {
146       displayName: 'video channel updated',
147       description: 'video channel description updated',
148       support: 'video channel support text updated'
149     }
150
151     await updateVideoChannel(servers[0].url, servers[0].accessToken, secondVideoChannelId, videoChannelAttributes)
152
153     await waitJobs(servers)
154   })
155
156   it('Should have video channel updated', async function () {
157     for (const server of servers) {
158       const res = await getVideoChannelsList(server.url, 0, 1, '-name')
159
160       expect(res.body.total).to.equal(2)
161       expect(res.body.data).to.be.an('array')
162       expect(res.body.data).to.have.lengthOf(1)
163       expect(res.body.data[0].displayName).to.equal('video channel updated')
164       expect(res.body.data[0].description).to.equal('video channel description updated')
165       expect(res.body.data[0].support).to.equal('video channel support text updated')
166     }
167   })
168
169   it('Should update video channel avatar', async function () {
170     this.timeout(5000)
171
172     const fixture = 'avatar.png'
173
174     await updateVideoChannelAvatar({
175       url: servers[0].url,
176       accessToken: servers[0].accessToken,
177       videoChannelId: secondVideoChannelId,
178       fixture
179     })
180
181     await waitJobs(servers)
182   })
183
184   it('Should have video channel avatar updated', async function () {
185     for (const server of servers) {
186       const res = await getVideoChannelsList(server.url, 0, 1, '-name')
187
188       const videoChannel = res.body.data.find(c => c.id === secondVideoChannelId)
189
190       await testImage(server.url, 'avatar-resized', videoChannel.avatar.path, '.png')
191     }
192   })
193
194   it('Should get video channel', async function () {
195     const res = await getVideoChannel(servers[0].url, secondVideoChannelId)
196
197     const videoChannel = res.body
198     expect(videoChannel.displayName).to.equal('video channel updated')
199     expect(videoChannel.description).to.equal('video channel description updated')
200     expect(videoChannel.support).to.equal('video channel support text updated')
201   })
202
203   it('Should list the second video channel videos', async function () {
204     this.timeout(10000)
205
206     for (const server of servers) {
207       const res1 = await getVideoChannelVideos(server.url, server.accessToken, secondVideoChannelUUID, 0, 5)
208       expect(res1.body.total).to.equal(1)
209       expect(res1.body.data).to.be.an('array')
210       expect(res1.body.data).to.have.lengthOf(1)
211       expect(res1.body.data[0].name).to.equal('my video name')
212     }
213   })
214
215   it('Should change the video channel of a video', async function () {
216     this.timeout(10000)
217
218     await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, { channelId: firstVideoChannelId })
219
220     await waitJobs(servers)
221   })
222
223   it('Should list the first video channel videos', async function () {
224     this.timeout(10000)
225
226     for (const server of servers) {
227       const res1 = await getVideoChannelVideos(server.url, server.accessToken, secondVideoChannelUUID, 0, 5)
228       expect(res1.body.total).to.equal(0)
229
230       const res2 = await getVideoChannelVideos(server.url, server.accessToken, firstVideoChannelUUID, 0, 5)
231       expect(res2.body.total).to.equal(1)
232
233       const videos: Video[] = res2.body.data
234       expect(videos).to.be.an('array')
235       expect(videos).to.have.lengthOf(1)
236       expect(videos[0].name).to.equal('my video name')
237     }
238   })
239
240   it('Should delete video channel', async function () {
241     await deleteVideoChannel(servers[0].url, servers[0].accessToken, secondVideoChannelId)
242   })
243
244   it('Should have video channel deleted', async function () {
245     const res = await getVideoChannelsList(servers[0].url, 0, 10)
246
247     expect(res.body.total).to.equal(1)
248     expect(res.body.data).to.be.an('array')
249     expect(res.body.data).to.have.lengthOf(1)
250     expect(res.body.data[0].displayName).to.equal('Default root channel')
251   })
252
253   after(async function () {
254     killallServers(servers)
255   })
256 })