1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
6 ChangeOwnershipCommand,
12 setAccessTokensToServers,
13 setDefaultVideoChannel,
15 } from '@shared/server-commands'
16 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
18 const expect = chai.expect
20 describe('Test video change ownership - nominal', function () {
21 let servers: PeerTubeServer[] = []
23 const firstUser = 'first'
24 const secondUser = 'second'
26 let firstUserToken = ''
27 let firstUserChannelId: number
29 let secondUserToken = ''
30 let secondUserChannelId: number
32 let lastRequestId: number
36 let command: ChangeOwnershipCommand
38 before(async function () {
41 servers = await createMultipleServers(2)
42 await setAccessTokensToServers(servers)
43 await setDefaultVideoChannel(servers)
45 await servers[0].config.updateCustomSubConfig({
56 firstUserToken = await servers[0].users.generateUserAndToken(firstUser)
57 secondUserToken = await servers[0].users.generateUserAndToken(secondUser)
60 const { videoChannels } = await servers[0].users.getMyInfo({ token: firstUserToken })
61 firstUserChannelId = videoChannels[0].id
65 const { videoChannels } = await servers[0].users.getMyInfo({ token: secondUserToken })
66 secondUserChannelId = videoChannels[0].id
71 name: 'my super name',
72 description: 'my super description'
74 const { id } = await servers[0].videos.upload({ token: firstUserToken, attributes })
76 servers[0].store.videoCreated = await servers[0].videos.get({ id })
80 const attributes = { name: 'live', channelId: firstUserChannelId, privacy: VideoPrivacy.PUBLIC }
81 const video = await servers[0].live.create({ token: firstUserToken, fields: attributes })
86 command = servers[0].changeOwnership
88 await doubleFollow(servers[0], servers[1])
91 it('Should not have video change ownership', async function () {
93 const body = await command.list({ token: firstUserToken })
95 expect(body.total).to.equal(0)
96 expect(body.data).to.be.an('array')
97 expect(body.data.length).to.equal(0)
101 const body = await command.list({ token: secondUserToken })
103 expect(body.total).to.equal(0)
104 expect(body.data).to.be.an('array')
105 expect(body.data.length).to.equal(0)
109 it('Should send a request to change ownership of a video', async function () {
112 await command.create({ token: firstUserToken, videoId: servers[0].store.videoCreated.id, username: secondUser })
115 it('Should only return a request to change ownership for the second user', async function () {
117 const body = await command.list({ token: firstUserToken })
119 expect(body.total).to.equal(0)
120 expect(body.data).to.be.an('array')
121 expect(body.data.length).to.equal(0)
125 const body = await command.list({ token: secondUserToken })
127 expect(body.total).to.equal(1)
128 expect(body.data).to.be.an('array')
129 expect(body.data.length).to.equal(1)
131 lastRequestId = body.data[0].id
135 it('Should accept the same change ownership request without crashing', async function () {
138 await command.create({ token: firstUserToken, videoId: servers[0].store.videoCreated.id, username: secondUser })
141 it('Should not create multiple change ownership requests while one is waiting', async function () {
144 const body = await command.list({ token: secondUserToken })
146 expect(body.total).to.equal(1)
147 expect(body.data).to.be.an('array')
148 expect(body.data.length).to.equal(1)
151 it('Should not be possible to refuse the change of ownership from first user', async function () {
154 await command.refuse({ token: firstUserToken, ownershipId: lastRequestId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
157 it('Should be possible to refuse the change of ownership from second user', async function () {
160 await command.refuse({ token: secondUserToken, ownershipId: lastRequestId })
163 it('Should send a new request to change ownership of a video', async function () {
166 await command.create({ token: firstUserToken, videoId: servers[0].store.videoCreated.id, username: secondUser })
169 it('Should return two requests to change ownership for the second user', async function () {
171 const body = await command.list({ token: firstUserToken })
173 expect(body.total).to.equal(0)
174 expect(body.data).to.be.an('array')
175 expect(body.data.length).to.equal(0)
179 const body = await command.list({ token: secondUserToken })
181 expect(body.total).to.equal(2)
182 expect(body.data).to.be.an('array')
183 expect(body.data.length).to.equal(2)
185 lastRequestId = body.data[0].id
189 it('Should not be possible to accept the change of ownership from first user', async function () {
192 await command.accept({
193 token: firstUserToken,
194 ownershipId: lastRequestId,
195 channelId: secondUserChannelId,
196 expectedStatus: HttpStatusCode.FORBIDDEN_403
200 it('Should be possible to accept the change of ownership from second user', async function () {
203 await command.accept({ token: secondUserToken, ownershipId: lastRequestId, channelId: secondUserChannelId })
205 await waitJobs(servers)
208 it('Should have the channel of the video updated', async function () {
209 for (const server of servers) {
210 const video = await server.videos.get({ id: servers[0].store.videoCreated.uuid })
212 expect(video.name).to.equal('my super name')
213 expect(video.channel.displayName).to.equal('Main second channel')
214 expect(video.channel.name).to.equal('second_channel')
218 it('Should send a request to change ownership of a live', async function () {
221 await command.create({ token: firstUserToken, videoId: liveId, username: secondUser })
223 const body = await command.list({ token: secondUserToken })
225 expect(body.total).to.equal(3)
226 expect(body.data.length).to.equal(3)
228 lastRequestId = body.data[0].id
231 it('Should accept a live ownership change', async function () {
234 await command.accept({ token: secondUserToken, ownershipId: lastRequestId, channelId: secondUserChannelId })
236 await waitJobs(servers)
238 for (const server of servers) {
239 const video = await server.videos.get({ id: servers[0].store.videoCreated.uuid })
241 expect(video.name).to.equal('my super name')
242 expect(video.channel.displayName).to.equal('Main second channel')
243 expect(video.channel.name).to.equal('second_channel')
247 after(async function () {
248 await cleanupTests(servers)
252 describe('Test video change ownership - quota too small', function () {
253 let server: PeerTubeServer
254 const firstUser = 'first'
255 const secondUser = 'second'
257 let firstUserToken = ''
258 let secondUserToken = ''
259 let lastRequestId: number
261 before(async function () {
265 server = await createSingleServer(1)
266 await setAccessTokensToServers([ server ])
268 await server.users.create({ username: secondUser, videoQuota: 10 })
270 firstUserToken = await server.users.generateUserAndToken(firstUser)
271 secondUserToken = await server.login.getAccessToken(secondUser)
273 // Upload some videos on the server
275 name: 'my super name',
276 description: 'my super description'
278 await server.videos.upload({ token: firstUserToken, attributes })
280 await waitJobs(server)
282 const { data } = await server.videos.list()
283 expect(data.length).to.equal(1)
285 server.store.videoCreated = data.find(video => video.name === 'my super name')
288 it('Should send a request to change ownership of a video', async function () {
291 await server.changeOwnership.create({ token: firstUserToken, videoId: server.store.videoCreated.id, username: secondUser })
294 it('Should only return a request to change ownership for the second user', async function () {
296 const body = await server.changeOwnership.list({ token: firstUserToken })
298 expect(body.total).to.equal(0)
299 expect(body.data).to.be.an('array')
300 expect(body.data.length).to.equal(0)
304 const body = await server.changeOwnership.list({ token: secondUserToken })
306 expect(body.total).to.equal(1)
307 expect(body.data).to.be.an('array')
308 expect(body.data.length).to.equal(1)
310 lastRequestId = body.data[0].id
314 it('Should not be possible to accept the change of ownership from second user because of exceeded quota', async function () {
317 const { videoChannels } = await server.users.getMyInfo({ token: secondUserToken })
318 const channelId = videoChannels[0].id
320 await server.changeOwnership.accept({
321 token: secondUserToken,
322 ownershipId: lastRequestId,
324 expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413
328 after(async function () {
329 await cleanupTests([ server ])