live.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import 'mocha'
  3. import { omit } from 'lodash'
  4. import {
  5. buildAbsoluteFixturePath,
  6. cleanupTests,
  7. createSingleServer,
  8. LiveCommand,
  9. makePostBodyRequest,
  10. makeUploadRequest,
  11. PeerTubeServer,
  12. sendRTMPStream,
  13. setAccessTokensToServers,
  14. stopFfmpeg
  15. } from '@shared/extra-utils'
  16. import { HttpStatusCode, VideoCreateResult, VideoPrivacy } from '@shared/models'
  17. describe('Test video lives API validator', function () {
  18. const path = '/api/v1/videos/live'
  19. let server: PeerTubeServer
  20. let userAccessToken = ''
  21. let channelId: number
  22. let video: VideoCreateResult
  23. let videoIdNotLive: number
  24. let command: LiveCommand
  25. // ---------------------------------------------------------------
  26. before(async function () {
  27. this.timeout(30000)
  28. server = await createSingleServer(1)
  29. await setAccessTokensToServers([ server ])
  30. await server.config.updateCustomSubConfig({
  31. newConfig: {
  32. live: {
  33. enabled: true,
  34. maxInstanceLives: 20,
  35. maxUserLives: 20,
  36. allowReplay: true
  37. }
  38. }
  39. })
  40. const username = 'user1'
  41. const password = 'my super password'
  42. await server.users.create({ username: username, password: password })
  43. userAccessToken = await server.login.getAccessToken({ username, password })
  44. {
  45. const { videoChannels } = await server.users.getMyInfo()
  46. channelId = videoChannels[0].id
  47. }
  48. {
  49. videoIdNotLive = (await server.videos.quickUpload({ name: 'not live' })).id
  50. }
  51. command = server.live
  52. })
  53. describe('When creating a live', function () {
  54. let baseCorrectParams
  55. before(function () {
  56. baseCorrectParams = {
  57. name: 'my super name',
  58. category: 5,
  59. licence: 1,
  60. language: 'pt',
  61. nsfw: false,
  62. commentsEnabled: true,
  63. downloadEnabled: true,
  64. waitTranscoding: true,
  65. description: 'my super description',
  66. support: 'my super support text',
  67. tags: [ 'tag1', 'tag2' ],
  68. privacy: VideoPrivacy.PUBLIC,
  69. channelId,
  70. saveReplay: false,
  71. permanentLive: false
  72. }
  73. })
  74. it('Should fail with nothing', async function () {
  75. const fields = {}
  76. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  77. })
  78. it('Should fail with a long name', async function () {
  79. const fields = { ...baseCorrectParams, name: 'super'.repeat(65) }
  80. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  81. })
  82. it('Should fail with a bad category', async function () {
  83. const fields = { ...baseCorrectParams, category: 125 }
  84. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  85. })
  86. it('Should fail with a bad licence', async function () {
  87. const fields = { ...baseCorrectParams, licence: 125 }
  88. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  89. })
  90. it('Should fail with a bad language', async function () {
  91. const fields = { ...baseCorrectParams, language: 'a'.repeat(15) }
  92. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  93. })
  94. it('Should fail with a long description', async function () {
  95. const fields = { ...baseCorrectParams, description: 'super'.repeat(2500) }
  96. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  97. })
  98. it('Should fail with a long support text', async function () {
  99. const fields = { ...baseCorrectParams, support: 'super'.repeat(201) }
  100. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  101. })
  102. it('Should fail without a channel', async function () {
  103. const fields = omit(baseCorrectParams, 'channelId')
  104. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  105. })
  106. it('Should fail with a bad channel', async function () {
  107. const fields = { ...baseCorrectParams, channelId: 545454 }
  108. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  109. })
  110. it('Should fail with another user channel', async function () {
  111. const user = {
  112. username: 'fake',
  113. password: 'fake_password'
  114. }
  115. await server.users.create({ username: user.username, password: user.password })
  116. const accessTokenUser = await server.login.getAccessToken(user)
  117. const { videoChannels } = await server.users.getMyInfo({ token: accessTokenUser })
  118. const customChannelId = videoChannels[0].id
  119. const fields = { ...baseCorrectParams, channelId: customChannelId }
  120. await makePostBodyRequest({ url: server.url, path, token: userAccessToken, fields })
  121. })
  122. it('Should fail with too many tags', async function () {
  123. const fields = { ...baseCorrectParams, tags: [ 'tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6' ] }
  124. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  125. })
  126. it('Should fail with a tag length too low', async function () {
  127. const fields = { ...baseCorrectParams, tags: [ 'tag1', 't' ] }
  128. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  129. })
  130. it('Should fail with a tag length too big', async function () {
  131. const fields = { ...baseCorrectParams, tags: [ 'tag1', 'my_super_tag_too_long_long_long_long_long_long' ] }
  132. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  133. })
  134. it('Should fail with an incorrect thumbnail file', async function () {
  135. const fields = baseCorrectParams
  136. const attaches = {
  137. thumbnailfile: buildAbsoluteFixturePath('video_short.mp4')
  138. }
  139. await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
  140. })
  141. it('Should fail with a big thumbnail file', async function () {
  142. const fields = baseCorrectParams
  143. const attaches = {
  144. thumbnailfile: buildAbsoluteFixturePath('preview-big.png')
  145. }
  146. await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
  147. })
  148. it('Should fail with an incorrect preview file', async function () {
  149. const fields = baseCorrectParams
  150. const attaches = {
  151. previewfile: buildAbsoluteFixturePath('video_short.mp4')
  152. }
  153. await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
  154. })
  155. it('Should fail with a big preview file', async function () {
  156. const fields = baseCorrectParams
  157. const attaches = {
  158. previewfile: buildAbsoluteFixturePath('preview-big.png')
  159. }
  160. await makeUploadRequest({ url: server.url, path, token: server.accessToken, fields, attaches })
  161. })
  162. it('Should fail with save replay and permanent live set to true', async function () {
  163. const fields = { ...baseCorrectParams, saveReplay: true, permanentLive: true }
  164. await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields })
  165. })
  166. it('Should succeed with the correct parameters', async function () {
  167. this.timeout(30000)
  168. const res = await makePostBodyRequest({
  169. url: server.url,
  170. path,
  171. token: server.accessToken,
  172. fields: baseCorrectParams,
  173. expectedStatus: HttpStatusCode.OK_200
  174. })
  175. video = res.body.video
  176. })
  177. it('Should forbid if live is disabled', async function () {
  178. await server.config.updateCustomSubConfig({
  179. newConfig: {
  180. live: {
  181. enabled: false
  182. }
  183. }
  184. })
  185. await makePostBodyRequest({
  186. url: server.url,
  187. path,
  188. token: server.accessToken,
  189. fields: baseCorrectParams,
  190. expectedStatus: HttpStatusCode.FORBIDDEN_403
  191. })
  192. })
  193. it('Should forbid to save replay if not enabled by the admin', async function () {
  194. const fields = { ...baseCorrectParams, saveReplay: true }
  195. await server.config.updateCustomSubConfig({
  196. newConfig: {
  197. live: {
  198. enabled: true,
  199. allowReplay: false
  200. }
  201. }
  202. })
  203. await makePostBodyRequest({
  204. url: server.url,
  205. path,
  206. token: server.accessToken,
  207. fields,
  208. expectedStatus: HttpStatusCode.FORBIDDEN_403
  209. })
  210. })
  211. it('Should allow to save replay if enabled by the admin', async function () {
  212. const fields = { ...baseCorrectParams, saveReplay: true }
  213. await server.config.updateCustomSubConfig({
  214. newConfig: {
  215. live: {
  216. enabled: true,
  217. allowReplay: true
  218. }
  219. }
  220. })
  221. await makePostBodyRequest({
  222. url: server.url,
  223. path,
  224. token: server.accessToken,
  225. fields,
  226. expectedStatus: HttpStatusCode.OK_200
  227. })
  228. })
  229. it('Should not allow live if max instance lives is reached', async function () {
  230. await server.config.updateCustomSubConfig({
  231. newConfig: {
  232. live: {
  233. enabled: true,
  234. maxInstanceLives: 1
  235. }
  236. }
  237. })
  238. await makePostBodyRequest({
  239. url: server.url,
  240. path,
  241. token: server.accessToken,
  242. fields: baseCorrectParams,
  243. expectedStatus: HttpStatusCode.FORBIDDEN_403
  244. })
  245. })
  246. it('Should not allow live if max user lives is reached', async function () {
  247. await server.config.updateCustomSubConfig({
  248. newConfig: {
  249. live: {
  250. enabled: true,
  251. maxInstanceLives: 20,
  252. maxUserLives: 1
  253. }
  254. }
  255. })
  256. await makePostBodyRequest({
  257. url: server.url,
  258. path,
  259. token: server.accessToken,
  260. fields: baseCorrectParams,
  261. expectedStatus: HttpStatusCode.FORBIDDEN_403
  262. })
  263. })
  264. })
  265. describe('When getting live information', function () {
  266. it('Should fail without access token', async function () {
  267. await command.get({ token: '', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
  268. })
  269. it('Should fail with a bad access token', async function () {
  270. await command.get({ token: 'toto', videoId: video.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
  271. })
  272. it('Should fail with access token of another user', async function () {
  273. await command.get({ token: userAccessToken, videoId: video.id, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
  274. })
  275. it('Should fail with a bad video id', async function () {
  276. await command.get({ videoId: 'toto', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  277. })
  278. it('Should fail with an unknown video id', async function () {
  279. await command.get({ videoId: 454555, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
  280. })
  281. it('Should fail with a non live video', async function () {
  282. await command.get({ videoId: videoIdNotLive, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
  283. })
  284. it('Should succeed with the correct params', async function () {
  285. await command.get({ videoId: video.id })
  286. await command.get({ videoId: video.uuid })
  287. await command.get({ videoId: video.shortUUID })
  288. })
  289. })
  290. describe('When updating live information', async function () {
  291. it('Should fail without access token', async function () {
  292. await command.update({ token: '', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
  293. })
  294. it('Should fail with a bad access token', async function () {
  295. await command.update({ token: 'toto', videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
  296. })
  297. it('Should fail with access token of another user', async function () {
  298. await command.update({ token: userAccessToken, videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
  299. })
  300. it('Should fail with a bad video id', async function () {
  301. await command.update({ videoId: 'toto', fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  302. })
  303. it('Should fail with an unknown video id', async function () {
  304. await command.update({ videoId: 454555, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
  305. })
  306. it('Should fail with a non live video', async function () {
  307. await command.update({ videoId: videoIdNotLive, fields: {}, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
  308. })
  309. it('Should fail with save replay and permanent live set to true', async function () {
  310. const fields = { saveReplay: true, permanentLive: true }
  311. await command.update({ videoId: video.id, fields, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  312. })
  313. it('Should succeed with the correct params', async function () {
  314. await command.update({ videoId: video.id, fields: { saveReplay: false } })
  315. await command.update({ videoId: video.uuid, fields: { saveReplay: false } })
  316. await command.update({ videoId: video.shortUUID, fields: { saveReplay: false } })
  317. })
  318. it('Should fail to update replay status if replay is not allowed on the instance', async function () {
  319. await server.config.updateCustomSubConfig({
  320. newConfig: {
  321. live: {
  322. enabled: true,
  323. allowReplay: false
  324. }
  325. }
  326. })
  327. await command.update({ videoId: video.id, fields: { saveReplay: true }, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
  328. })
  329. it('Should fail to update a live if it has already started', async function () {
  330. this.timeout(40000)
  331. const live = await command.get({ videoId: video.id })
  332. const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
  333. await command.waitUntilPublished({ videoId: video.id })
  334. await command.update({ videoId: video.id, fields: {}, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
  335. await stopFfmpeg(ffmpegCommand)
  336. })
  337. it('Should fail to stream twice in the save live', async function () {
  338. this.timeout(40000)
  339. const live = await command.get({ videoId: video.id })
  340. const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: live.rtmpUrl, streamKey: live.streamKey })
  341. await command.waitUntilPublished({ videoId: video.id })
  342. await command.runAndTestStreamError({ videoId: video.id, shouldHaveError: true })
  343. await stopFfmpeg(ffmpegCommand)
  344. })
  345. })
  346. after(async function () {
  347. await cleanupTests([ server ])
  348. })
  349. })