]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-change-ownership.ts
99d774c2b175e5a68d0037ff607ebb4013a1967d
[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 await command.create({ token: firstUserToken, videoId: servers[0].store.videoCreated.id, username: secondUser })
134 })
135
136 it('Should not create multiple change ownership requests while one is waiting', async function () {
137 const body = await command.list({ token: secondUserToken })
138
139 expect(body.total).to.equal(1)
140 expect(body.data).to.be.an('array')
141 expect(body.data.length).to.equal(1)
142 })
143
144 it('Should not be possible to refuse the change of ownership from first user', async function () {
145 await command.refuse({ token: firstUserToken, ownershipId: lastRequestId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
146 })
147
148 it('Should be possible to refuse the change of ownership from second user', async function () {
149 await command.refuse({ token: secondUserToken, ownershipId: lastRequestId })
150 })
151
152 it('Should send a new request to change ownership of a video', async function () {
153 this.timeout(15000)
154
155 await command.create({ token: firstUserToken, videoId: servers[0].store.videoCreated.id, username: secondUser })
156 })
157
158 it('Should return two requests to change ownership for the second user', async function () {
159 {
160 const body = await command.list({ token: firstUserToken })
161
162 expect(body.total).to.equal(0)
163 expect(body.data).to.be.an('array')
164 expect(body.data.length).to.equal(0)
165 }
166
167 {
168 const body = await command.list({ token: secondUserToken })
169
170 expect(body.total).to.equal(2)
171 expect(body.data).to.be.an('array')
172 expect(body.data.length).to.equal(2)
173
174 lastRequestId = body.data[0].id
175 }
176 })
177
178 it('Should not be possible to accept the change of ownership from first user', async function () {
179 await command.accept({
180 token: firstUserToken,
181 ownershipId: lastRequestId,
182 channelId: secondUserChannelId,
183 expectedStatus: HttpStatusCode.FORBIDDEN_403
184 })
185 })
186
187 it('Should be possible to accept the change of ownership from second user', async function () {
188 await command.accept({ token: secondUserToken, ownershipId: lastRequestId, channelId: secondUserChannelId })
189
190 await waitJobs(servers)
191 })
192
193 it('Should have the channel of the video updated', async function () {
194 for (const server of servers) {
195 const video = await server.videos.get({ id: servers[0].store.videoCreated.uuid })
196
197 expect(video.name).to.equal('my super name')
198 expect(video.channel.displayName).to.equal('Main second channel')
199 expect(video.channel.name).to.equal('second_channel')
200 }
201 })
202
203 it('Should send a request to change ownership of a live', async function () {
204 this.timeout(15000)
205
206 await command.create({ token: firstUserToken, videoId: liveId, username: secondUser })
207
208 const body = await command.list({ token: secondUserToken })
209
210 expect(body.total).to.equal(3)
211 expect(body.data.length).to.equal(3)
212
213 lastRequestId = body.data[0].id
214 })
215
216 it('Should accept a live ownership change', async function () {
217 this.timeout(20000)
218
219 await command.accept({ token: secondUserToken, ownershipId: lastRequestId, channelId: secondUserChannelId })
220
221 await waitJobs(servers)
222
223 for (const server of servers) {
224 const video = await server.videos.get({ id: servers[0].store.videoCreated.uuid })
225
226 expect(video.name).to.equal('my super name')
227 expect(video.channel.displayName).to.equal('Main second channel')
228 expect(video.channel.name).to.equal('second_channel')
229 }
230 })
231
232 after(async function () {
233 await cleanupTests(servers)
234 })
235 })
236
237 describe('Test video change ownership - quota too small', function () {
238 let server: PeerTubeServer
239 const firstUser = 'first'
240 const secondUser = 'second'
241
242 let firstUserToken = ''
243 let secondUserToken = ''
244 let lastRequestId: number
245
246 before(async function () {
247 this.timeout(50000)
248
249 // Run one server
250 server = await createSingleServer(1)
251 await setAccessTokensToServers([ server ])
252
253 await server.users.create({ username: secondUser, videoQuota: 10 })
254
255 firstUserToken = await server.users.generateUserAndToken(firstUser)
256 secondUserToken = await server.login.getAccessToken(secondUser)
257
258 // Upload some videos on the server
259 const attributes = {
260 name: 'my super name',
261 description: 'my super description'
262 }
263 await server.videos.upload({ token: firstUserToken, attributes })
264
265 await waitJobs(server)
266
267 const { data } = await server.videos.list()
268 expect(data.length).to.equal(1)
269
270 server.store.videoCreated = data.find(video => video.name === 'my super name')
271 })
272
273 it('Should send a request to change ownership of a video', async function () {
274 this.timeout(15000)
275
276 await server.changeOwnership.create({ token: firstUserToken, videoId: server.store.videoCreated.id, username: secondUser })
277 })
278
279 it('Should only return a request to change ownership for the second user', async function () {
280 {
281 const body = await server.changeOwnership.list({ token: firstUserToken })
282
283 expect(body.total).to.equal(0)
284 expect(body.data).to.be.an('array')
285 expect(body.data.length).to.equal(0)
286 }
287
288 {
289 const body = await server.changeOwnership.list({ token: secondUserToken })
290
291 expect(body.total).to.equal(1)
292 expect(body.data).to.be.an('array')
293 expect(body.data.length).to.equal(1)
294
295 lastRequestId = body.data[0].id
296 }
297 })
298
299 it('Should not be possible to accept the change of ownership from second user because of exceeded quota', async function () {
300 const { videoChannels } = await server.users.getMyInfo({ token: secondUserToken })
301 const channelId = videoChannels[0].id
302
303 await server.changeOwnership.accept({
304 token: secondUserToken,
305 ownershipId: lastRequestId,
306 channelId,
307 expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413
308 })
309 })
310
311 after(async function () {
312 await cleanupTests([ server ])
313 })
314 })