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