FixturesManager.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Fix any style issues and re-enable lint.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS102: Remove unnecessary code created because of implicit returns
  6. * DS207: Consider shorter variations of null checks
  7. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  8. */
  9. let FixturesManager
  10. const RealTimeClient = require('./RealTimeClient')
  11. const MockWebServer = require('./MockWebServer')
  12. const MockDocUpdaterServer = require('./MockDocUpdaterServer')
  13. module.exports = FixturesManager = {
  14. setUpProject(options, callback) {
  15. if (options == null) {
  16. options = {}
  17. }
  18. if (callback == null) {
  19. callback = function () {}
  20. }
  21. if (!options.user_id) {
  22. options.user_id = FixturesManager.getRandomId()
  23. }
  24. if (!options.project_id) {
  25. options.project_id = FixturesManager.getRandomId()
  26. }
  27. if (!options.project) {
  28. options.project = { name: 'Test Project' }
  29. }
  30. let {
  31. project_id: projectId,
  32. user_id: userId,
  33. privilegeLevel,
  34. project,
  35. publicAccess,
  36. userMetadata,
  37. anonymousAccessToken,
  38. } = options
  39. if (privilegeLevel === 'owner') {
  40. project.owner = { _id: userId }
  41. } else {
  42. project.owner = { _id: '404404404404404404404404' }
  43. }
  44. const privileges = {}
  45. privileges[userId] = privilegeLevel
  46. if (publicAccess) {
  47. anonymousAccessToken =
  48. anonymousAccessToken || FixturesManager.getRandomId()
  49. privileges[anonymousAccessToken] = publicAccess
  50. }
  51. const metadataByUser = {}
  52. metadataByUser[userId] = userMetadata
  53. MockWebServer.createMockProject(
  54. projectId,
  55. privileges,
  56. project,
  57. metadataByUser
  58. )
  59. return MockWebServer.run(error => {
  60. if (error != null) {
  61. throw error
  62. }
  63. return RealTimeClient.setSession(
  64. {
  65. user: {
  66. _id: userId,
  67. first_name: 'Joe',
  68. last_name: 'Bloggs',
  69. },
  70. },
  71. error => {
  72. if (error != null) {
  73. throw error
  74. }
  75. return callback(null, {
  76. project_id: projectId,
  77. user_id: userId,
  78. privilegeLevel,
  79. project,
  80. anonymousAccessToken,
  81. })
  82. }
  83. )
  84. })
  85. },
  86. setUpDoc(projectId, options, callback) {
  87. if (options == null) {
  88. options = {}
  89. }
  90. if (callback == null) {
  91. callback = function () {}
  92. }
  93. if (!options.doc_id) {
  94. options.doc_id = FixturesManager.getRandomId()
  95. }
  96. if (!options.lines) {
  97. options.lines = ['doc', 'lines']
  98. }
  99. if (!options.version) {
  100. options.version = 42
  101. }
  102. if (!options.ops) {
  103. options.ops = ['mock', 'ops']
  104. }
  105. const { doc_id: docId, lines, version, ops, ranges } = options
  106. MockDocUpdaterServer.createMockDoc(projectId, docId, {
  107. lines,
  108. version,
  109. ops,
  110. ranges,
  111. })
  112. return MockDocUpdaterServer.run(error => {
  113. if (error != null) {
  114. throw error
  115. }
  116. return callback(null, {
  117. project_id: projectId,
  118. doc_id: docId,
  119. lines,
  120. version,
  121. ops,
  122. })
  123. })
  124. },
  125. setUpEditorSession(options, callback) {
  126. FixturesManager.setUpProject(options, (err, detailsProject) => {
  127. if (err) return callback(err)
  128. FixturesManager.setUpDoc(
  129. detailsProject.project_id,
  130. options,
  131. (err, detailsDoc) => {
  132. if (err) return callback(err)
  133. callback(null, Object.assign({}, detailsProject, detailsDoc))
  134. }
  135. )
  136. })
  137. },
  138. getRandomId() {
  139. return require('crypto')
  140. .createHash('sha1')
  141. .update(Math.random().toString())
  142. .digest('hex')
  143. .slice(0, 24)
  144. },
  145. }