checks.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
  2. import { expect } from 'chai'
  3. import { pathExists, readFile } from 'fs-extra'
  4. import { join } from 'path'
  5. import { root } from '@shared/core-utils'
  6. import { HttpStatusCode } from '@shared/models'
  7. import { makeGetRequest, PeerTubeServer } from '@shared/server-commands'
  8. // Default interval -> 5 minutes
  9. function dateIsValid (dateString: string, interval = 300000) {
  10. const dateToCheck = new Date(dateString)
  11. const now = new Date()
  12. return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
  13. }
  14. function expectStartWith (str: string, start: string) {
  15. expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.true
  16. }
  17. function expectNotStartWith (str: string, start: string) {
  18. expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.false
  19. }
  20. function expectEndWith (str: string, end: string) {
  21. expect(str.endsWith(end), `${str} does not end with ${end}`).to.be.true
  22. }
  23. // ---------------------------------------------------------------------------
  24. async function expectLogDoesNotContain (server: PeerTubeServer, str: string) {
  25. const content = await server.servers.getLogContent()
  26. expect(content.toString()).to.not.contain(str)
  27. }
  28. async function expectLogContain (server: PeerTubeServer, str: string) {
  29. const content = await server.servers.getLogContent()
  30. expect(content.toString()).to.contain(str)
  31. }
  32. async function testImage (url: string, imageName: string, imageHTTPPath: string, extension = '.jpg') {
  33. const res = await makeGetRequest({
  34. url,
  35. path: imageHTTPPath,
  36. expectedStatus: HttpStatusCode.OK_200
  37. })
  38. const body = res.body
  39. const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
  40. const minLength = data.length - ((40 * data.length) / 100)
  41. const maxLength = data.length + ((40 * data.length) / 100)
  42. expect(body.length).to.be.above(minLength, 'the generated image is way smaller than the recorded fixture')
  43. expect(body.length).to.be.below(maxLength, 'the generated image is way larger than the recorded fixture')
  44. }
  45. async function testFileExistsOrNot (server: PeerTubeServer, directory: string, filePath: string, exist: boolean) {
  46. const base = server.servers.buildDirectory(directory)
  47. expect(await pathExists(join(base, filePath))).to.equal(exist)
  48. }
  49. function checkBadStartPagination (url: string, path: string, token?: string, query = {}) {
  50. return makeGetRequest({
  51. url,
  52. path,
  53. token,
  54. query: { ...query, start: 'hello' },
  55. expectedStatus: HttpStatusCode.BAD_REQUEST_400
  56. })
  57. }
  58. async function checkBadCountPagination (url: string, path: string, token?: string, query = {}) {
  59. await makeGetRequest({
  60. url,
  61. path,
  62. token,
  63. query: { ...query, count: 'hello' },
  64. expectedStatus: HttpStatusCode.BAD_REQUEST_400
  65. })
  66. await makeGetRequest({
  67. url,
  68. path,
  69. token,
  70. query: { ...query, count: 2000 },
  71. expectedStatus: HttpStatusCode.BAD_REQUEST_400
  72. })
  73. }
  74. function checkBadSortPagination (url: string, path: string, token?: string, query = {}) {
  75. return makeGetRequest({
  76. url,
  77. path,
  78. token,
  79. query: { ...query, sort: 'hello' },
  80. expectedStatus: HttpStatusCode.BAD_REQUEST_400
  81. })
  82. }
  83. export {
  84. dateIsValid,
  85. testImage,
  86. expectLogDoesNotContain,
  87. testFileExistsOrNot,
  88. expectStartWith,
  89. expectNotStartWith,
  90. expectEndWith,
  91. checkBadStartPagination,
  92. checkBadCountPagination,
  93. checkBadSortPagination,
  94. expectLogContain
  95. }