UpdateMergerTests.coffee 7.0 KB

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