video-change-ownership.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import { expect } from 'chai'
  3. import {
  4. ChangeOwnershipCommand,
  5. cleanupTests,
  6. createMultipleServers,
  7. createSingleServer,
  8. doubleFollow,
  9. PeerTubeServer,
  10. setAccessTokensToServers,
  11. setDefaultVideoChannel,
  12. waitJobs
  13. } from '@shared/server-commands'
  14. import { HttpStatusCode, VideoPrivacy } from '@shared/models'
  15. describe('Test video change ownership - nominal', function () {
  16. let servers: PeerTubeServer[] = []
  17. const firstUser = 'first'
  18. const secondUser = 'second'
  19. let firstUserToken = ''
  20. let firstUserChannelId: number
  21. let secondUserToken = ''
  22. let secondUserChannelId: number
  23. let lastRequestId: number
  24. let liveId: number
  25. let command: ChangeOwnershipCommand
  26. before(async function () {
  27. this.timeout(50000)
  28. servers = await createMultipleServers(2)
  29. await setAccessTokensToServers(servers)
  30. await setDefaultVideoChannel(servers)
  31. await servers[0].config.updateCustomSubConfig({
  32. newConfig: {
  33. transcoding: {
  34. enabled: false
  35. },
  36. live: {
  37. enabled: true
  38. }
  39. }
  40. })
  41. firstUserToken = await servers[0].users.generateUserAndToken(firstUser)
  42. secondUserToken = await servers[0].users.generateUserAndToken(secondUser)
  43. {
  44. const { videoChannels } = await servers[0].users.getMyInfo({ token: firstUserToken })
  45. firstUserChannelId = videoChannels[0].id
  46. }
  47. {
  48. const { videoChannels } = await servers[0].users.getMyInfo({ token: secondUserToken })
  49. secondUserChannelId = videoChannels[0].id
  50. }
  51. {
  52. const attributes = {
  53. name: 'my super name',
  54. description: 'my super description'
  55. }
  56. const { id } = await servers[0].videos.upload({ token: firstUserToken, attributes })
  57. servers[0].store.videoCreated = await servers[0].videos.get({ id })
  58. }
  59. {
  60. const attributes = { name: 'live', channelId: firstUserChannelId, privacy: VideoPrivacy.PUBLIC }
  61. const video = await servers[0].live.create({ token: firstUserToken, fields: attributes })
  62. liveId = video.id
  63. }
  64. command = servers[0].changeOwnership
  65. await doubleFollow(servers[0], servers[1])
  66. })
  67. it('Should not have video change ownership', async function () {
  68. {
  69. const body = await command.list({ token: firstUserToken })
  70. expect(body.total).to.equal(0)
  71. expect(body.data).to.be.an('array')
  72. expect(body.data.length).to.equal(0)
  73. }
  74. {
  75. const body = await command.list({ token: secondUserToken })
  76. expect(body.total).to.equal(0)
  77. expect(body.data).to.be.an('array')
  78. expect(body.data.length).to.equal(0)
  79. }
  80. })
  81. it('Should send a request to change ownership of a video', async function () {
  82. this.timeout(15000)
  83. await command.create({ token: firstUserToken, videoId: servers[0].store.videoCreated.id, username: secondUser })
  84. })
  85. it('Should only return a request to change ownership for the second user', async function () {
  86. {
  87. const body = await command.list({ token: firstUserToken })
  88. expect(body.total).to.equal(0)
  89. expect(body.data).to.be.an('array')
  90. expect(body.data.length).to.equal(0)
  91. }
  92. {
  93. const body = await command.list({ token: secondUserToken })
  94. expect(body.total).to.equal(1)
  95. expect(body.data).to.be.an('array')
  96. expect(body.data.length).to.equal(1)
  97. lastRequestId = body.data[0].id
  98. }
  99. })
  100. it('Should accept the same change ownership request without crashing', async function () {
  101. this.timeout(10000)
  102. await command.create({ token: firstUserToken, videoId: servers[0].store.videoCreated.id, username: secondUser })
  103. })
  104. it('Should not create multiple change ownership requests while one is waiting', async function () {
  105. this.timeout(10000)
  106. const body = await command.list({ token: secondUserToken })
  107. expect(body.total).to.equal(1)
  108. expect(body.data).to.be.an('array')
  109. expect(body.data.length).to.equal(1)
  110. })
  111. it('Should not be possible to refuse the change of ownership from first user', async function () {
  112. this.timeout(10000)
  113. await command.refuse({ token: firstUserToken, ownershipId: lastRequestId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
  114. })
  115. it('Should be possible to refuse the change of ownership from second user', async function () {
  116. this.timeout(10000)
  117. await command.refuse({ token: secondUserToken, ownershipId: lastRequestId })
  118. })
  119. it('Should send a new request to change ownership of a video', async function () {
  120. this.timeout(15000)
  121. await command.create({ token: firstUserToken, videoId: servers[0].store.videoCreated.id, username: secondUser })
  122. })
  123. it('Should return two requests to change ownership for the second user', async function () {
  124. {
  125. const body = await command.list({ token: firstUserToken })
  126. expect(body.total).to.equal(0)
  127. expect(body.data).to.be.an('array')
  128. expect(body.data.length).to.equal(0)
  129. }
  130. {
  131. const body = await command.list({ token: secondUserToken })
  132. expect(body.total).to.equal(2)
  133. expect(body.data).to.be.an('array')
  134. expect(body.data.length).to.equal(2)
  135. lastRequestId = body.data[0].id
  136. }
  137. })
  138. it('Should not be possible to accept the change of ownership from first user', async function () {
  139. this.timeout(10000)
  140. await command.accept({
  141. token: firstUserToken,
  142. ownershipId: lastRequestId,
  143. channelId: secondUserChannelId,
  144. expectedStatus: HttpStatusCode.FORBIDDEN_403
  145. })
  146. })
  147. it('Should be possible to accept the change of ownership from second user', async function () {
  148. this.timeout(10000)
  149. await command.accept({ token: secondUserToken, ownershipId: lastRequestId, channelId: secondUserChannelId })
  150. await waitJobs(servers)
  151. })
  152. it('Should have the channel of the video updated', async function () {
  153. for (const server of servers) {
  154. const video = await server.videos.get({ id: servers[0].store.videoCreated.uuid })
  155. expect(video.name).to.equal('my super name')
  156. expect(video.channel.displayName).to.equal('Main second channel')
  157. expect(video.channel.name).to.equal('second_channel')
  158. }
  159. })
  160. it('Should send a request to change ownership of a live', async function () {
  161. this.timeout(15000)
  162. await command.create({ token: firstUserToken, videoId: liveId, username: secondUser })
  163. const body = await command.list({ token: secondUserToken })
  164. expect(body.total).to.equal(3)
  165. expect(body.data.length).to.equal(3)
  166. lastRequestId = body.data[0].id
  167. })
  168. it('Should accept a live ownership change', async function () {
  169. this.timeout(20000)
  170. await command.accept({ token: secondUserToken, ownershipId: lastRequestId, channelId: secondUserChannelId })
  171. await waitJobs(servers)
  172. for (const server of servers) {
  173. const video = await server.videos.get({ id: servers[0].store.videoCreated.uuid })
  174. expect(video.name).to.equal('my super name')
  175. expect(video.channel.displayName).to.equal('Main second channel')
  176. expect(video.channel.name).to.equal('second_channel')
  177. }
  178. })
  179. after(async function () {
  180. await cleanupTests(servers)
  181. })
  182. })
  183. describe('Test video change ownership - quota too small', function () {
  184. let server: PeerTubeServer
  185. const firstUser = 'first'
  186. const secondUser = 'second'
  187. let firstUserToken = ''
  188. let secondUserToken = ''
  189. let lastRequestId: number
  190. before(async function () {
  191. this.timeout(50000)
  192. // Run one server
  193. server = await createSingleServer(1)
  194. await setAccessTokensToServers([ server ])
  195. await server.users.create({ username: secondUser, videoQuota: 10 })
  196. firstUserToken = await server.users.generateUserAndToken(firstUser)
  197. secondUserToken = await server.login.getAccessToken(secondUser)
  198. // Upload some videos on the server
  199. const attributes = {
  200. name: 'my super name',
  201. description: 'my super description'
  202. }
  203. await server.videos.upload({ token: firstUserToken, attributes })
  204. await waitJobs(server)
  205. const { data } = await server.videos.list()
  206. expect(data.length).to.equal(1)
  207. server.store.videoCreated = data.find(video => video.name === 'my super name')
  208. })
  209. it('Should send a request to change ownership of a video', async function () {
  210. this.timeout(15000)
  211. await server.changeOwnership.create({ token: firstUserToken, videoId: server.store.videoCreated.id, username: secondUser })
  212. })
  213. it('Should only return a request to change ownership for the second user', async function () {
  214. {
  215. const body = await server.changeOwnership.list({ token: firstUserToken })
  216. expect(body.total).to.equal(0)
  217. expect(body.data).to.be.an('array')
  218. expect(body.data.length).to.equal(0)
  219. }
  220. {
  221. const body = await server.changeOwnership.list({ token: secondUserToken })
  222. expect(body.total).to.equal(1)
  223. expect(body.data).to.be.an('array')
  224. expect(body.data.length).to.equal(1)
  225. lastRequestId = body.data[0].id
  226. }
  227. })
  228. it('Should not be possible to accept the change of ownership from second user because of exceeded quota', async function () {
  229. this.timeout(10000)
  230. const { videoChannels } = await server.users.getMyInfo({ token: secondUserToken })
  231. const channelId = videoChannels[0].id
  232. await server.changeOwnership.accept({
  233. token: secondUserToken,
  234. ownershipId: lastRequestId,
  235. channelId,
  236. expectedStatus: HttpStatusCode.PAYLOAD_TOO_LARGE_413
  237. })
  238. })
  239. after(async function () {
  240. await cleanupTests([ server ])
  241. })
  242. })