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