TpdsUpdateHandlerTests.coffee 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. expect = require('chai').expect
  5. modulePath = require('path').join __dirname, '../../../../app/js/Features/ThirdPartyDataStore/TpdsUpdateHandler.js'
  6. describe 'TpdsUpdateHandler', ->
  7. beforeEach ->
  8. @requestQueuer = {}
  9. @updateMerger =
  10. deleteUpdate: (user_id, project_id, path, source, cb)->cb()
  11. mergeUpdate:(user_id, project_id, path, update, source, cb)->cb()
  12. @editorController = {}
  13. @project_id = "dsjajilknaksdn"
  14. @project = {_id:@project_id, name:"projectNameHere"}
  15. @projectLocator = findUsersProjectByName:sinon.stub().callsArgWith(2, null, @project)
  16. @projectCreationHandler =
  17. createBlankProject : sinon.stub().callsArgWith(2, null, @project)
  18. @projectDeleter = {markAsDeletedByExternalSource:sinon.stub().callsArgWith(1)}
  19. @rootDocManager = setRootDocAutomatically:sinon.stub()
  20. @FileTypeManager =
  21. shouldIgnore: sinon.stub().callsArgWith(1, null, false)
  22. @CooldownManager =
  23. isProjectOnCooldown: sinon.stub().callsArgWith(1, null, false)
  24. @handler = SandboxedModule.require modulePath, requires:
  25. './UpdateMerger': @updateMerger
  26. './Editor/EditorController': @editorController
  27. '../Project/ProjectLocator': @projectLocator
  28. '../Project/ProjectCreationHandler':@projectCreationHandler
  29. '../Project/ProjectDeleter': @projectDeleter
  30. "../Project/ProjectRootDocManager" : @rootDocManager
  31. '../Uploads/FileTypeManager': @FileTypeManager
  32. '../Cooldown/CooldownManager': @CooldownManager
  33. 'logger-sharelatex': log:->
  34. @user_id = "dsad29jlkjas"
  35. @source = "dropbox"
  36. describe 'getting an update', ->
  37. it 'should send the update to the update merger', (done)->
  38. path = "/path/here"
  39. update = {}
  40. @updateMerger.mergeUpdate = sinon.stub()
  41. @updateMerger.mergeUpdate.withArgs(@user_id, @project_id, path, update, @source).callsArg(5)
  42. @handler.newUpdate @user_id, @project.name, path, update, @source, =>
  43. @projectCreationHandler.createBlankProject.called.should.equal false
  44. done()
  45. it 'should create a new project if one does not already exit', (done)->
  46. @projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
  47. path = "/"
  48. @handler.newUpdate @user_id, @project.name, path, {}, @source, =>
  49. @projectCreationHandler.createBlankProject.calledWith(@user_id, @project.name).should.equal true
  50. done()
  51. it 'should set the root doc automatically if a new project is created', (done)->
  52. @projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
  53. @handler._rootDocTimeoutLength = 0
  54. path = "/"
  55. @handler.newUpdate @user_id, @project.name, path, {}, @source, =>
  56. setTimeout (=>
  57. @rootDocManager.setRootDocAutomatically.calledWith(@project._id).should.equal true
  58. done()
  59. ), 1
  60. it 'should not update files that should be ignored', (done) ->
  61. @FileTypeManager.shouldIgnore = sinon.stub().callsArgWith(1, null, true)
  62. @projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
  63. path = "/.gitignore"
  64. @updateMerger.mergeUpdate = sinon.stub()
  65. @handler.newUpdate @user_id, @project.name, path, {}, @source, =>
  66. @updateMerger.mergeUpdate.called.should.equal false
  67. done()
  68. it 'should check if the project is on cooldown', (done) ->
  69. @CooldownManager.isProjectOnCooldown = sinon.stub().callsArgWith(1, null, false)
  70. @projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
  71. path = "/path/here"
  72. update = {}
  73. @updateMerger.mergeUpdate = sinon.stub()
  74. @updateMerger.mergeUpdate.withArgs(@user_id, @project_id, path, update, @source).callsArg(5)
  75. @handler.newUpdate @user_id, @project.name, path, update, @source, (err) =>
  76. expect(err).to.be.oneOf [null, undefined]
  77. @CooldownManager.isProjectOnCooldown.callCount.should.equal 1
  78. @CooldownManager.isProjectOnCooldown.calledWith(@project_id).should.equal true
  79. @FileTypeManager.shouldIgnore.callCount.should.equal 1
  80. @updateMerger.mergeUpdate.callCount.should.equal 1
  81. done()
  82. it 'should return error and not proceed with update if project is on cooldown', (done) ->
  83. @CooldownManager.isProjectOnCooldown = sinon.stub().callsArgWith(1, null, true)
  84. @projectLocator.findUsersProjectByName = sinon.stub().callsArgWith(2)
  85. @FileTypeManager.shouldIgnore = sinon.stub().callsArgWith(1, null, false)
  86. path = "/path/here"
  87. update = {}
  88. @updateMerger.mergeUpdate = sinon.stub()
  89. @updateMerger.mergeUpdate.withArgs(@user_id, @project_id, path, update, @source).callsArg(5)
  90. @handler.newUpdate @user_id, @project.name, path, update, @source, (err) =>
  91. expect(err).to.not.be.oneOf [null, undefined]
  92. expect(err).to.be.instanceof Error
  93. @CooldownManager.isProjectOnCooldown.callCount.should.equal 1
  94. @CooldownManager.isProjectOnCooldown.calledWith(@project_id).should.equal true
  95. @FileTypeManager.shouldIgnore.callCount.should.equal 0
  96. @updateMerger.mergeUpdate.callCount.should.equal 0
  97. done()
  98. describe 'getting a delete :', ->
  99. it 'should call deleteEntity in the collaberation manager', (done)->
  100. path = "/delete/this"
  101. update = {}
  102. @updateMerger.deleteUpdate = sinon.stub().callsArg(4)
  103. @handler.deleteUpdate @user_id, @project.name, path, @source, =>
  104. @projectDeleter.markAsDeletedByExternalSource.calledWith(@project._id).should.equal false
  105. @updateMerger.deleteUpdate
  106. .calledWith(@user_id, @project_id, path, @source)
  107. .should.equal true
  108. done()
  109. it 'should mark the project as deleted by external source if path is a single slash', (done)->
  110. path = "/"
  111. @handler.deleteUpdate @user_id, @project.name, path, @source, =>
  112. @projectDeleter.markAsDeletedByExternalSource.calledWith(@project._id).should.equal true
  113. done()