Add user adminFlags
[oweals/peertube.git] / server / tests / api / videos / video-change-ownership.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6   acceptChangeOwnership,
7   changeVideoOwnership,
8   createUser, doubleFollow, flushAndRunMultipleServers,
9   flushTests,
10   getMyUserInformation,
11   getVideoChangeOwnershipList,
12   getVideosList,
13   killallServers,
14   refuseChangeOwnership,
15   runServer,
16   ServerInfo,
17   setAccessTokensToServers,
18   uploadVideo,
19   userLogin,
20   getVideo
21 } from '../../../../shared/utils'
22 import { waitJobs } from '../../../../shared/utils/server/jobs'
23 import { User } from '../../../../shared/models/users'
24 import { VideoDetails } from '../../../../shared/models/videos'
25
26 const expect = chai.expect
27
28 describe('Test video change ownership - nominal', function () {
29   let servers: ServerInfo[] = []
30   const firstUser = {
31     username: 'first',
32     password: 'My great password'
33   }
34   const secondUser = {
35     username: 'second',
36     password: 'My other password'
37   }
38   let firstUserAccessToken = ''
39   let secondUserAccessToken = ''
40   let lastRequestChangeOwnershipId = undefined
41
42   before(async function () {
43     this.timeout(50000)
44
45     servers = await flushAndRunMultipleServers(2)
46     await setAccessTokensToServers(servers)
47
48     const videoQuota = 42000000
49     await createUser({
50       url: servers[ 0 ].url,
51       accessToken: servers[ 0 ].accessToken,
52       username: firstUser.username,
53       password: firstUser.password,
54       videoQuota: videoQuota
55     })
56     await createUser({
57       url: servers[ 0 ].url,
58       accessToken: servers[ 0 ].accessToken,
59       username: secondUser.username,
60       password: secondUser.password,
61       videoQuota: videoQuota
62     })
63
64     firstUserAccessToken = await userLogin(servers[0], firstUser)
65     secondUserAccessToken = await userLogin(servers[0], secondUser)
66
67     const videoAttributes = {
68       name: 'my super name',
69       description: 'my super description'
70     }
71     await uploadVideo(servers[0].url, firstUserAccessToken, videoAttributes)
72
73     await waitJobs(servers)
74
75     const res = await getVideosList(servers[0].url)
76     const videos = res.body.data
77
78     expect(videos.length).to.equal(1)
79
80     const video = videos.find(video => video.name === 'my super name')
81     expect(video.channel.name).to.equal('first_channel')
82     servers[0].video = video
83
84     await doubleFollow(servers[0], servers[1])
85   })
86
87   it('Should not have video change ownership', async function () {
88     const resFirstUser = await getVideoChangeOwnershipList(servers[0].url, firstUserAccessToken)
89
90     expect(resFirstUser.body.total).to.equal(0)
91     expect(resFirstUser.body.data).to.be.an('array')
92     expect(resFirstUser.body.data.length).to.equal(0)
93
94     const resSecondUser = await getVideoChangeOwnershipList(servers[0].url, secondUserAccessToken)
95
96     expect(resSecondUser.body.total).to.equal(0)
97     expect(resSecondUser.body.data).to.be.an('array')
98     expect(resSecondUser.body.data.length).to.equal(0)
99   })
100
101   it('Should send a request to change ownership of a video', async function () {
102     this.timeout(15000)
103
104     await changeVideoOwnership(servers[0].url, firstUserAccessToken, servers[0].video.id, secondUser.username)
105   })
106
107   it('Should only return a request to change ownership for the second user', async function () {
108     const resFirstUser = await getVideoChangeOwnershipList(servers[0].url, firstUserAccessToken)
109
110     expect(resFirstUser.body.total).to.equal(0)
111     expect(resFirstUser.body.data).to.be.an('array')
112     expect(resFirstUser.body.data.length).to.equal(0)
113
114     const resSecondUser = await getVideoChangeOwnershipList(servers[0].url, secondUserAccessToken)
115
116     expect(resSecondUser.body.total).to.equal(1)
117     expect(resSecondUser.body.data).to.be.an('array')
118     expect(resSecondUser.body.data.length).to.equal(1)
119
120     lastRequestChangeOwnershipId = resSecondUser.body.data[0].id
121   })
122
123   it('Should accept the same change ownership request without crashing', async function () {
124     this.timeout(10000)
125
126     await changeVideoOwnership(servers[0].url, firstUserAccessToken, servers[0].video.id, secondUser.username)
127   })
128
129   it('Should not create multiple change ownership requests while one is waiting', async function () {
130     this.timeout(10000)
131
132     const resSecondUser = await getVideoChangeOwnershipList(servers[0].url, secondUserAccessToken)
133
134     expect(resSecondUser.body.total).to.equal(1)
135     expect(resSecondUser.body.data).to.be.an('array')
136     expect(resSecondUser.body.data.length).to.equal(1)
137   })
138
139   it('Should not be possible to refuse the change of ownership from first user', async function () {
140     this.timeout(10000)
141
142     await refuseChangeOwnership(servers[0].url, firstUserAccessToken, lastRequestChangeOwnershipId, 403)
143   })
144
145   it('Should be possible to refuse the change of ownership from second user', async function () {
146     this.timeout(10000)
147
148     await refuseChangeOwnership(servers[0].url, secondUserAccessToken, lastRequestChangeOwnershipId)
149   })
150
151   it('Should send a new request to change ownership of a video', async function () {
152     this.timeout(15000)
153
154     await changeVideoOwnership(servers[0].url, firstUserAccessToken, servers[0].video.id, secondUser.username)
155   })
156
157   it('Should return two requests to change ownership for the second user', async function () {
158     const resFirstUser = await getVideoChangeOwnershipList(servers[0].url, firstUserAccessToken)
159
160     expect(resFirstUser.body.total).to.equal(0)
161     expect(resFirstUser.body.data).to.be.an('array')
162     expect(resFirstUser.body.data.length).to.equal(0)
163
164     const resSecondUser = await getVideoChangeOwnershipList(servers[0].url, secondUserAccessToken)
165
166     expect(resSecondUser.body.total).to.equal(2)
167     expect(resSecondUser.body.data).to.be.an('array')
168     expect(resSecondUser.body.data.length).to.equal(2)
169
170     lastRequestChangeOwnershipId = resSecondUser.body.data[0].id
171   })
172
173   it('Should not be possible to accept the change of ownership from first user', async function () {
174     this.timeout(10000)
175
176     const secondUserInformationResponse = await getMyUserInformation(servers[0].url, secondUserAccessToken)
177     const secondUserInformation: User = secondUserInformationResponse.body
178     const channelId = secondUserInformation.videoChannels[0].id
179     await acceptChangeOwnership(servers[0].url, firstUserAccessToken, lastRequestChangeOwnershipId, channelId, 403)
180   })
181
182   it('Should be possible to accept the change of ownership from second user', async function () {
183     this.timeout(10000)
184
185     const secondUserInformationResponse = await getMyUserInformation(servers[0].url, secondUserAccessToken)
186     const secondUserInformation: User = secondUserInformationResponse.body
187     const channelId = secondUserInformation.videoChannels[0].id
188     await acceptChangeOwnership(servers[0].url, secondUserAccessToken, lastRequestChangeOwnershipId, channelId)
189
190     await waitJobs(servers)
191   })
192
193   it('Should have video channel updated', async function () {
194     for (const server of servers) {
195       const res = await getVideo(server.url, servers[0].video.uuid)
196
197       const video: VideoDetails = res.body
198
199       expect(video.name).to.equal('my super name')
200       expect(video.channel.displayName).to.equal('Main second channel')
201       expect(video.channel.name).to.equal('second_channel')
202     }
203   })
204
205   after(async function () {
206     killallServers(servers)
207   })
208 })
209
210 describe('Test video change ownership - quota too small', function () {
211   let server: ServerInfo = undefined
212   const firstUser = {
213     username: 'first',
214     password: 'My great password'
215   }
216   const secondUser = {
217     username: 'second',
218     password: 'My other password'
219   }
220   let firstUserAccessToken = ''
221   let secondUserAccessToken = ''
222   let lastRequestChangeOwnershipId = undefined
223
224   before(async function () {
225     this.timeout(50000)
226
227     // Run one server
228     await flushTests()
229     server = await runServer(1)
230     await setAccessTokensToServers([server])
231
232     const videoQuota = 42000000
233     const limitedVideoQuota = 10
234     await createUser({
235       url: server.url,
236       accessToken: server.accessToken,
237       username: firstUser.username,
238       password: firstUser.password,
239       videoQuota: videoQuota
240     })
241     await createUser({
242       url: server.url,
243       accessToken: server.accessToken,
244       username: secondUser.username,
245       password: secondUser.password,
246       videoQuota: limitedVideoQuota
247     })
248
249     firstUserAccessToken = await userLogin(server, firstUser)
250     secondUserAccessToken = await userLogin(server, secondUser)
251
252     // Upload some videos on the server
253     const video1Attributes = {
254       name: 'my super name',
255       description: 'my super description'
256     }
257     await uploadVideo(server.url, firstUserAccessToken, video1Attributes)
258
259     await waitJobs(server)
260
261     const res = await getVideosList(server.url)
262     const videos = res.body.data
263
264     expect(videos.length).to.equal(1)
265
266     server.video = videos.find(video => video.name === 'my super name')
267   })
268
269   it('Should send a request to change ownership of a video', async function () {
270     this.timeout(15000)
271
272     await changeVideoOwnership(server.url, firstUserAccessToken, server.video.id, secondUser.username)
273   })
274
275   it('Should only return a request to change ownership for the second user', async function () {
276     const resFirstUser = await getVideoChangeOwnershipList(server.url, firstUserAccessToken)
277
278     expect(resFirstUser.body.total).to.equal(0)
279     expect(resFirstUser.body.data).to.be.an('array')
280     expect(resFirstUser.body.data.length).to.equal(0)
281
282     const resSecondUser = await getVideoChangeOwnershipList(server.url, secondUserAccessToken)
283
284     expect(resSecondUser.body.total).to.equal(1)
285     expect(resSecondUser.body.data).to.be.an('array')
286     expect(resSecondUser.body.data.length).to.equal(1)
287
288     lastRequestChangeOwnershipId = resSecondUser.body.data[0].id
289   })
290
291   it('Should not be possible to accept the change of ownership from second user because of exceeded quota', async function () {
292     this.timeout(10000)
293
294     const secondUserInformationResponse = await getMyUserInformation(server.url, secondUserAccessToken)
295     const secondUserInformation: User = secondUserInformationResponse.body
296     const channelId = secondUserInformation.videoChannels[0].id
297     await acceptChangeOwnership(server.url, secondUserAccessToken, lastRequestChangeOwnershipId, channelId, 403)
298   })
299
300   after(async function () {
301     killallServers([server])
302   })
303 })