MockDocUpdaterServer.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-return-assign,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. let MockDocUpdaterServer
  15. const sinon = require('sinon')
  16. const express = require('express')
  17. module.exports = MockDocUpdaterServer = {
  18. docs: {},
  19. createMockDoc(project_id, doc_id, data) {
  20. return (MockDocUpdaterServer.docs[`${project_id}:${doc_id}`] = data)
  21. },
  22. getDocument(project_id, doc_id, fromVersion, callback) {
  23. if (callback == null) {
  24. callback = function (error, data) {}
  25. }
  26. return callback(null, MockDocUpdaterServer.docs[`${project_id}:${doc_id}`])
  27. },
  28. deleteProject: sinon.stub().callsArg(1),
  29. getDocumentRequest(req, res, next) {
  30. const { project_id, doc_id } = req.params
  31. let { fromVersion } = req.query
  32. fromVersion = parseInt(fromVersion, 10)
  33. return MockDocUpdaterServer.getDocument(
  34. project_id,
  35. doc_id,
  36. fromVersion,
  37. (error, data) => {
  38. if (error != null) {
  39. return next(error)
  40. }
  41. if (!data) {
  42. return res.sendStatus(404)
  43. }
  44. return res.json(data)
  45. }
  46. )
  47. },
  48. deleteProjectRequest(req, res, next) {
  49. const { project_id } = req.params
  50. return MockDocUpdaterServer.deleteProject(project_id, (error) => {
  51. if (error != null) {
  52. return next(error)
  53. }
  54. return res.sendStatus(204)
  55. })
  56. },
  57. running: false,
  58. run(callback) {
  59. if (callback == null) {
  60. callback = function (error) {}
  61. }
  62. if (MockDocUpdaterServer.running) {
  63. return callback()
  64. }
  65. const app = express()
  66. app.get(
  67. '/project/:project_id/doc/:doc_id',
  68. MockDocUpdaterServer.getDocumentRequest
  69. )
  70. app.delete(
  71. '/project/:project_id',
  72. MockDocUpdaterServer.deleteProjectRequest
  73. )
  74. return app
  75. .listen(3003, (error) => {
  76. MockDocUpdaterServer.running = true
  77. return callback(error)
  78. })
  79. .on('error', (error) => {
  80. console.error('error starting MockDocUpdaterServer:', error.message)
  81. return process.exit(1)
  82. })
  83. }
  84. }
  85. sinon.spy(MockDocUpdaterServer, 'getDocument')