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