HealthCheckControllerTests.js 3.0 KB

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