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