UpdateMergerTests.coffee 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. Stream = require('stream')
  2. SandboxedModule = require('sandboxed-module')
  3. sinon = require('sinon')
  4. require('chai').should()
  5. modulePath = require('path').join __dirname, '../../../../app/js/Features/ThirdPartyDataStore/UpdateMerger.js'
  6. BufferedStream = require('bufferedstream')
  7. describe 'UpdateMerger :', ->
  8. beforeEach ->
  9. @editorController = {}
  10. @updateReciver = {}
  11. @projectLocator = {}
  12. @projectEntityHandler = {}
  13. @fs = {}
  14. @FileTypeManager = {}
  15. @LockManager =
  16. getLock:sinon.stub()
  17. releaseLock:sinon.stub()
  18. @updateMerger = SandboxedModule.require modulePath, requires:
  19. '../Editor/EditorController': @editorController
  20. '../Project/ProjectLocator': @projectLocator
  21. '../Project/ProjectEntityHandler': @projectEntityHandler
  22. 'fs': @fs
  23. '../Uploads/FileTypeManager':@FileTypeManager
  24. "../../infrastructure/LockManager": @LockManager
  25. 'logger-sharelatex':
  26. log: ->
  27. err: ->
  28. @project_id = "project_id_here"
  29. @source = "dropbox"
  30. @update = new BufferedStream()
  31. @update.headers = {}
  32. describe 'mergeUpdate :', ->
  33. beforeEach ->
  34. @path = "/doc1"
  35. @fsPath = "file/system/path.tex"
  36. @updateMerger.p.writeStreamToDisk = sinon.stub().callsArgWith(3, null, @fsPath)
  37. @LockManager.getLock.callsArgWith(1)
  38. @LockManager.releaseLock.callsArgWith(1)
  39. @FileTypeManager.isBinary = sinon.stub()
  40. it 'should get the element id', (done)->
  41. @projectLocator.findElementByPath = sinon.stub().callsArgWith(2)
  42. @FileTypeManager.isBinary.callsArgWith(2, null, false)
  43. @updateMerger.p.processDoc = sinon.stub().callsArgWith(5)
  44. @updateMerger.mergeUpdate @project_id, @path, @update, @source, =>
  45. @projectLocator.findElementByPath.calledWith(@project_id, @path).should.equal true
  46. done()
  47. it 'should process update as doc when it is a doc', (done)->
  48. doc_id = "231312s"
  49. @FileTypeManager.isBinary.callsArgWith(2, null, false)
  50. @projectLocator.findElementByPath = (_, __, cb)->cb(null, {_id:doc_id})
  51. @updateMerger.p.processDoc = sinon.stub().callsArgWith(5)
  52. filePath = "/folder/doc.tex"
  53. @updateMerger.mergeUpdate @project_id, filePath, @update, @source, =>
  54. @FileTypeManager.isBinary.calledWith(filePath, @fsPath).should.equal true
  55. @updateMerger.p.processDoc.calledWith(@project_id, doc_id, @fsPath, filePath, @source).should.equal true
  56. done()
  57. it 'should process update as file when it is not a doc', (done)->
  58. file_id = "1231"
  59. @projectLocator.findElementByPath = (_, __, cb)->cb(null, {_id:file_id})
  60. @FileTypeManager.isBinary.callsArgWith(2, null, true)
  61. @updateMerger.p.processFile = sinon.stub().callsArgWith(5)
  62. filePath = "/folder/file1.png"
  63. @updateMerger.mergeUpdate @project_id, filePath, @update, @source, =>
  64. @updateMerger.p.processFile.calledWith(@project_id, file_id, @fsPath, filePath, @source).should.equal true
  65. @FileTypeManager.isBinary.calledWith(filePath, @fsPath).should.equal true
  66. done()
  67. it "should get the lock", (done)->
  68. @FileTypeManager.isBinary.callsArgWith(2, null, false)
  69. @projectLocator.findElementByPath = (_, __, cb)->cb(null, {_id:"doc_id"})
  70. @updateMerger.p.processDoc = sinon.stub().callsArgWith(5)
  71. @updateMerger.mergeUpdate @project_id, @path, @update, @source, =>
  72. @LockManager.getLock.calledWith(@project_id).should.equal true
  73. done()
  74. describe 'processDoc :', (done)->
  75. beforeEach ->
  76. @doc_id = "312312klnkld"
  77. @docLines = "\\documentclass{article}\n\\usepackage[utf8]{inputenc}\n\n\\title{42}\n\\author{Jane Doe}\n\\date{June 2011}"
  78. @splitDocLines = @docLines.split("\n")
  79. @fs.readFile = sinon.stub().callsArgWith(2, null, @docLines)
  80. it 'should set the doc text in the editor controller', (done)->
  81. @editorController.setDoc = ->
  82. mock = sinon.mock(@editorController).expects("setDoc").withArgs(@project_id, @doc_id, @splitDocLines, @source).callsArg(4)
  83. @update.write(@docLines)
  84. @update.end()
  85. @updateMerger.p.processDoc @project_id, @doc_id, @update, "path", @source, ->
  86. mock.verify()
  87. done()
  88. it 'should create a new doc when it doesnt exist', (done)->
  89. folder = {_id:"adslkjioj"}
  90. docName = "main.tex"
  91. path = "folder1/folder2/#{docName}"
  92. @editorController.mkdirpWithoutLock = sinon.stub().withArgs(@project_id).callsArgWith(2, null, [folder], folder)
  93. @editorController.addDocWithoutLock = ->
  94. mock = sinon.mock(@editorController).expects("addDocWithoutLock").withArgs(@project_id, folder._id, docName, @splitDocLines, @source).callsArg(5)
  95. @update.write(@docLines)
  96. @update.end()
  97. @updateMerger.p.processDoc @project_id, undefined, @update, path, @source, ->
  98. mock.verify()
  99. done()
  100. describe 'processFile :', (done)->
  101. beforeEach ->
  102. @file_id = "file_id_here"
  103. @folder_id = "folder_id_here"
  104. @path = "folder/file.png"
  105. @folder = _id: @folder_id
  106. @fileName = "file.png"
  107. @fsPath = "fs/path.tex"
  108. @editorController.addFileWithoutLock = sinon.stub().callsArg(5)
  109. @editorController.replaceFile = sinon.stub().callsArg(4)
  110. @editorController.deleteEntityWithoutLock = sinon.stub()
  111. @editorController.mkdirpWithoutLock = sinon.stub().withArgs(@project_id).callsArgWith(2, null, [@folder], @folder)
  112. @updateMerger.p.writeStreamToDisk = sinon.stub().withArgs(@project_id, @file_id, @update).callsArgWith(3, null, @fsPath)
  113. it 'should replace file if the file already exists', (done)->
  114. @updateMerger.p.processFile @project_id, @file_id, @fsPath, @path, @source, =>
  115. @editorController.addFileWithoutLock.called.should.equal false
  116. @editorController.replaceFile.calledWith(@project_id, @file_id, @fsPath, @source).should.equal true
  117. done()
  118. it 'should call add file if the file does not exist', (done)->
  119. @updateMerger.p.processFile @project_id, undefined, @fsPath, @path, @source, =>
  120. @editorController.mkdirpWithoutLock.calledWith(@project_id, "folder/").should.equal true
  121. @editorController.addFileWithoutLock.calledWith(@project_id, @folder_id, @fileName, @fsPath, @source).should.equal true
  122. @editorController.replaceFile.called.should.equal false
  123. done()
  124. describe 'delete entity :', (done)->
  125. beforeEach ->
  126. @path = "folder/doc1"
  127. @editorController.deleteEntityWithoutLock = ->
  128. @entity_id = "entity_id_here"
  129. @entity = _id:@entity_id
  130. @projectLocator.findElementByPath = (project_id, path, cb)=> cb(null, @entity, @path)
  131. it 'should get the element id', ->
  132. @projectLocator.findElementByPath = sinon.spy()
  133. @updateMerger.deleteUpdate @project_id, @path, @source, ->
  134. @projectLocator.findElementByPath.calledWith(@project_id, @path).should.equal true
  135. it 'should delete the entity in the editor controller with type doc when entity has docLines array', (done)->
  136. @entity.lines = []
  137. mock = sinon.mock(@editorController).expects("deleteEntityWithoutLock").withArgs(@project_id, @entity_id, "doc", @source).callsArg(4)
  138. @updateMerger.deleteUpdate @project_id, @path, @source, ->
  139. mock.verify()
  140. done()
  141. it 'should delete the entity in the editor controller with type folder when entity has folders array', (done)->
  142. @entity.folders = []
  143. mock = sinon.mock(@editorController).expects("deleteEntityWithoutLock").withArgs(@project_id, @entity_id, "folder", @source).callsArg(4)
  144. @updateMerger.deleteUpdate @project_id, @path, @source, ->
  145. mock.verify()
  146. done()
  147. it 'should delete the entity in the editor controller with type file when entity has no interesting properties', (done)->
  148. mock = sinon.mock(@editorController).expects("deleteEntityWithoutLock").withArgs(@project_id, @entity_id, "file", @source).callsArg(4)
  149. @updateMerger.deleteUpdate @project_id, @path, @source, ->
  150. mock.verify()
  151. done()