2
1

services.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import 'mocha'
  3. import {
  4. cleanupTests,
  5. flushAndRunServer,
  6. makeGetRequest,
  7. ServerInfo,
  8. setAccessTokensToServers,
  9. uploadVideo,
  10. createVideoPlaylist,
  11. setDefaultVideoChannel
  12. } from '../../../../shared/extra-utils'
  13. import { VideoPlaylistPrivacy } from '@shared/models'
  14. import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
  15. describe('Test services API validators', function () {
  16. let server: ServerInfo
  17. let playlistUUID: string
  18. // ---------------------------------------------------------------
  19. before(async function () {
  20. this.timeout(60000)
  21. server = await flushAndRunServer(1)
  22. await setAccessTokensToServers([ server ])
  23. await setDefaultVideoChannel([ server ])
  24. {
  25. const res = await uploadVideo(server.url, server.accessToken, { name: 'my super name' })
  26. server.video = res.body.video
  27. }
  28. {
  29. const res = await createVideoPlaylist({
  30. url: server.url,
  31. token: server.accessToken,
  32. playlistAttrs: {
  33. displayName: 'super playlist',
  34. privacy: VideoPlaylistPrivacy.PUBLIC,
  35. videoChannelId: server.videoChannel.id
  36. }
  37. })
  38. playlistUUID = res.body.videoPlaylist.uuid
  39. }
  40. })
  41. describe('Test oEmbed API validators', function () {
  42. it('Should fail with an invalid url', async function () {
  43. const embedUrl = 'hello.com'
  44. await checkParamEmbed(server, embedUrl)
  45. })
  46. it('Should fail with an invalid host', async function () {
  47. const embedUrl = 'http://hello.com/videos/watch/' + server.video.uuid
  48. await checkParamEmbed(server, embedUrl)
  49. })
  50. it('Should fail with an invalid element id', async function () {
  51. const embedUrl = `http://localhost:${server.port}/videos/watch/blabla`
  52. await checkParamEmbed(server, embedUrl)
  53. })
  54. it('Should fail with an unknown element', async function () {
  55. const embedUrl = `http://localhost:${server.port}/videos/watch/88fc0165-d1f0-4a35-a51a-3b47f668689c`
  56. await checkParamEmbed(server, embedUrl, HttpStatusCode.NOT_FOUND_404)
  57. })
  58. it('Should fail with an invalid path', async function () {
  59. const embedUrl = `http://localhost:${server.port}/videos/watchs/${server.video.uuid}`
  60. await checkParamEmbed(server, embedUrl)
  61. })
  62. it('Should fail with an invalid max height', async function () {
  63. const embedUrl = `http://localhost:${server.port}/videos/watch/${server.video.uuid}`
  64. await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { maxheight: 'hello' })
  65. })
  66. it('Should fail with an invalid max width', async function () {
  67. const embedUrl = `http://localhost:${server.port}/videos/watch/${server.video.uuid}`
  68. await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { maxwidth: 'hello' })
  69. })
  70. it('Should fail with an invalid format', async function () {
  71. const embedUrl = `http://localhost:${server.port}/videos/watch/${server.video.uuid}`
  72. await checkParamEmbed(server, embedUrl, HttpStatusCode.BAD_REQUEST_400, { format: 'blabla' })
  73. })
  74. it('Should fail with a non supported format', async function () {
  75. const embedUrl = `http://localhost:${server.port}/videos/watch/${server.video.uuid}`
  76. await checkParamEmbed(server, embedUrl, HttpStatusCode.NOT_IMPLEMENTED_501, { format: 'xml' })
  77. })
  78. it('Should succeed with the correct params with a video', async function () {
  79. const embedUrl = `http://localhost:${server.port}/videos/watch/${server.video.uuid}`
  80. const query = {
  81. format: 'json',
  82. maxheight: 400,
  83. maxwidth: 400
  84. }
  85. await checkParamEmbed(server, embedUrl, HttpStatusCode.OK_200, query)
  86. })
  87. it('Should succeed with the correct params with a playlist', async function () {
  88. const embedUrl = `http://localhost:${server.port}/videos/watch/playlist/${playlistUUID}`
  89. const query = {
  90. format: 'json',
  91. maxheight: 400,
  92. maxwidth: 400
  93. }
  94. await checkParamEmbed(server, embedUrl, HttpStatusCode.OK_200, query)
  95. })
  96. })
  97. after(async function () {
  98. await cleanupTests([ server ])
  99. })
  100. })
  101. function checkParamEmbed (server: ServerInfo, embedUrl: string, statusCodeExpected = HttpStatusCode.BAD_REQUEST_400, query = {}) {
  102. const path = '/services/oembed'
  103. return makeGetRequest({
  104. url: server.url,
  105. path,
  106. query: Object.assign(query, { url: embedUrl }),
  107. statusCodeExpected
  108. })
  109. }