UpdateMergerTests.coffee 6.6 KB

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