video-privacy.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import { expect } from 'chai'
  3. import { wait } from '@shared/core-utils'
  4. import { HttpStatusCode, VideoCreateResult, VideoPrivacy } from '@shared/models'
  5. import { cleanupTests, createSingleServer, doubleFollow, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/server-commands'
  6. describe('Test video privacy', function () {
  7. const servers: PeerTubeServer[] = []
  8. let anotherUserToken: string
  9. let privateVideoId: number
  10. let privateVideoUUID: string
  11. let internalVideoId: number
  12. let internalVideoUUID: string
  13. let unlistedVideo: VideoCreateResult
  14. let nonFederatedUnlistedVideoUUID: string
  15. let now: number
  16. const dontFederateUnlistedConfig = {
  17. federation: {
  18. videos: {
  19. federate_unlisted: false
  20. }
  21. }
  22. }
  23. before(async function () {
  24. this.timeout(50000)
  25. // Run servers
  26. servers.push(await createSingleServer(1, dontFederateUnlistedConfig))
  27. servers.push(await createSingleServer(2))
  28. // Get the access tokens
  29. await setAccessTokensToServers(servers)
  30. // Server 1 and server 2 follow each other
  31. await doubleFollow(servers[0], servers[1])
  32. })
  33. describe('Private and internal videos', function () {
  34. it('Should upload a private and internal videos on server 1', async function () {
  35. this.timeout(50000)
  36. for (const privacy of [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL ]) {
  37. const attributes = { privacy }
  38. await servers[0].videos.upload({ attributes })
  39. }
  40. await waitJobs(servers)
  41. })
  42. it('Should not have these private and internal videos on server 2', async function () {
  43. const { total, data } = await servers[1].videos.list()
  44. expect(total).to.equal(0)
  45. expect(data).to.have.lengthOf(0)
  46. })
  47. it('Should not list the private and internal videos for an unauthenticated user on server 1', async function () {
  48. const { total, data } = await servers[0].videos.list()
  49. expect(total).to.equal(0)
  50. expect(data).to.have.lengthOf(0)
  51. })
  52. it('Should not list the private video and list the internal video for an authenticated user on server 1', async function () {
  53. const { total, data } = await servers[0].videos.listWithToken()
  54. expect(total).to.equal(1)
  55. expect(data).to.have.lengthOf(1)
  56. expect(data[0].privacy.id).to.equal(VideoPrivacy.INTERNAL)
  57. })
  58. it('Should list my (private and internal) videos', async function () {
  59. const { total, data } = await servers[0].videos.listMyVideos()
  60. expect(total).to.equal(2)
  61. expect(data).to.have.lengthOf(2)
  62. const privateVideo = data.find(v => v.privacy.id === VideoPrivacy.PRIVATE)
  63. privateVideoId = privateVideo.id
  64. privateVideoUUID = privateVideo.uuid
  65. const internalVideo = data.find(v => v.privacy.id === VideoPrivacy.INTERNAL)
  66. internalVideoId = internalVideo.id
  67. internalVideoUUID = internalVideo.uuid
  68. })
  69. it('Should not be able to watch the private/internal video with non authenticated user', async function () {
  70. await servers[0].videos.get({ id: privateVideoUUID, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
  71. await servers[0].videos.get({ id: internalVideoUUID, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
  72. })
  73. it('Should not be able to watch the private video with another user', async function () {
  74. this.timeout(10000)
  75. const user = {
  76. username: 'hello',
  77. password: 'super password'
  78. }
  79. await servers[0].users.create({ username: user.username, password: user.password })
  80. anotherUserToken = await servers[0].login.getAccessToken(user)
  81. await servers[0].videos.getWithToken({
  82. token: anotherUserToken,
  83. id: privateVideoUUID,
  84. expectedStatus: HttpStatusCode.FORBIDDEN_403
  85. })
  86. })
  87. it('Should be able to watch the internal video with another user', async function () {
  88. await servers[0].videos.getWithToken({ token: anotherUserToken, id: internalVideoUUID })
  89. })
  90. it('Should be able to watch the private video with the correct user', async function () {
  91. await servers[0].videos.getWithToken({ id: privateVideoUUID })
  92. })
  93. })
  94. describe('Unlisted videos', function () {
  95. it('Should upload an unlisted video on server 2', async function () {
  96. this.timeout(120000)
  97. const attributes = {
  98. name: 'unlisted video',
  99. privacy: VideoPrivacy.UNLISTED
  100. }
  101. await servers[1].videos.upload({ attributes })
  102. // Server 2 has transcoding enabled
  103. await waitJobs(servers)
  104. })
  105. it('Should not have this unlisted video listed on server 1 and 2', async function () {
  106. for (const server of servers) {
  107. const { total, data } = await server.videos.list()
  108. expect(total).to.equal(0)
  109. expect(data).to.have.lengthOf(0)
  110. }
  111. })
  112. it('Should list my (unlisted) videos', async function () {
  113. const { total, data } = await servers[1].videos.listMyVideos()
  114. expect(total).to.equal(1)
  115. expect(data).to.have.lengthOf(1)
  116. unlistedVideo = data[0]
  117. })
  118. it('Should not be able to get this unlisted video using its id', async function () {
  119. await servers[1].videos.get({ id: unlistedVideo.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
  120. })
  121. it('Should be able to get this unlisted video using its uuid/shortUUID', async function () {
  122. for (const server of servers) {
  123. for (const id of [ unlistedVideo.uuid, unlistedVideo.shortUUID ]) {
  124. const video = await server.videos.get({ id })
  125. expect(video.name).to.equal('unlisted video')
  126. }
  127. }
  128. })
  129. it('Should upload a non-federating unlisted video to server 1', async function () {
  130. this.timeout(30000)
  131. const attributes = {
  132. name: 'unlisted video',
  133. privacy: VideoPrivacy.UNLISTED
  134. }
  135. await servers[0].videos.upload({ attributes })
  136. await waitJobs(servers)
  137. })
  138. it('Should list my new unlisted video', async function () {
  139. const { total, data } = await servers[0].videos.listMyVideos()
  140. expect(total).to.equal(3)
  141. expect(data).to.have.lengthOf(3)
  142. nonFederatedUnlistedVideoUUID = data[0].uuid
  143. })
  144. it('Should be able to get non-federated unlisted video from origin', async function () {
  145. const video = await servers[0].videos.get({ id: nonFederatedUnlistedVideoUUID })
  146. expect(video.name).to.equal('unlisted video')
  147. })
  148. it('Should not be able to get non-federated unlisted video from federated server', async function () {
  149. await servers[1].videos.get({ id: nonFederatedUnlistedVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
  150. })
  151. })
  152. describe('Privacy update', function () {
  153. it('Should update the private and internal videos to public on server 1', async function () {
  154. this.timeout(100000)
  155. now = Date.now()
  156. {
  157. const attributes = {
  158. name: 'private video becomes public',
  159. privacy: VideoPrivacy.PUBLIC
  160. }
  161. await servers[0].videos.update({ id: privateVideoId, attributes })
  162. }
  163. {
  164. const attributes = {
  165. name: 'internal video becomes public',
  166. privacy: VideoPrivacy.PUBLIC
  167. }
  168. await servers[0].videos.update({ id: internalVideoId, attributes })
  169. }
  170. await wait(10000)
  171. await waitJobs(servers)
  172. })
  173. it('Should have this new public video listed on server 1 and 2', async function () {
  174. for (const server of servers) {
  175. const { total, data } = await server.videos.list()
  176. expect(total).to.equal(2)
  177. expect(data).to.have.lengthOf(2)
  178. const privateVideo = data.find(v => v.name === 'private video becomes public')
  179. const internalVideo = data.find(v => v.name === 'internal video becomes public')
  180. expect(privateVideo).to.not.be.undefined
  181. expect(internalVideo).to.not.be.undefined
  182. expect(new Date(privateVideo.publishedAt).getTime()).to.be.at.least(now)
  183. // We don't change the publish date of internal videos
  184. expect(new Date(internalVideo.publishedAt).getTime()).to.be.below(now)
  185. expect(privateVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
  186. expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
  187. }
  188. })
  189. it('Should set these videos as private and internal', async function () {
  190. this.timeout(10000)
  191. await servers[0].videos.update({ id: internalVideoId, attributes: { privacy: VideoPrivacy.PRIVATE } })
  192. await servers[0].videos.update({ id: privateVideoId, attributes: { privacy: VideoPrivacy.INTERNAL } })
  193. await waitJobs(servers)
  194. for (const server of servers) {
  195. const { total, data } = await server.videos.list()
  196. expect(total).to.equal(0)
  197. expect(data).to.have.lengthOf(0)
  198. }
  199. {
  200. const { total, data } = await servers[0].videos.listMyVideos()
  201. expect(total).to.equal(3)
  202. expect(data).to.have.lengthOf(3)
  203. const privateVideo = data.find(v => v.name === 'private video becomes public')
  204. const internalVideo = data.find(v => v.name === 'internal video becomes public')
  205. expect(privateVideo).to.not.be.undefined
  206. expect(internalVideo).to.not.be.undefined
  207. expect(privateVideo.privacy.id).to.equal(VideoPrivacy.INTERNAL)
  208. expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PRIVATE)
  209. }
  210. })
  211. })
  212. after(async function () {
  213. await cleanupTests(servers)
  214. })
  215. })