UpdateMergerTests.coffee 6.8 KB

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