video-blacklist.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import 'mocha'
  3. import {
  4. cleanupTests,
  5. createUser,
  6. doubleFollow,
  7. flushAndRunMultipleServers,
  8. getBlacklistedVideosList,
  9. getVideo,
  10. getVideoWithToken,
  11. makePostBodyRequest,
  12. makePutBodyRequest,
  13. removeVideoFromBlacklist,
  14. ServerInfo,
  15. setAccessTokensToServers,
  16. uploadVideo,
  17. userLogin,
  18. waitJobs
  19. } from '../../../../shared/extra-utils'
  20. import {
  21. checkBadCountPagination,
  22. checkBadSortPagination,
  23. checkBadStartPagination
  24. } from '../../../../shared/extra-utils/requests/check-api-params'
  25. import { VideoBlacklistType, VideoDetails } from '../../../../shared/models/videos'
  26. import { expect } from 'chai'
  27. import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
  28. describe('Test video blacklist API validators', function () {
  29. let servers: ServerInfo[]
  30. let notBlacklistedVideoId: number
  31. let remoteVideoUUID: string
  32. let userAccessToken1 = ''
  33. let userAccessToken2 = ''
  34. // ---------------------------------------------------------------
  35. before(async function () {
  36. this.timeout(120000)
  37. servers = await flushAndRunMultipleServers(2)
  38. await setAccessTokensToServers(servers)
  39. await doubleFollow(servers[0], servers[1])
  40. {
  41. const username = 'user1'
  42. const password = 'my super password'
  43. await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: username, password: password })
  44. userAccessToken1 = await userLogin(servers[0], { username, password })
  45. }
  46. {
  47. const username = 'user2'
  48. const password = 'my super password'
  49. await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: username, password: password })
  50. userAccessToken2 = await userLogin(servers[0], { username, password })
  51. }
  52. {
  53. const res = await uploadVideo(servers[0].url, userAccessToken1, {})
  54. servers[0].video = res.body.video
  55. }
  56. {
  57. const res = await uploadVideo(servers[0].url, servers[0].accessToken, {})
  58. notBlacklistedVideoId = res.body.video.uuid
  59. }
  60. {
  61. const res = await uploadVideo(servers[1].url, servers[1].accessToken, {})
  62. remoteVideoUUID = res.body.video.uuid
  63. }
  64. await waitJobs(servers)
  65. })
  66. describe('When adding a video in blacklist', function () {
  67. const basePath = '/api/v1/videos/'
  68. it('Should fail with nothing', async function () {
  69. const path = basePath + servers[0].video + '/blacklist'
  70. const fields = {}
  71. await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
  72. })
  73. it('Should fail with a wrong video', async function () {
  74. const wrongPath = '/api/v1/videos/blabla/blacklist'
  75. const fields = {}
  76. await makePostBodyRequest({ url: servers[0].url, path: wrongPath, token: servers[0].accessToken, fields })
  77. })
  78. it('Should fail with a non authenticated user', async function () {
  79. const path = basePath + servers[0].video + '/blacklist'
  80. const fields = {}
  81. await makePostBodyRequest({ url: servers[0].url, path, token: 'hello', fields, statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
  82. })
  83. it('Should fail with a non admin user', async function () {
  84. const path = basePath + servers[0].video + '/blacklist'
  85. const fields = {}
  86. await makePostBodyRequest({
  87. url: servers[0].url,
  88. path,
  89. token: userAccessToken2,
  90. fields,
  91. statusCodeExpected: HttpStatusCode.FORBIDDEN_403
  92. })
  93. })
  94. it('Should fail with an invalid reason', async function () {
  95. const path = basePath + servers[0].video.uuid + '/blacklist'
  96. const fields = { reason: 'a'.repeat(305) }
  97. await makePostBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
  98. })
  99. it('Should fail to unfederate a remote video', async function () {
  100. const path = basePath + remoteVideoUUID + '/blacklist'
  101. const fields = { unfederate: true }
  102. await makePostBodyRequest({
  103. url: servers[0].url,
  104. path,
  105. token: servers[0].accessToken,
  106. fields,
  107. statusCodeExpected: HttpStatusCode.CONFLICT_409
  108. })
  109. })
  110. it('Should succeed with the correct params', async function () {
  111. const path = basePath + servers[0].video.uuid + '/blacklist'
  112. const fields = {}
  113. await makePostBodyRequest({
  114. url: servers[0].url,
  115. path,
  116. token: servers[0].accessToken,
  117. fields,
  118. statusCodeExpected: HttpStatusCode.NO_CONTENT_204
  119. })
  120. })
  121. })
  122. describe('When updating a video in blacklist', function () {
  123. const basePath = '/api/v1/videos/'
  124. it('Should fail with a wrong video', async function () {
  125. const wrongPath = '/api/v1/videos/blabla/blacklist'
  126. const fields = {}
  127. await makePutBodyRequest({ url: servers[0].url, path: wrongPath, token: servers[0].accessToken, fields })
  128. })
  129. it('Should fail with a video not blacklisted', async function () {
  130. const path = '/api/v1/videos/' + notBlacklistedVideoId + '/blacklist'
  131. const fields = {}
  132. await makePutBodyRequest({
  133. url: servers[0].url,
  134. path,
  135. token: servers[0].accessToken,
  136. fields,
  137. statusCodeExpected: HttpStatusCode.NOT_FOUND_404
  138. })
  139. })
  140. it('Should fail with a non authenticated user', async function () {
  141. const path = basePath + servers[0].video + '/blacklist'
  142. const fields = {}
  143. await makePutBodyRequest({ url: servers[0].url, path, token: 'hello', fields, statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401 })
  144. })
  145. it('Should fail with a non admin user', async function () {
  146. const path = basePath + servers[0].video + '/blacklist'
  147. const fields = {}
  148. await makePutBodyRequest({
  149. url: servers[0].url,
  150. path,
  151. token: userAccessToken2,
  152. fields,
  153. statusCodeExpected: HttpStatusCode.FORBIDDEN_403
  154. })
  155. })
  156. it('Should fail with an invalid reason', async function () {
  157. const path = basePath + servers[0].video.uuid + '/blacklist'
  158. const fields = { reason: 'a'.repeat(305) }
  159. await makePutBodyRequest({ url: servers[0].url, path, token: servers[0].accessToken, fields })
  160. })
  161. it('Should succeed with the correct params', async function () {
  162. const path = basePath + servers[0].video.uuid + '/blacklist'
  163. const fields = { reason: 'hello' }
  164. await makePutBodyRequest({
  165. url: servers[0].url,
  166. path,
  167. token: servers[0].accessToken,
  168. fields,
  169. statusCodeExpected: HttpStatusCode.NO_CONTENT_204
  170. })
  171. })
  172. })
  173. describe('When getting blacklisted video', function () {
  174. it('Should fail with a non authenticated user', async function () {
  175. await getVideo(servers[0].url, servers[0].video.uuid, HttpStatusCode.UNAUTHORIZED_401)
  176. })
  177. it('Should fail with another user', async function () {
  178. await getVideoWithToken(servers[0].url, userAccessToken2, servers[0].video.uuid, HttpStatusCode.FORBIDDEN_403)
  179. })
  180. it('Should succeed with the owner authenticated user', async function () {
  181. const res = await getVideoWithToken(servers[0].url, userAccessToken1, servers[0].video.uuid, HttpStatusCode.OK_200)
  182. const video: VideoDetails = res.body
  183. expect(video.blacklisted).to.be.true
  184. })
  185. it('Should succeed with an admin', async function () {
  186. const res = await getVideoWithToken(servers[0].url, servers[0].accessToken, servers[0].video.uuid, HttpStatusCode.OK_200)
  187. const video: VideoDetails = res.body
  188. expect(video.blacklisted).to.be.true
  189. })
  190. })
  191. describe('When removing a video in blacklist', function () {
  192. it('Should fail with a non authenticated user', async function () {
  193. await removeVideoFromBlacklist(servers[0].url, 'fake token', servers[0].video.uuid, HttpStatusCode.UNAUTHORIZED_401)
  194. })
  195. it('Should fail with a non admin user', async function () {
  196. await removeVideoFromBlacklist(servers[0].url, userAccessToken2, servers[0].video.uuid, HttpStatusCode.FORBIDDEN_403)
  197. })
  198. it('Should fail with an incorrect id', async function () {
  199. await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, 'hello', HttpStatusCode.BAD_REQUEST_400)
  200. })
  201. it('Should fail with a not blacklisted video', async function () {
  202. // The video was not added to the blacklist so it should fail
  203. await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, notBlacklistedVideoId, HttpStatusCode.NOT_FOUND_404)
  204. })
  205. it('Should succeed with the correct params', async function () {
  206. await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, servers[0].video.uuid, HttpStatusCode.NO_CONTENT_204)
  207. })
  208. })
  209. describe('When listing videos in blacklist', function () {
  210. const basePath = '/api/v1/videos/blacklist/'
  211. it('Should fail with a non authenticated user', async function () {
  212. await getBlacklistedVideosList({ url: servers[0].url, token: 'fake token', specialStatus: HttpStatusCode.UNAUTHORIZED_401 })
  213. })
  214. it('Should fail with a non admin user', async function () {
  215. await getBlacklistedVideosList({ url: servers[0].url, token: userAccessToken2, specialStatus: HttpStatusCode.FORBIDDEN_403 })
  216. })
  217. it('Should fail with a bad start pagination', async function () {
  218. await checkBadStartPagination(servers[0].url, basePath, servers[0].accessToken)
  219. })
  220. it('Should fail with a bad count pagination', async function () {
  221. await checkBadCountPagination(servers[0].url, basePath, servers[0].accessToken)
  222. })
  223. it('Should fail with an incorrect sort', async function () {
  224. await checkBadSortPagination(servers[0].url, basePath, servers[0].accessToken)
  225. })
  226. it('Should fail with an invalid type', async function () {
  227. await getBlacklistedVideosList({
  228. url: servers[0].url,
  229. token: servers[0].accessToken,
  230. type: 0,
  231. specialStatus: HttpStatusCode.BAD_REQUEST_400
  232. })
  233. })
  234. it('Should succeed with the correct parameters', async function () {
  235. await getBlacklistedVideosList({ url: servers[0].url, token: servers[0].accessToken, type: VideoBlacklistType.MANUAL })
  236. })
  237. })
  238. after(async function () {
  239. await cleanupTests(servers)
  240. })
  241. })