UpdateMergerTests.coffee 7.0 KB

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