MockDocUpdaterServer.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. return res.json(data)
  42. }
  43. )
  44. },
  45. deleteProjectRequest(req, res, next) {
  46. const { project_id } = req.params
  47. return MockDocUpdaterServer.deleteProject(project_id, (error) => {
  48. if (error != null) {
  49. return next(error)
  50. }
  51. return res.sendStatus(204)
  52. })
  53. },
  54. running: false,
  55. run(callback) {
  56. if (callback == null) {
  57. callback = function (error) {}
  58. }
  59. if (MockDocUpdaterServer.running) {
  60. return callback()
  61. }
  62. const app = express()
  63. app.get(
  64. '/project/:project_id/doc/:doc_id',
  65. MockDocUpdaterServer.getDocumentRequest
  66. )
  67. app.delete(
  68. '/project/:project_id',
  69. MockDocUpdaterServer.deleteProjectRequest
  70. )
  71. return app
  72. .listen(3003, (error) => {
  73. MockDocUpdaterServer.running = true
  74. return callback(error)
  75. })
  76. .on('error', (error) => {
  77. console.error('error starting MockDocUpdaterServer:', error.message)
  78. return process.exit(1)
  79. })
  80. }
  81. }
  82. sinon.spy(MockDocUpdaterServer, 'getDocument')