MockDocUpdaterServer.js 2.3 KB

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