]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-change-ownership.ts
17c738e6fbd32e05bf44557ecf5ba5d89a2f7cdd
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-change-ownership.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
6 import {
7 ChangeOwnershipCommand,
8 cleanupTests,
9 createUser,
10 doubleFollow,
11 flushAndRunMultipleServers,
12 flushAndRunServer,
13 getMyUserInformation,
14 getVideo,
15 getVideosList,
16 ServerInfo,
17 setAccessTokensToServers,
18 setDefaultVideoChannel,
19 uploadVideo
20 } from '../../../../shared/extra-utils'
21 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
22 import { User } from '../../../../shared/models/users'
23 import { VideoDetails, VideoPrivacy } from '../../../../shared/models/videos'
24
25 const expect = chai.expect
26
27 describe('Test video change ownership - nominal', function () {
28 let servers: ServerInfo[] = []
29 const firstUser = {
30 username: 'first',
31 password: 'My great password'
32 }
33 const secondUser = {
34 username: 'second',
35 password: 'My other password'
36 }
37
38 let firstUserToken = ''
39 let firstUserChannelId: number
40
41 let secondUserToken = ''
42 let secondUserChannelId: number
43
44 let lastRequestId: number
45
46 let liveId: number
47
48 let command: ChangeOwnershipCommand
49
50 before(async function () {
51 this.timeout(50000)
52
53 servers = await flushAndRunMultipleServers(2)
54 await setAccessTokensToServers(servers)
55 await setDefaultVideoChannel(servers)
56
57 await servers[0].configCommand.updateCustomSubConfig({
58 newConfig: {
59 transcoding: {
60 enabled: false
61 },
62 live: {
63 enabled: true
64 }
65 }
66 })
67
68 const videoQuota = 42000000
69 await createUser({
70 url: servers[0].url,
71 accessToken: servers[0].accessToken,
72 username: firstUser.username,
73 password: firstUser.password,
74 videoQuota: videoQuota
75 })
76 await createUser({
77 url: servers[0].url,
78 accessToken: servers[0].accessToken,
79 username: secondUser.username,
80 password: secondUser.password,
81 videoQuota: videoQuota
82 })
83
84 firstUserToken = await servers[0].loginCommand.getAccessToken(firstUser)
85 secondUserToken = await servers[0].loginCommand.getAccessToken(secondUser)
86
87 {
88 const res = await getMyUserInformation(servers[0].url, firstUserToken)
89 const firstUserInformation: User = res.body
90 firstUserChannelId = firstUserInformation.videoChannels[0].id
91 }
92
93 {
94 const res = await getMyUserInformation(servers[0].url, secondUserToken)
95 const secondUserInformation: User = res.body
96 secondUserChannelId = secondUserInformation.videoChannels[0].id
97 }
98
99 {
100 const videoAttributes = {
101 name: 'my super name',
102 description: 'my super description'
103 }
104 const res = await uploadVideo(servers[0].url, firstUserToken, videoAttributes)
105
106 const resVideo = await getVideo(servers[0].url, res.body.video.id)
107 servers[0].video = resVideo.body
108 }
109
110 {
111 const attributes = { name: 'live', channelId: firstUserChannelId, privacy: VideoPrivacy.PUBLIC }
112 const video = await servers[0].liveCommand.create({ token: firstUserToken, fields: attributes })
113
114 liveId = video.id
115 }
116
117 command = servers[0].changeOwnershipCommand
118
119 await doubleFollow(servers[0], servers[1])
120 })
121
122 it('Should not have video change ownership', async function () {
123 {
124 const body = await command.list({ token: firstUserToken })
125
126 expect(body.total).to.equal(0)
127 expect(body.data).to.be.an('array')
128 expect(body.data.length).to.equal(0)
129 }
130
131 {
132 const body = await command.list({ token: secondUserToken })
133
134 expect(body.total).to.equal(0)
135 expect(body.data).to.be.an('array')
136 expect(body.data.length).to.equal(0)
137 }
138 })
139
140 it('Should send a request to change ownership of a video', async function () {
141 this.timeout(15000)
142
143 await command.create({ token: firstUserToken, videoId: servers[0].video.id, username: secondUser.username })
144 })
145
146 it('Should only return a request to change ownership for the second user', async function () {
147 {
148 const body = await command.list({ token: firstUserToken })
149
150 expect(body.total).to.equal(0)
151 expect(body.data).to.be.an('array')
152 expect(body.data.length).to.equal(0)
153 }
154
155 {
156 const body = await command.list({ token: secondUserToken })
157
158 expect(body.total).to.equal(1)
159 expect(body.data).to.be.an('array')
160 expect(body.data.length).to.equal(1)
161
162 lastRequestId = body.data[0].id
163 }
164 })
165
166 it('Should accept the same change ownership request without crashing', async function () {
167 this.timeout(10000)
168
169 await command.create({ token: firstUserToken, videoId: servers[0].video.id, username: secondUser.username })
170 })
171
172 it('Should not create multiple change ownership requests while one is waiting', async function () {
173 this.timeout(10000)
174
175 const body = await command.list({ token: secondUserToken })
176
177 expect(body.total).to.equal(1)
178 expect(body.data).to.be.an('array')
179 expect(body.data.length).to.equal(1)
180 })
181
182 it('Should not be possible to refuse the change of ownership from first user', async function () {
183 this.timeout(10000)
184
185 await command.refuse({ token: firstUserToken, ownershipId: lastRequestId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
186 })
187
188 it('Should be possible to refuse the change of ownership from second user', async function () {
189 this.timeout(10000)
190
191 await command.refuse({ token: secondUserToken, ownershipId: lastRequestId })
192 })
193
194 it('Should send a new request to change ownership of a video', async function () {
195 this.timeout(15000)
196
197 await command.create({ token: firstUserToken, videoId: servers[0].video.id, username: secondUser.username })
198 })
199
200 it('Should return two requests to change ownership for the second user', async function () {
201 {
202 const body = await command.list({ token: firstUserToken })
203
204 expect(body.total).to.equal(0)
205 expect(body.data).to.be.an('array')
206 expect(body.data.length).to.equal(0)
207 }
208
209 {
210 const body = await command.list({ token: secondUserToken })
211
212 expect(body.total).to.equal(2)
213 expect(body.data).to.be.an('array')
214 expect(body.data.length).to.equal(2)
215
216 lastRequestId = body.data[0].id
217 }
218 })
219
220 it('Should not be possible to accept the change of ownership from first user', async function () {
221 this.timeout(10000)
222
223 await command.accept({
224 token: firstUserToken,
225 ownershipId: lastRequestId,
226 channelId: secondUserChannelId,
227 expectedStatus: HttpStatusCode.FORBIDDEN_403
228 })
229 })
230
231 it('Should be possible to accept the change of ownership from second user', async function () {
232 this.timeout(10000)
233
234 await command.accept({ token: secondUserToken, ownershipId: lastRequestId, channelId: secondUserChannelId })
235
236 await waitJobs(servers)
237 })
238
239 it('Should have the channel of the video updated', async function () {
240 for (const server of servers) {
241 const res = await getVideo(server.url, servers[0].video.uuid)
242
243 const video: VideoDetails = res.body
244
245 expect(video.name).to.equal('my super name')
246 expect(video.channel.displayName).to.equal('Main second channel')
247 expect(video.channel.name).to.equal('second_channel')
248 }
249 })
250
251 it('Should send a request to change ownership of a live', async function () {
252 this.timeout(15000)
253
254 await command.create({ token: firstUserToken, videoId: liveId, username: secondUser.username })
255
256 const body = await command.list({ token: secondUserToken })
257
258 expect(body.total).to.equal(3)
259 expect(body.data.length).to.equal(3)
260
261 lastRequestId = body.data[0].id
262 })
263
264 it('Should accept a live ownership change', async function () {
265 this.timeout(20000)
266
267 await command.accept({ token: secondUserToken, ownershipId: lastRequestId, channelId: secondUserChannelId })
268
269 await waitJobs(servers)
270
271 for (const server of servers) {
272 const res = await getVideo(server.url, servers[0].video.uuid)
273
274 const video: VideoDetails = res.body
275
276 expect(video.name).to.equal('my super name')
277 expect(video.channel.displayName).to.equal('Main second channel')
278 expect(video.channel.name).to.equal('second_channel')
279 }
280 })
281
282 after(async function () {
283 await cleanupTests(servers)
284 })
285 })
286
287 describe('Test video change ownership - quota too small', function () {
288 let server: ServerInfo
289 const firstUser = {
290 username: 'first',
291 password: 'My great password'
292 }
293 const secondUser = {
294 username: 'second',
295 password: 'My other password'
296 }
297 let firstUserToken = ''
298 let secondUserToken = ''
299 let lastRequestId: number
300
301 before(async function () {
302 this.timeout(50000)
303
304 // Run one server
305 server = await flushAndRunServer(1)
306 await setAccessTokensToServers([ server ])
307
308 const videoQuota = 42000000
309 const limitedVideoQuota = 10
310 await createUser({
311 url: server.url,
312 accessToken: server.accessToken,
313 username: firstUser.username,
314 password: firstUser.password,
315 videoQuota: videoQuota
316 })
317 await createUser({
318 url: server.url,
319 accessToken: server.accessToken,
320 username: secondUser.username,
321 password: secondUser.password,
322 videoQuota: limitedVideoQuota
323 })
324
325 firstUserToken = await server.loginCommand.getAccessToken(firstUser)
326 secondUserToken = await server.loginCommand.getAccessToken(secondUser)
327
328 // Upload some videos on the server
329 const video1Attributes = {
330 name: 'my super name',
331 description: 'my super description'
332 }
333 await uploadVideo(server.url, firstUserToken, video1Attributes)
334
335 await waitJobs(server)
336
337 const res = await getVideosList(server.url)
338 const videos = res.body.data
339
340 expect(videos.length).to.equal(1)
341
342 server.video = videos.find(video => video.name === 'my super name')
343 })
344
345 it('Should send a request to change ownership of a video', async function () {
346 this.timeout(15000)
347
348 await server.changeOwnershipCommand.create({ token: firstUserToken, videoId: server.video.id, username: secondUser.username })
349 })
350
351 it('Should only return a request to change ownership for the second user', async function () {
352 {
353 const body = await server.changeOwnershipCommand.list({ token: firstUserToken })
354
355 expect(body.total).to.equal(0)
356 expect(body.data).to.be.an('array')
357 expect(body.data.length).to.equal(0)
358 }
359
360 {
361 const body = await server.changeOwnershipCommand.list({ token: secondUserToken })
362
363 expect(body.total).to.equal(1)
364 expect(body.data).to.be.an('array')
365 expect(body.data.length).to.equal(1)
366
367 lastRequestId = body.data[0].id
368 }
369 })
370
371 it('Should not be possible to accept the change of ownership from second user because of exceeded quota', async function () {
372 this.timeout(10000)
373
374 const secondUserInformationResponse = await getMyUserInformation(server.url, secondUserToken)
375 const secondUserInformation: User = secondUserInformationResponse.body
376 const channelId = secondUserInformation.videoChannels[0].id
377
378 await server.changeOwnershipCommand.accept({
379 token: secondUserToken,
380 ownershipId: lastRequestId,
381 channelId,
382 expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413
383 })
384 })
385
386 after(async function () {
387 await cleanupTests([ server ])
388 })
389 })