1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
5 ChangeOwnershipCommand,
11 setAccessTokensToServers,
12 setDefaultVideoChannel,
14 } from '@shared/server-commands'
15 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
17 const expect = chai.expect
19 describe('Test video change ownership - nominal', function () {
20 let servers: PeerTubeServer[] = []
22 const firstUser = 'first'
23 const secondUser = 'second'
25 let firstUserToken = ''
26 let firstUserChannelId: number
28 let secondUserToken = ''
29 let secondUserChannelId: number
31 let lastRequestId: number
35 let command: ChangeOwnershipCommand
37 before(async function () {
40 servers = await createMultipleServers(2)
41 await setAccessTokensToServers(servers)
42 await setDefaultVideoChannel(servers)
44 await servers[0].config.updateCustomSubConfig({
55 firstUserToken = await servers[0].users.generateUserAndToken(firstUser)
56 secondUserToken = await servers[0].users.generateUserAndToken(secondUser)
59 const { videoChannels } = await servers[0].users.getMyInfo({ token: firstUserToken })
60 firstUserChannelId = videoChannels[0].id
64 const { videoChannels } = await servers[0].users.getMyInfo({ token: secondUserToken })
65 secondUserChannelId = videoChannels[0].id
70 name: 'my super name',
71 description: 'my super description'
73 const { id } = await servers[0].videos.upload({ token: firstUserToken, attributes })
75 servers[0].store.videoCreated = await servers[0].videos.get({ id })
79 const attributes = { name: 'live', channelId: firstUserChannelId, privacy: VideoPrivacy.PUBLIC }
80 const video = await servers[0].live.create({ token: firstUserToken, fields: attributes })
85 command = servers[0].changeOwnership
87 await doubleFollow(servers[0], servers[1])
90 it('Should not have video change ownership', async function () {
92 const body = await command.list({ token: firstUserToken })
94 expect(body.total).to.equal(0)
95 expect(body.data).to.be.an('array')
96 expect(body.data.length).to.equal(0)
100 const body = await command.list({ token: secondUserToken })
102 expect(body.total).to.equal(0)
103 expect(body.data).to.be.an('array')
104 expect(body.data.length).to.equal(0)
108 it('Should send a request to change ownership of a video', async function () {
111 await command.create({ token: firstUserToken, videoId: servers[0].store.videoCreated.id, username: secondUser })
114 it('Should only return a request to change ownership for the second user', async function () {
116 const body = await command.list({ token: firstUserToken })
118 expect(body.total).to.equal(0)
119 expect(body.data).to.be.an('array')
120 expect(body.data.length).to.equal(0)
124 const body = await command.list({ token: secondUserToken })
126 expect(body.total).to.equal(1)
127 expect(body.data).to.be.an('array')
128 expect(body.data.length).to.equal(1)
130 lastRequestId = body.data[0].id
134 it('Should accept the same change ownership request without crashing', async function () {
137 await command.create({ token: firstUserToken, videoId: servers[0].store.videoCreated.id, username: secondUser })
140 it('Should not create multiple change ownership requests while one is waiting', async function () {
143 const body = await command.list({ token: secondUserToken })
145 expect(body.total).to.equal(1)
146 expect(body.data).to.be.an('array')
147 expect(body.data.length).to.equal(1)
150 it('Should not be possible to refuse the change of ownership from first user', async function () {
153 await command.refuse({ token: firstUserToken, ownershipId: lastRequestId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
156 it('Should be possible to refuse the change of ownership from second user', async function () {
159 await command.refuse({ token: secondUserToken, ownershipId: lastRequestId })
162 it('Should send a new request to change ownership of a video', async function () {
165 await command.create({ token: firstUserToken, videoId: servers[0].store.videoCreated.id, username: secondUser })
168 it('Should return two requests to change ownership for the second user', async function () {
170 const body = await command.list({ token: firstUserToken })
172 expect(body.total).to.equal(0)
173 expect(body.data).to.be.an('array')
174 expect(body.data.length).to.equal(0)
178 const body = await command.list({ token: secondUserToken })
180 expect(body.total).to.equal(2)
181 expect(body.data).to.be.an('array')
182 expect(body.data.length).to.equal(2)
184 lastRequestId = body.data[0].id
188 it('Should not be possible to accept the change of ownership from first user', async function () {
191 await command.accept({
192 token: firstUserToken,
193 ownershipId: lastRequestId,
194 channelId: secondUserChannelId,
195 expectedStatus: HttpStatusCode.FORBIDDEN_403
199 it('Should be possible to accept the change of ownership from second user', async function () {
202 await command.accept({ token: secondUserToken, ownershipId: lastRequestId, channelId: secondUserChannelId })
204 await waitJobs(servers)
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 })
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')
217 it('Should send a request to change ownership of a live', async function () {
220 await command.create({ token: firstUserToken, videoId: liveId, username: secondUser })
222 const body = await command.list({ token: secondUserToken })
224 expect(body.total).to.equal(3)
225 expect(body.data.length).to.equal(3)
227 lastRequestId = body.data[0].id
230 it('Should accept a live ownership change', async function () {
233 await command.accept({ token: secondUserToken, ownershipId: lastRequestId, channelId: secondUserChannelId })
235 await waitJobs(servers)
237 for (const server of servers) {
238 const video = await server.videos.get({ id: servers[0].store.videoCreated.uuid })
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')
246 after(async function () {
247 await cleanupTests(servers)
251 describe('Test video change ownership - quota too small', function () {
252 let server: PeerTubeServer
253 const firstUser = 'first'
254 const secondUser = 'second'
256 let firstUserToken = ''
257 let secondUserToken = ''
258 let lastRequestId: number
260 before(async function () {
264 server = await createSingleServer(1)
265 await setAccessTokensToServers([ server ])
267 await server.users.create({ username: secondUser, videoQuota: 10 })
269 firstUserToken = await server.users.generateUserAndToken(firstUser)
270 secondUserToken = await server.login.getAccessToken(secondUser)
272 // Upload some videos on the server
274 name: 'my super name',
275 description: 'my super description'
277 await server.videos.upload({ token: firstUserToken, attributes })
279 await waitJobs(server)
281 const { data } = await server.videos.list()
282 expect(data.length).to.equal(1)
284 server.store.videoCreated = data.find(video => video.name === 'my super name')
287 it('Should send a request to change ownership of a video', async function () {
290 await server.changeOwnership.create({ token: firstUserToken, videoId: server.store.videoCreated.id, username: secondUser })
293 it('Should only return a request to change ownership for the second user', async function () {
295 const body = await server.changeOwnership.list({ token: firstUserToken })
297 expect(body.total).to.equal(0)
298 expect(body.data).to.be.an('array')
299 expect(body.data.length).to.equal(0)
303 const body = await server.changeOwnership.list({ token: secondUserToken })
305 expect(body.total).to.equal(1)
306 expect(body.data).to.be.an('array')
307 expect(body.data.length).to.equal(1)
309 lastRequestId = body.data[0].id
313 it('Should not be possible to accept the change of ownership from second user because of exceeded quota', async function () {
316 const { videoChannels } = await server.users.getMyInfo({ token: secondUserToken })
317 const channelId = videoChannels[0].id
319 await server.changeOwnership.accept({
320 token: secondUserToken,
321 ownershipId: lastRequestId,
323 expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413
327 after(async function () {
328 await cleanupTests([ server ])