FixturesManager.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  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 FixturesManager
  14. const RealTimeClient = require('./RealTimeClient')
  15. const MockWebServer = require('./MockWebServer')
  16. const MockDocUpdaterServer = require('./MockDocUpdaterServer')
  17. module.exports = FixturesManager = {
  18. setUpProject(options, callback) {
  19. if (options == null) {
  20. options = {}
  21. }
  22. if (callback == null) {
  23. callback = function (error, data) {}
  24. }
  25. if (!options.user_id) {
  26. options.user_id = FixturesManager.getRandomId()
  27. }
  28. if (!options.project_id) {
  29. options.project_id = FixturesManager.getRandomId()
  30. }
  31. if (!options.project) {
  32. options.project = { name: 'Test Project' }
  33. }
  34. const {
  35. project_id,
  36. user_id,
  37. privilegeLevel,
  38. project,
  39. publicAccess
  40. } = options
  41. const privileges = {}
  42. privileges[user_id] = privilegeLevel
  43. if (publicAccess) {
  44. privileges['anonymous-user'] = publicAccess
  45. }
  46. MockWebServer.createMockProject(project_id, privileges, project)
  47. return MockWebServer.run((error) => {
  48. if (error != null) {
  49. throw error
  50. }
  51. return RealTimeClient.setSession(
  52. {
  53. user: {
  54. _id: user_id,
  55. first_name: 'Joe',
  56. last_name: 'Bloggs'
  57. }
  58. },
  59. (error) => {
  60. if (error != null) {
  61. throw error
  62. }
  63. return callback(null, {
  64. project_id,
  65. user_id,
  66. privilegeLevel,
  67. project
  68. })
  69. }
  70. )
  71. })
  72. },
  73. setUpDoc(project_id, options, callback) {
  74. if (options == null) {
  75. options = {}
  76. }
  77. if (callback == null) {
  78. callback = function (error, data) {}
  79. }
  80. if (!options.doc_id) {
  81. options.doc_id = FixturesManager.getRandomId()
  82. }
  83. if (!options.lines) {
  84. options.lines = ['doc', 'lines']
  85. }
  86. if (!options.version) {
  87. options.version = 42
  88. }
  89. if (!options.ops) {
  90. options.ops = ['mock', 'ops']
  91. }
  92. const { doc_id, lines, version, ops, ranges } = options
  93. MockDocUpdaterServer.createMockDoc(project_id, doc_id, {
  94. lines,
  95. version,
  96. ops,
  97. ranges
  98. })
  99. return MockDocUpdaterServer.run((error) => {
  100. if (error != null) {
  101. throw error
  102. }
  103. return callback(null, { project_id, doc_id, lines, version, ops })
  104. })
  105. },
  106. setUpEditorSession(options, callback) {
  107. FixturesManager.setUpProject(options, (err, detailsProject) => {
  108. if (err) return callback(err)
  109. FixturesManager.setUpDoc(
  110. detailsProject.project_id,
  111. options,
  112. (err, detailsDoc) => {
  113. if (err) return callback(err)
  114. callback(null, Object.assign({}, detailsProject, detailsDoc))
  115. }
  116. )
  117. })
  118. },
  119. getRandomId() {
  120. return require('crypto')
  121. .createHash('sha1')
  122. .update(Math.random().toString())
  123. .digest('hex')
  124. .slice(0, 24)
  125. }
  126. }