TpdsUpdateHandlerTests.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const SandboxedModule = require('sandboxed-module')
  15. const sinon = require('sinon')
  16. require('chai').should()
  17. const { expect } = require('chai')
  18. const modulePath = require('path').join(
  19. __dirname,
  20. '../../../../app/src/Features/ThirdPartyDataStore/TpdsUpdateHandler.js'
  21. )
  22. describe('TpdsUpdateHandler', function() {
  23. beforeEach(function() {
  24. this.requestQueuer = {}
  25. this.updateMerger = {
  26. deleteUpdate(user_id, project_id, path, source, cb) {
  27. return cb()
  28. },
  29. mergeUpdate(user_id, project_id, path, update, source, cb) {
  30. return cb()
  31. }
  32. }
  33. this.editorController = {}
  34. this.project_id = 'dsjajilknaksdn'
  35. this.project = { _id: this.project_id, name: 'projectNameHere' }
  36. this.projectLocator = {
  37. findUsersProjectByName: sinon.stub().callsArgWith(2, null, this.project)
  38. }
  39. this.projectCreationHandler = {
  40. createBlankProject: sinon.stub().callsArgWith(2, null, this.project)
  41. }
  42. this.projectDeleter = {
  43. markAsDeletedByExternalSource: sinon.stub().callsArgWith(1)
  44. }
  45. this.rootDocManager = { setRootDocAutomatically: sinon.stub() }
  46. this.FileTypeManager = {
  47. shouldIgnore: sinon.stub().callsArgWith(1, null, false)
  48. }
  49. this.CooldownManager = {
  50. isProjectOnCooldown: sinon.stub().callsArgWith(1, null, false)
  51. }
  52. this.handler = SandboxedModule.require(modulePath, {
  53. requires: {
  54. './UpdateMerger': this.updateMerger,
  55. './Editor/EditorController': this.editorController,
  56. '../Project/ProjectLocator': this.projectLocator,
  57. '../Project/ProjectCreationHandler': this.projectCreationHandler,
  58. '../Project/ProjectDeleter': this.projectDeleter,
  59. '../Project/ProjectRootDocManager': this.rootDocManager,
  60. '../Uploads/FileTypeManager': this.FileTypeManager,
  61. '../Cooldown/CooldownManager': this.CooldownManager,
  62. 'logger-sharelatex': {
  63. log() {}
  64. }
  65. }
  66. })
  67. this.user_id = 'dsad29jlkjas'
  68. return (this.source = 'dropbox')
  69. })
  70. describe('getting an update', function() {
  71. it('should send the update to the update merger', function(done) {
  72. const path = '/path/here'
  73. const update = {}
  74. this.updateMerger.mergeUpdate = sinon.stub()
  75. this.updateMerger.mergeUpdate
  76. .withArgs(this.user_id, this.project_id, path, update, this.source)
  77. .callsArg(5)
  78. return this.handler.newUpdate(
  79. this.user_id,
  80. this.project.name,
  81. path,
  82. update,
  83. this.source,
  84. () => {
  85. this.projectCreationHandler.createBlankProject.called.should.equal(
  86. false
  87. )
  88. return done()
  89. }
  90. )
  91. })
  92. it('should create a new project if one does not already exit', function(done) {
  93. this.projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
  94. const path = '/'
  95. return this.handler.newUpdate(
  96. this.user_id,
  97. this.project.name,
  98. path,
  99. {},
  100. this.source,
  101. () => {
  102. this.projectCreationHandler.createBlankProject
  103. .calledWith(this.user_id, this.project.name)
  104. .should.equal(true)
  105. return done()
  106. }
  107. )
  108. })
  109. it('should set the root doc automatically if a new project is created', function(done) {
  110. this.projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
  111. this.handler._rootDocTimeoutLength = 0
  112. const path = '/'
  113. return this.handler.newUpdate(
  114. this.user_id,
  115. this.project.name,
  116. path,
  117. {},
  118. this.source,
  119. () => {
  120. return setTimeout(() => {
  121. this.rootDocManager.setRootDocAutomatically
  122. .calledWith(this.project._id)
  123. .should.equal(true)
  124. return done()
  125. }, 1)
  126. }
  127. )
  128. })
  129. it('should not update files that should be ignored', function(done) {
  130. this.FileTypeManager.shouldIgnore = sinon
  131. .stub()
  132. .callsArgWith(1, null, true)
  133. this.projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
  134. const path = '/.gitignore'
  135. this.updateMerger.mergeUpdate = sinon.stub()
  136. return this.handler.newUpdate(
  137. this.user_id,
  138. this.project.name,
  139. path,
  140. {},
  141. this.source,
  142. () => {
  143. this.updateMerger.mergeUpdate.called.should.equal(false)
  144. return done()
  145. }
  146. )
  147. })
  148. it('should check if the project is on cooldown', function(done) {
  149. this.CooldownManager.isProjectOnCooldown = sinon
  150. .stub()
  151. .callsArgWith(1, null, false)
  152. this.projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
  153. const path = '/path/here'
  154. const update = {}
  155. this.updateMerger.mergeUpdate = sinon.stub()
  156. this.updateMerger.mergeUpdate
  157. .withArgs(this.user_id, this.project_id, path, update, this.source)
  158. .callsArg(5)
  159. return this.handler.newUpdate(
  160. this.user_id,
  161. this.project.name,
  162. path,
  163. update,
  164. this.source,
  165. err => {
  166. expect(err).to.be.oneOf([null, undefined])
  167. this.CooldownManager.isProjectOnCooldown.callCount.should.equal(1)
  168. this.CooldownManager.isProjectOnCooldown
  169. .calledWith(this.project_id)
  170. .should.equal(true)
  171. this.FileTypeManager.shouldIgnore.callCount.should.equal(1)
  172. this.updateMerger.mergeUpdate.callCount.should.equal(1)
  173. return done()
  174. }
  175. )
  176. })
  177. it('should return error and not proceed with update if project is on cooldown', function(done) {
  178. this.CooldownManager.isProjectOnCooldown = sinon
  179. .stub()
  180. .callsArgWith(1, null, true)
  181. this.projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
  182. this.FileTypeManager.shouldIgnore = sinon
  183. .stub()
  184. .callsArgWith(1, null, false)
  185. const path = '/path/here'
  186. const update = {}
  187. this.updateMerger.mergeUpdate = sinon.stub()
  188. this.updateMerger.mergeUpdate
  189. .withArgs(this.user_id, this.project_id, path, update, this.source)
  190. .callsArg(5)
  191. return this.handler.newUpdate(
  192. this.user_id,
  193. this.project.name,
  194. path,
  195. update,
  196. this.source,
  197. err => {
  198. expect(err).to.not.be.oneOf([null, undefined])
  199. expect(err).to.be.instanceof(Error)
  200. this.CooldownManager.isProjectOnCooldown.callCount.should.equal(1)
  201. this.CooldownManager.isProjectOnCooldown
  202. .calledWith(this.project_id)
  203. .should.equal(true)
  204. this.FileTypeManager.shouldIgnore.callCount.should.equal(0)
  205. this.updateMerger.mergeUpdate.callCount.should.equal(0)
  206. return done()
  207. }
  208. )
  209. })
  210. })
  211. describe('getting a delete :', function() {
  212. it('should call deleteEntity in the collaberation manager', function(done) {
  213. const path = '/delete/this'
  214. const update = {}
  215. this.updateMerger.deleteUpdate = sinon.stub().callsArg(4)
  216. return this.handler.deleteUpdate(
  217. this.user_id,
  218. this.project.name,
  219. path,
  220. this.source,
  221. () => {
  222. this.projectDeleter.markAsDeletedByExternalSource
  223. .calledWith(this.project._id)
  224. .should.equal(false)
  225. this.updateMerger.deleteUpdate
  226. .calledWith(this.user_id, this.project_id, path, this.source)
  227. .should.equal(true)
  228. return done()
  229. }
  230. )
  231. })
  232. it('should mark the project as deleted by external source if path is a single slash', function(done) {
  233. const path = '/'
  234. return this.handler.deleteUpdate(
  235. this.user_id,
  236. this.project.name,
  237. path,
  238. this.source,
  239. () => {
  240. this.projectDeleter.markAsDeletedByExternalSource
  241. .calledWith(this.project._id)
  242. .should.equal(true)
  243. return done()
  244. }
  245. )
  246. })
  247. })
  248. })