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