UpdateMergerTests.coffee 7.1 KB

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