UpdateMergerTests.coffee 7.2 KB

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