UpdateMergerTests.coffee 6.8 KB

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