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