1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
5 ChangeOwnershipCommand,
11 setAccessTokensToServers,
12 setDefaultVideoChannel,
14 } from '@shared/server-commands'
15 import { HttpStatusCode, VideoPrivacy } from '@shared/models'
17 describe('Test video change ownership - nominal', function () {
18 let servers: PeerTubeServer[] = []
20 const firstUser = 'first'
21 const secondUser = 'second'
23 let firstUserToken = ''
24 let firstUserChannelId: number
26 let secondUserToken = ''
27 let secondUserChannelId: number
29 let lastRequestId: number
33 let command: ChangeOwnershipCommand
35 before(async function () {
38 servers = await createMultipleServers(2)
39 await setAccessTokensToServers(servers)
40 await setDefaultVideoChannel(servers)
42 await servers[0].config.updateCustomSubConfig({
53 firstUserToken = await servers[0].users.generateUserAndToken(firstUser)
54 secondUserToken = await servers[0].users.generateUserAndToken(secondUser)
57 const { videoChannels } = await servers[0].users.getMyInfo({ token: firstUserToken })
58 firstUserChannelId = videoChannels[0].id
62 const { videoChannels } = await servers[0].users.getMyInfo({ token: secondUserToken })
63 secondUserChannelId = videoChannels[0].id
68 name: 'my super name',
69 description: 'my super description'
71 const { id } = await servers[0].videos.upload({ token: firstUserToken, attributes })
73 servers[0].store.videoCreated = await servers[0].videos.get({ id })
77 const attributes = { name: 'live', channelId: firstUserChannelId, privacy: VideoPrivacy.PUBLIC }
78 const video = await servers[0].live.create({ token: firstUserToken, fields: attributes })
83 command = servers[0].changeOwnership
85 await doubleFollow(servers[0], servers[1])
88 it('Should not have video change ownership', async function () {
90 const body = await command.list({ token: firstUserToken })
92 expect(body.total).to.equal(0)
93 expect(body.data).to.be.an('array')
94 expect(body.data.length).to.equal(0)
98 const body = await command.list({ token: secondUserToken })
100 expect(body.total).to.equal(0)
101 expect(body.data).to.be.an('array')
102 expect(body.data.length).to.equal(0)
106 it('Should send a request to change ownership of a video', async function () {
109 await command.create({ token: firstUserToken, videoId: servers[0].store.videoCreated.id, username: secondUser })
112 it('Should only return a request to change ownership for the second user', async function () {
114 const body = await command.list({ token: firstUserToken })
116 expect(body.total).to.equal(0)
117 expect(body.data).to.be.an('array')
118 expect(body.data.length).to.equal(0)
122 const body = await command.list({ token: secondUserToken })
124 expect(body.total).to.equal(1)
125 expect(body.data).to.be.an('array')
126 expect(body.data.length).to.equal(1)
128 lastRequestId = body.data[0].id
132 it('Should accept the same change ownership request without crashing', async function () {
135 await command.create({ token: firstUserToken, videoId: servers[0].store.videoCreated.id, username: secondUser })
138 it('Should not create multiple change ownership requests while one is waiting', async function () {
141 const body = await command.list({ token: secondUserToken })
143 expect(body.total).to.equal(1)
144 expect(body.data).to.be.an('array')
145 expect(body.data.length).to.equal(1)
148 it('Should not be possible to refuse the change of ownership from first user', async function () {
151 await command.refuse({ token: firstUserToken, ownershipId: lastRequestId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
154 it('Should be possible to refuse the change of ownership from second user', async function () {
157 await command.refuse({ token: secondUserToken, ownershipId: lastRequestId })
160 it('Should send a new request to change ownership of a video', async function () {
163 await command.create({ token: firstUserToken, videoId: servers[0].store.videoCreated.id, username: secondUser })
166 it('Should return two requests to change ownership for the second user', async function () {
168 const body = await command.list({ token: firstUserToken })
170 expect(body.total).to.equal(0)
171 expect(body.data).to.be.an('array')
172 expect(body.data.length).to.equal(0)
176 const body = await command.list({ token: secondUserToken })
178 expect(body.total).to.equal(2)
179 expect(body.data).to.be.an('array')
180 expect(body.data.length).to.equal(2)
182 lastRequestId = body.data[0].id
186 it('Should not be possible to accept the change of ownership from first user', async function () {
189 await command.accept({
190 token: firstUserToken,
191 ownershipId: lastRequestId,
192 channelId: secondUserChannelId,
193 expectedStatus: HttpStatusCode.FORBIDDEN_403
197 it('Should be possible to accept the change of ownership from second user', async function () {
200 await command.accept({ token: secondUserToken, ownershipId: lastRequestId, channelId: secondUserChannelId })
202 await waitJobs(servers)
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 })
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')
215 it('Should send a request to change ownership of a live', async function () {
218 await command.create({ token: firstUserToken, videoId: liveId, username: secondUser })
220 const body = await command.list({ token: secondUserToken })
222 expect(body.total).to.equal(3)
223 expect(body.data.length).to.equal(3)
225 lastRequestId = body.data[0].id
228 it('Should accept a live ownership change', async function () {
231 await command.accept({ token: secondUserToken, ownershipId: lastRequestId, channelId: secondUserChannelId })
233 await waitJobs(servers)
235 for (const server of servers) {
236 const video = await server.videos.get({ id: servers[0].store.videoCreated.uuid })
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')
244 after(async function () {
245 await cleanupTests(servers)
249 describe('Test video change ownership - quota too small', function () {
250 let server: PeerTubeServer
251 const firstUser = 'first'
252 const secondUser = 'second'
254 let firstUserToken = ''
255 let secondUserToken = ''
256 let lastRequestId: number
258 before(async function () {
262 server = await createSingleServer(1)
263 await setAccessTokensToServers([ server ])
265 await server.users.create({ username: secondUser, videoQuota: 10 })
267 firstUserToken = await server.users.generateUserAndToken(firstUser)
268 secondUserToken = await server.login.getAccessToken(secondUser)
270 // Upload some videos on the server
272 name: 'my super name',
273 description: 'my super description'
275 await server.videos.upload({ token: firstUserToken, attributes })
277 await waitJobs(server)
279 const { data } = await server.videos.list()
280 expect(data.length).to.equal(1)
282 server.store.videoCreated = data.find(video => video.name === 'my super name')
285 it('Should send a request to change ownership of a video', async function () {
288 await server.changeOwnership.create({ token: firstUserToken, videoId: server.store.videoCreated.id, username: secondUser })
291 it('Should only return a request to change ownership for the second user', async function () {
293 const body = await server.changeOwnership.list({ token: firstUserToken })
295 expect(body.total).to.equal(0)
296 expect(body.data).to.be.an('array')
297 expect(body.data.length).to.equal(0)
301 const body = await server.changeOwnership.list({ token: secondUserToken })
303 expect(body.total).to.equal(1)
304 expect(body.data).to.be.an('array')
305 expect(body.data.length).to.equal(1)
307 lastRequestId = body.data[0].id
311 it('Should not be possible to accept the change of ownership from second user because of exceeded quota', async function () {
314 const { videoChannels } = await server.users.getMyInfo({ token: secondUserToken })
315 const channelId = videoChannels[0].id
317 await server.changeOwnership.accept({
318 token: secondUserToken,
319 ownershipId: lastRequestId,
321 expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413
325 after(async function () {
326 await cleanupTests([ server ])