user-videos.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import { expect } from 'chai'
  3. import { HttpStatusCode } from '@peertube/peertube-models'
  4. import {
  5. cleanupTests,
  6. createSingleServer,
  7. PeerTubeServer,
  8. setAccessTokensToServers,
  9. setDefaultAccountAvatar,
  10. setDefaultChannelAvatar,
  11. waitJobs
  12. } from '@peertube/peertube-server-commands'
  13. describe('Test user videos', function () {
  14. let server: PeerTubeServer
  15. let videoId: number
  16. let videoId2: number
  17. let token: string
  18. let anotherUserToken: string
  19. before(async function () {
  20. this.timeout(120000)
  21. server = await createSingleServer(1)
  22. await setAccessTokensToServers([ server ])
  23. await setDefaultChannelAvatar([ server ])
  24. await setDefaultAccountAvatar([ server ])
  25. await server.videos.quickUpload({ name: 'root video' })
  26. await server.videos.quickUpload({ name: 'root video 2' })
  27. token = await server.users.generateUserAndToken('user')
  28. anotherUserToken = await server.users.generateUserAndToken('user2')
  29. })
  30. describe('List my videos', function () {
  31. it('Should list my videos', async function () {
  32. const { data, total } = await server.videos.listMyVideos()
  33. expect(total).to.equal(2)
  34. expect(data).to.have.lengthOf(2)
  35. })
  36. })
  37. describe('Upload', function () {
  38. it('Should upload the video with the correct token', async function () {
  39. await server.videos.upload({ token })
  40. const { data } = await server.videos.list()
  41. const video = data[0]
  42. expect(video.account.name).to.equal('user')
  43. videoId = video.id
  44. })
  45. it('Should upload the video again with the correct token', async function () {
  46. const { id } = await server.videos.upload({ token })
  47. videoId2 = id
  48. })
  49. })
  50. describe('Ratings', function () {
  51. it('Should retrieve a video rating', async function () {
  52. await server.videos.rate({ id: videoId, token, rating: 'like' })
  53. const rating = await server.users.getMyRating({ token, videoId })
  54. expect(rating.videoId).to.equal(videoId)
  55. expect(rating.rating).to.equal('like')
  56. })
  57. it('Should retrieve ratings list', async function () {
  58. await server.videos.rate({ id: videoId, token, rating: 'like' })
  59. const body = await server.accounts.listRatings({ accountName: 'user', token })
  60. expect(body.total).to.equal(1)
  61. expect(body.data[0].video.id).to.equal(videoId)
  62. expect(body.data[0].rating).to.equal('like')
  63. })
  64. it('Should retrieve ratings list by rating type', async function () {
  65. {
  66. const body = await server.accounts.listRatings({ accountName: 'user', token, rating: 'like' })
  67. expect(body.data.length).to.equal(1)
  68. }
  69. {
  70. const body = await server.accounts.listRatings({ accountName: 'user', token, rating: 'dislike' })
  71. expect(body.data.length).to.equal(0)
  72. }
  73. })
  74. })
  75. describe('Remove video', function () {
  76. it('Should not be able to remove the video with an incorrect token', async function () {
  77. await server.videos.remove({ token: 'bad_token', id: videoId, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
  78. })
  79. it('Should not be able to remove the video with the token of another account', async function () {
  80. await server.videos.remove({ token: anotherUserToken, id: videoId, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
  81. })
  82. it('Should be able to remove the video with the correct token', async function () {
  83. await server.videos.remove({ token, id: videoId })
  84. await server.videos.remove({ token, id: videoId2 })
  85. })
  86. })
  87. describe('My videos & quotas', function () {
  88. it('Should be able to upload a video with a user', async function () {
  89. this.timeout(30000)
  90. const attributes = {
  91. name: 'super user video',
  92. fixture: 'video_short.webm'
  93. }
  94. await server.videos.upload({ token, attributes })
  95. await server.channels.create({ token, attributes: { name: 'other_channel' } })
  96. })
  97. it('Should have video quota updated', async function () {
  98. const quota = await server.users.getMyQuotaUsed({ token })
  99. expect(quota.videoQuotaUsed).to.equal(218910)
  100. expect(quota.videoQuotaUsedDaily).to.equal(218910)
  101. const { data } = await server.users.list()
  102. const tmpUser = data.find(u => u.username === 'user')
  103. expect(tmpUser.videoQuotaUsed).to.equal(218910)
  104. expect(tmpUser.videoQuotaUsedDaily).to.equal(218910)
  105. })
  106. it('Should be able to list my videos', async function () {
  107. const { total, data } = await server.videos.listMyVideos({ token })
  108. expect(total).to.equal(1)
  109. expect(data).to.have.lengthOf(1)
  110. const video = data[0]
  111. expect(video.name).to.equal('super user video')
  112. expect(video.thumbnailPath).to.not.be.null
  113. expect(video.previewPath).to.not.be.null
  114. })
  115. it('Should be able to filter by channel in my videos', async function () {
  116. const myInfo = await server.users.getMyInfo({ token })
  117. const mainChannel = myInfo.videoChannels.find(c => c.name !== 'other_channel')
  118. const otherChannel = myInfo.videoChannels.find(c => c.name === 'other_channel')
  119. {
  120. const { total, data } = await server.videos.listMyVideos({ token, channelId: mainChannel.id })
  121. expect(total).to.equal(1)
  122. expect(data).to.have.lengthOf(1)
  123. const video = data[0]
  124. expect(video.name).to.equal('super user video')
  125. expect(video.thumbnailPath).to.not.be.null
  126. expect(video.previewPath).to.not.be.null
  127. }
  128. {
  129. const { total, data } = await server.videos.listMyVideos({ token, channelId: otherChannel.id })
  130. expect(total).to.equal(0)
  131. expect(data).to.have.lengthOf(0)
  132. }
  133. })
  134. it('Should be able to search in my videos', async function () {
  135. {
  136. const { total, data } = await server.videos.listMyVideos({ token, sort: '-createdAt', search: 'user video' })
  137. expect(total).to.equal(1)
  138. expect(data).to.have.lengthOf(1)
  139. }
  140. {
  141. const { total, data } = await server.videos.listMyVideos({ token, sort: '-createdAt', search: 'toto' })
  142. expect(total).to.equal(0)
  143. expect(data).to.have.lengthOf(0)
  144. }
  145. })
  146. it('Should disable web videos, enable HLS, and update my quota', async function () {
  147. this.timeout(160000)
  148. {
  149. const config = await server.config.getCustomConfig()
  150. config.transcoding.webVideos.enabled = false
  151. config.transcoding.hls.enabled = true
  152. config.transcoding.enabled = true
  153. await server.config.updateExistingConfig({ newConfig: config })
  154. }
  155. {
  156. const attributes = {
  157. name: 'super user video 2',
  158. fixture: 'video_short.webm'
  159. }
  160. await server.videos.upload({ token, attributes })
  161. await waitJobs([ server ])
  162. }
  163. {
  164. const data = await server.users.getMyQuotaUsed({ token })
  165. expect(data.videoQuotaUsed).to.be.greaterThan(220000)
  166. expect(data.videoQuotaUsedDaily).to.be.greaterThan(220000)
  167. }
  168. })
  169. })
  170. after(async function () {
  171. await cleanupTests([ server ])
  172. })
  173. })