HealthCheckControllerTests.mjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { expect } from 'chai'
  2. import Settings from '@overleaf/settings'
  3. import UserHelper from './helpers/User.mjs'
  4. const User = UserHelper.promises
  5. describe('HealthCheckController', function () {
  6. describe('SmokeTests', function () {
  7. let user, projectId
  8. const captchaDisabledBefore = Settings.recaptcha.disabled.login
  9. beforeEach(async function () {
  10. user = new User()
  11. await user.login()
  12. projectId = await user.createProject('SmokeTest')
  13. // HACK: Inject the details into the app
  14. Settings.smokeTest.userId = user.id
  15. Settings.smokeTest.user = user.email
  16. Settings.smokeTest.password = user.password
  17. Settings.smokeTest.projectId = projectId
  18. Settings.recaptcha.disabled.login = true
  19. })
  20. afterEach(function () {
  21. Settings.recaptcha.disabled.login = captchaDisabledBefore
  22. })
  23. async function performSmokeTestRequest() {
  24. const start = Date.now()
  25. const { response, body } = await user.doRequest('GET', {
  26. url: '/health_check/full',
  27. json: true,
  28. })
  29. const end = Date.now()
  30. expect(body).to.exist
  31. expect(body.stats).to.exist
  32. expect(Date.parse(body.stats.start)).to.be.within(start, start + 1000)
  33. expect(Date.parse(body.stats.end)).to.be.within(end - 1000, end)
  34. expect(body.stats.duration).to.be.within(0, 10000)
  35. expect(body.stats.steps).to.be.instanceof(Array)
  36. return { response, body }
  37. }
  38. describe('happy path', function () {
  39. it('should respond with a 200 and stats', async function () {
  40. const { response, body } = await performSmokeTestRequest()
  41. expect(body.error).to.not.exist
  42. expect(response.statusCode).to.equal(200)
  43. })
  44. })
  45. describe('when the request is aborted', function () {
  46. it('should not crash', async function () {
  47. try {
  48. await user.doRequest('GET', {
  49. timeout: 1,
  50. url: '/health_check/full',
  51. json: true,
  52. })
  53. } catch (err) {
  54. expect(err.code).to.be.oneOf(['ETIMEDOUT', 'ESOCKETTIMEDOUT'])
  55. return
  56. }
  57. expect.fail('expected request to fail with timeout error')
  58. })
  59. })
  60. describe('when the project does not exist', function () {
  61. beforeEach(function () {
  62. Settings.smokeTest.projectId = '404'
  63. })
  64. it('should respond with a 500 ', async function () {
  65. const { response, body } = await performSmokeTestRequest()
  66. expect(body.error).to.equal('run.101_loadEditor failed')
  67. expect(response.statusCode).to.equal(500)
  68. })
  69. })
  70. describe('when the password mismatches', function () {
  71. beforeEach(function () {
  72. Settings.smokeTest.password = 'foo-bar'
  73. })
  74. it('should respond with a 500 with mismatching password', async function () {
  75. const { response, body } = await performSmokeTestRequest()
  76. expect(body.error).to.equal('run.002_login failed')
  77. expect(response.statusCode).to.equal(500)
  78. })
  79. })
  80. })
  81. })