2
1

video-imports.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import 'mocha'
  3. import { omit } from 'lodash'
  4. import { join } from 'path'
  5. import {
  6. cleanupTests,
  7. createUser,
  8. flushAndRunServer,
  9. getMyUserInformation,
  10. immutableAssign,
  11. makeGetRequest,
  12. makePostBodyRequest,
  13. makeUploadRequest,
  14. ServerInfo,
  15. setAccessTokensToServers,
  16. updateCustomSubConfig,
  17. userLogin
  18. } from '../../../../shared/extra-utils'
  19. import {
  20. checkBadCountPagination,
  21. checkBadSortPagination,
  22. checkBadStartPagination
  23. } from '../../../../shared/extra-utils/requests/check-api-params'
  24. import { getMagnetURI, getGoodVideoUrl } from '../../../../shared/extra-utils/videos/video-imports'
  25. import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum'
  26. import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
  27. describe('Test video imports API validator', function () {
  28. const path = '/api/v1/videos/imports'
  29. let server: ServerInfo
  30. let userAccessToken = ''
  31. let channelId: number
  32. // ---------------------------------------------------------------
  33. before(async function () {
  34. this.timeout(30000)
  35. server = await flushAndRunServer(1)
  36. await setAccessTokensToServers([ server ])
  37. const username = 'user1'
  38. const password = 'my super password'
  39. await createUser({ url: server.url, accessToken: server.accessToken, username: username, password: password })
  40. userAccessToken = await userLogin(server, { username, password })
  41. {
  42. const res = await getMyUserInformation(server.url, server.accessToken)
  43. channelId = res.body.videoChannels[0].id
  44. }
  45. })
  46. describe('When listing my video imports', function () {
  47. const myPath = '/api/v1/users/me/videos/imports'
  48. it('Should fail with a bad start pagination', async function () {
  49. await checkBadStartPagination(server.url, myPath, server.accessToken)
  50. })
  51. it('Should fail with a bad count pagination', async function () {
  52. await checkBadCountPagination(server.url, myPath, server.accessToken)
  53. })
  54. it('Should fail with an incorrect sort', async function () {
  55. await checkBadSortPagination(server.url, myPath, server.accessToken)
  56. })
  57. it('Should success with the correct parameters', async function () {
  58. await makeGetRequest({ url: server.url, path: myPath, statusCodeExpected: HttpStatusCode.OK_200, token: server.accessToken })
  59. })
  60. })
  61. describe('When adding a video import', function () {
  62. let baseCorrectParams
  63. before(function () {
  64. baseCorrectParams = {
  65. targetUrl: getGoodVideoUrl(),
  66. name: 'my super name',
  67. category: 5,
  68. licence: 1,
  69. language: 'pt',
  70. nsfw: false,
  71. commentsEnabled: true,
  72. downloadEnabled: true,
  73. waitTranscoding: true,
  74. description: 'my super description',
  75. support: 'my super support text',
  76. tags: [ 'tag1', 'tag2' ],
  77. privacy: VideoPrivacy.PUBLIC,
  78. channelId
  79. }
  80. })
  81. it('Should fail with nothing', async function () {
  82. const fields = {}
  83. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  84. })
  85. it('Should fail without a target url', async function () {
  86. const fields = omit(baseCorrectParams, 'targetUrl')
  87. await makePostBodyRequest({
  88. url: server.url,
  89. path,
  90. token: server.accessToken,
  91. fields,
  92. statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
  93. })
  94. })
  95. it('Should fail with a bad target url', async function () {
  96. const fields = immutableAssign(baseCorrectParams, { targetUrl: 'htt://hello' })
  97. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  98. })
  99. it('Should fail with a long name', async function () {
  100. const fields = immutableAssign(baseCorrectParams, { name: 'super'.repeat(65) })
  101. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  102. })
  103. it('Should fail with a bad category', async function () {
  104. const fields = immutableAssign(baseCorrectParams, { category: 125 })
  105. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  106. })
  107. it('Should fail with a bad licence', async function () {
  108. const fields = immutableAssign(baseCorrectParams, { licence: 125 })
  109. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  110. })
  111. it('Should fail with a bad language', async function () {
  112. const fields = immutableAssign(baseCorrectParams, { language: 'a'.repeat(15) })
  113. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  114. })
  115. it('Should fail with a long description', async function () {
  116. const fields = immutableAssign(baseCorrectParams, { description: 'super'.repeat(2500) })
  117. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  118. })
  119. it('Should fail with a long support text', async function () {
  120. const fields = immutableAssign(baseCorrectParams, { support: 'super'.repeat(201) })
  121. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  122. })
  123. it('Should fail without a channel', async function () {
  124. const fields = omit(baseCorrectParams, 'channelId')
  125. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  126. })
  127. it('Should fail with a bad channel', async function () {
  128. const fields = immutableAssign(baseCorrectParams, { channelId: 545454 })
  129. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  130. })
  131. it('Should fail with another user channel', async function () {
  132. const user = {
  133. username: 'fake',
  134. password: 'fake_password'
  135. }
  136. await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
  137. const accessTokenUser = await userLogin(server, user)
  138. const res = await getMyUserInformation(server.url, accessTokenUser)
  139. const customChannelId = res.body.videoChannels[0].id
  140. const fields = immutableAssign(baseCorrectParams, { channelId: customChannelId })
  141. await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
  142. })
  143. it('Should fail with too many tags', async function () {
  144. const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] })
  145. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  146. })
  147. it('Should fail with a tag length too low', async function () {
  148. const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 't' ] })
  149. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  150. })
  151. it('Should fail with a tag length too big', async function () {
  152. const fields = immutableAssign(baseCorrectParams, { tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] })
  153. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  154. })
  155. it('Should fail with an incorrect thumbnail file', async function () {
  156. const fields = baseCorrectParams
  157. const attaches = {
  158. thumbnailfile: join(__dirname, '..', '..', 'fixtures', 'avatar.png')
  159. }
  160. await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
  161. })
  162. it('Should fail with a big thumbnail file', async function () {
  163. const fields = baseCorrectParams
  164. const attaches = {
  165. thumbnailfile: join(__dirname, '..', '..', 'fixtures', 'avatar-big.png')
  166. }
  167. await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
  168. })
  169. it('Should fail with an incorrect preview file', async function () {
  170. const fields = baseCorrectParams
  171. const attaches = {
  172. previewfile: join(__dirname, '..', '..', 'fixtures', 'avatar.png')
  173. }
  174. await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
  175. })
  176. it('Should fail with a big preview file', async function () {
  177. const fields = baseCorrectParams
  178. const attaches = {
  179. previewfile: join(__dirname, '..', '..', 'fixtures', 'avatar-big.png')
  180. }
  181. await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
  182. })
  183. it('Should fail with an invalid torrent file', async function () {
  184. const fields = omit(baseCorrectParams, 'targetUrl')
  185. const attaches = {
  186. torrentfile: join(__dirname, '..', '..', 'fixtures', 'avatar-big.png')
  187. }
  188. await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
  189. })
  190. it('Should fail with an invalid magnet URI', async function () {
  191. let fields = omit(baseCorrectParams, 'targetUrl')
  192. fields = immutableAssign(fields, { magnetUri: 'blabla' })
  193. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  194. })
  195. it('Should succeed with the correct parameters', async function () {
  196. this.timeout(30000)
  197. await makePostBodyRequest({
  198. url: server.url,
  199. path,
  200. token: server.accessToken,
  201. fields: baseCorrectParams,
  202. statusCodeExpected: HttpStatusCode.OK_200
  203. })
  204. })
  205. it('Should forbid to import http videos', async function () {
  206. await updateCustomSubConfig(server.url, server.accessToken, {
  207. import: {
  208. videos: {
  209. http: {
  210. enabled: false
  211. },
  212. torrent: {
  213. enabled: true
  214. }
  215. }
  216. }
  217. })
  218. await makePostBodyRequest({
  219. url: server.url,
  220. path,
  221. token: server.accessToken,
  222. fields: baseCorrectParams,
  223. statusCodeExpected: HttpStatusCode.CONFLICT_409
  224. })
  225. })
  226. it('Should forbid to import torrent videos', async function () {
  227. await updateCustomSubConfig(server.url, server.accessToken, {
  228. import: {
  229. videos: {
  230. http: {
  231. enabled: true
  232. },
  233. torrent: {
  234. enabled: false
  235. }
  236. }
  237. }
  238. })
  239. let fields = omit(baseCorrectParams, 'targetUrl')
  240. fields = immutableAssign(fields, { magnetUri: getMagnetURI() })
  241. await makePostBodyRequest({
  242. url: server.url,
  243. path,
  244. token: server.accessToken,
  245. fields,
  246. statusCodeExpected: HttpStatusCode.CONFLICT_409
  247. })
  248. fields = omit(fields, 'magnetUri')
  249. const attaches = {
  250. torrentfile: join(__dirname, '..', '..', 'fixtures', 'video-720p.torrent')
  251. }
  252. await makeUploadRequest({
  253. url: server.url,
  254. path,
  255. token: server.accessToken,
  256. fields,
  257. attaches,
  258. statusCodeExpected: HttpStatusCode.CONFLICT_409
  259. })
  260. })
  261. })
  262. after(async function () {
  263. await cleanupTests([ server ])
  264. })
  265. })