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