]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-change-ownership.ts
emit more specific status codes on video upload (#3423)
[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 'mocha'
5 import {
6 acceptChangeOwnership,
7 changeVideoOwnership,
8 cleanupTests,
9 createUser,
10 doubleFollow,
11 flushAndRunMultipleServers,
12 flushAndRunServer,
13 getMyUserInformation,
14 getVideo,
15 getVideoChangeOwnershipList,
16 getVideosList,
17 refuseChangeOwnership,
18 ServerInfo,
19 setAccessTokensToServers,
20 uploadVideo,
21 userLogin
22 } from '../../../../shared/extra-utils'
23 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
24 import { User } from '../../../../shared/models/users'
25 import { VideoDetails } from '../../../../shared/models/videos'
26 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
27
28 const expect = chai.expect
29
30 describe('Test video change ownership - nominal', function () {
31 let servers: ServerInfo[] = []
32 const firstUser = {
33 username: 'first',
34 password: 'My great password'
35 }
36 const secondUser = {
37 username: 'second',
38 password: 'My other password'
39 }
40 let firstUserAccessToken = ''
41 let secondUserAccessToken = ''
42 let lastRequestChangeOwnershipId = ''
43
44 before(async function () {
45 this.timeout(50000)
46
47 servers = await flushAndRunMultipleServers(2)
48 await setAccessTokensToServers(servers)
49
50 const videoQuota = 42000000
51 await createUser({
52 url: servers[0].url,
53 accessToken: servers[0].accessToken,
54 username: firstUser.username,
55 password: firstUser.password,
56 videoQuota: videoQuota
57 })
58 await createUser({
59 url: servers[0].url,
60 accessToken: servers[0].accessToken,
61 username: secondUser.username,
62 password: secondUser.password,
63 videoQuota: videoQuota
64 })
65
66 firstUserAccessToken = await userLogin(servers[0], firstUser)
67 secondUserAccessToken = await userLogin(servers[0], secondUser)
68
69 const videoAttributes = {
70 name: 'my super name',
71 description: 'my super description'
72 }
73 await uploadVideo(servers[0].url, firstUserAccessToken, videoAttributes)
74
75 await waitJobs(servers)
76
77 const res = await getVideosList(servers[0].url)
78 const videos = res.body.data
79
80 expect(videos.length).to.equal(1)
81
82 const video = videos.find(video => video.name === 'my super name')
83 expect(video.channel.name).to.equal('first_channel')
84 servers[0].video = video
85
86 await doubleFollow(servers[0], servers[1])
87 })
88
89 it('Should not have video change ownership', async function () {
90 const resFirstUser = await getVideoChangeOwnershipList(servers[0].url, firstUserAccessToken)
91
92 expect(resFirstUser.body.total).to.equal(0)
93 expect(resFirstUser.body.data).to.be.an('array')
94 expect(resFirstUser.body.data.length).to.equal(0)
95
96 const resSecondUser = await getVideoChangeOwnershipList(servers[0].url, secondUserAccessToken)
97
98 expect(resSecondUser.body.total).to.equal(0)
99 expect(resSecondUser.body.data).to.be.an('array')
100 expect(resSecondUser.body.data.length).to.equal(0)
101 })
102
103 it('Should send a request to change ownership of a video', async function () {
104 this.timeout(15000)
105
106 await changeVideoOwnership(servers[0].url, firstUserAccessToken, servers[0].video.id, secondUser.username)
107 })
108
109 it('Should only return a request to change ownership for the second user', async function () {
110 const resFirstUser = await getVideoChangeOwnershipList(servers[0].url, firstUserAccessToken)
111
112 expect(resFirstUser.body.total).to.equal(0)
113 expect(resFirstUser.body.data).to.be.an('array')
114 expect(resFirstUser.body.data.length).to.equal(0)
115
116 const resSecondUser = await getVideoChangeOwnershipList(servers[0].url, secondUserAccessToken)
117
118 expect(resSecondUser.body.total).to.equal(1)
119 expect(resSecondUser.body.data).to.be.an('array')
120 expect(resSecondUser.body.data.length).to.equal(1)
121
122 lastRequestChangeOwnershipId = resSecondUser.body.data[0].id
123 })
124
125 it('Should accept the same change ownership request without crashing', async function () {
126 this.timeout(10000)
127
128 await changeVideoOwnership(servers[0].url, firstUserAccessToken, servers[0].video.id, secondUser.username)
129 })
130
131 it('Should not create multiple change ownership requests while one is waiting', async function () {
132 this.timeout(10000)
133
134 const resSecondUser = await getVideoChangeOwnershipList(servers[0].url, secondUserAccessToken)
135
136 expect(resSecondUser.body.total).to.equal(1)
137 expect(resSecondUser.body.data).to.be.an('array')
138 expect(resSecondUser.body.data.length).to.equal(1)
139 })
140
141 it('Should not be possible to refuse the change of ownership from first user', async function () {
142 this.timeout(10000)
143
144 await refuseChangeOwnership(servers[0].url, firstUserAccessToken, lastRequestChangeOwnershipId, HttpStatusCode.FORBIDDEN_403)
145 })
146
147 it('Should be possible to refuse the change of ownership from second user', async function () {
148 this.timeout(10000)
149
150 await refuseChangeOwnership(servers[0].url, secondUserAccessToken, lastRequestChangeOwnershipId)
151 })
152
153 it('Should send a new request to change ownership of a video', async function () {
154 this.timeout(15000)
155
156 await changeVideoOwnership(servers[0].url, firstUserAccessToken, servers[0].video.id, secondUser.username)
157 })
158
159 it('Should return two requests to change ownership for the second user', async function () {
160 const resFirstUser = await getVideoChangeOwnershipList(servers[0].url, firstUserAccessToken)
161
162 expect(resFirstUser.body.total).to.equal(0)
163 expect(resFirstUser.body.data).to.be.an('array')
164 expect(resFirstUser.body.data.length).to.equal(0)
165
166 const resSecondUser = await getVideoChangeOwnershipList(servers[0].url, secondUserAccessToken)
167
168 expect(resSecondUser.body.total).to.equal(2)
169 expect(resSecondUser.body.data).to.be.an('array')
170 expect(resSecondUser.body.data.length).to.equal(2)
171
172 lastRequestChangeOwnershipId = resSecondUser.body.data[0].id
173 })
174
175 it('Should not be possible to accept the change of ownership from first user', async function () {
176 this.timeout(10000)
177
178 const secondUserInformationResponse = await getMyUserInformation(servers[0].url, secondUserAccessToken)
179 const secondUserInformation: User = secondUserInformationResponse.body
180 const channelId = secondUserInformation.videoChannels[0].id
181 await acceptChangeOwnership(servers[0].url, firstUserAccessToken, lastRequestChangeOwnershipId, channelId, HttpStatusCode.FORBIDDEN_403)
182 })
183
184 it('Should be possible to accept the change of ownership from second user', async function () {
185 this.timeout(10000)
186
187 const secondUserInformationResponse = await getMyUserInformation(servers[0].url, secondUserAccessToken)
188 const secondUserInformation: User = secondUserInformationResponse.body
189 const channelId = secondUserInformation.videoChannels[0].id
190 await acceptChangeOwnership(servers[0].url, secondUserAccessToken, lastRequestChangeOwnershipId, channelId)
191
192 await waitJobs(servers)
193 })
194
195 it('Should have the channel of the video updated', async function () {
196 for (const server of servers) {
197 const res = await getVideo(server.url, servers[0].video.uuid)
198
199 const video: VideoDetails = res.body
200
201 expect(video.name).to.equal('my super name')
202 expect(video.channel.displayName).to.equal('Main second channel')
203 expect(video.channel.name).to.equal('second_channel')
204 }
205 })
206
207 after(async function () {
208 await cleanupTests(servers)
209 })
210 })
211
212 describe('Test video change ownership - quota too small', function () {
213 let server: ServerInfo
214 const firstUser = {
215 username: 'first',
216 password: 'My great password'
217 }
218 const secondUser = {
219 username: 'second',
220 password: 'My other password'
221 }
222 let firstUserAccessToken = ''
223 let secondUserAccessToken = ''
224 let lastRequestChangeOwnershipId = ''
225
226 before(async function () {
227 this.timeout(50000)
228
229 // Run one server
230 server = await flushAndRunServer(1)
231 await setAccessTokensToServers([ server ])
232
233 const videoQuota = 42000000
234 const limitedVideoQuota = 10
235 await createUser({
236 url: server.url,
237 accessToken: server.accessToken,
238 username: firstUser.username,
239 password: firstUser.password,
240 videoQuota: videoQuota
241 })
242 await createUser({
243 url: server.url,
244 accessToken: server.accessToken,
245 username: secondUser.username,
246 password: secondUser.password,
247 videoQuota: limitedVideoQuota
248 })
249
250 firstUserAccessToken = await userLogin(server, firstUser)
251 secondUserAccessToken = await userLogin(server, secondUser)
252
253 // Upload some videos on the server
254 const video1Attributes = {
255 name: 'my super name',
256 description: 'my super description'
257 }
258 await uploadVideo(server.url, firstUserAccessToken, video1Attributes)
259
260 await waitJobs(server)
261
262 const res = await getVideosList(server.url)
263 const videos = res.body.data
264
265 expect(videos.length).to.equal(1)
266
267 server.video = videos.find(video => video.name === 'my super name')
268 })
269
270 it('Should send a request to change ownership of a video', async function () {
271 this.timeout(15000)
272
273 await changeVideoOwnership(server.url, firstUserAccessToken, server.video.id, secondUser.username)
274 })
275
276 it('Should only return a request to change ownership for the second user', async function () {
277 const resFirstUser = await getVideoChangeOwnershipList(server.url, firstUserAccessToken)
278
279 expect(resFirstUser.body.total).to.equal(0)
280 expect(resFirstUser.body.data).to.be.an('array')
281 expect(resFirstUser.body.data.length).to.equal(0)
282
283 const resSecondUser = await getVideoChangeOwnershipList(server.url, secondUserAccessToken)
284
285 expect(resSecondUser.body.total).to.equal(1)
286 expect(resSecondUser.body.data).to.be.an('array')
287 expect(resSecondUser.body.data.length).to.equal(1)
288
289 lastRequestChangeOwnershipId = resSecondUser.body.data[0].id
290 })
291
292 it('Should not be possible to accept the change of ownership from second user because of exceeded quota', async function () {
293 this.timeout(10000)
294
295 const secondUserInformationResponse = await getMyUserInformation(server.url, secondUserAccessToken)
296 const secondUserInformation: User = secondUserInformationResponse.body
297 const channelId = secondUserInformation.videoChannels[0].id
298
299 await acceptChangeOwnership(
300 server.url,
301 secondUserAccessToken,
302 lastRequestChangeOwnershipId,
303 channelId,
304 HttpStatusCode.PAYLOAD_TOO_LARGE_413
305 )
306 })
307
308 after(async function () {
309 await cleanupTests([ server ])
310 })
311 })