UpdateMergerTests.coffee 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. @updateMerger = SandboxedModule.require modulePath, requires:
  10. 'fs': @fs =
  11. unlink:sinon.stub().callsArgWith(1)
  12. 'logger-sharelatex':
  13. log: ->
  14. err: ->
  15. '../Editor/EditorController': @EditorController = {}
  16. '../Uploads/FileTypeManager':@FileTypeManager = {}
  17. '../../infrastructure/FileWriter': @FileWriter = {}
  18. '../Project/ProjectEntityHandler': @ProjectEntityHandler = {}
  19. 'settings-sharelatex':{path:{dumpPath:"dump_here"}}
  20. @project_id = "project_id_here"
  21. @user_id = "mock-user-id"
  22. @docPath = @newDocPath = "/folder/doc.tex"
  23. @filePath = @newFilePath = "/folder/file.png"
  24. @existingDocPath = '/folder/other.tex'
  25. @existingFilePath = '/folder/fig1.pdf'
  26. @linkedFileData = {provider: 'url'}
  27. @existingDocs = [
  28. {path: '/main.tex'}
  29. {path: '/folder/other.tex'}
  30. ]
  31. @existingFiles = [
  32. {path: '/figure.pdf'}
  33. {path: '/folder/fig1.pdf'}
  34. ]
  35. @ProjectEntityHandler.getAllEntities = sinon.stub().callsArgWith(1, null, @existingDocs, @existingFiles)
  36. @fsPath = "/tmp/file/path"
  37. @source = "dropbox"
  38. @updateRequest = new BufferedStream()
  39. @FileWriter.writeStreamToDisk = sinon.stub().yields(null, @fsPath)
  40. @callback = sinon.stub()
  41. describe 'mergeUpdate', ->
  42. describe "doc updates for a new doc", ->
  43. beforeEach ->
  44. @FileTypeManager.isBinary = sinon.stub().yields(null, false)
  45. @updateMerger.p.processDoc = sinon.stub().yields()
  46. @updateMerger.mergeUpdate @user_id, @project_id, @docPath, @updateRequest, @source, @callback
  47. it 'should look at the file contents', ->
  48. @FileTypeManager.isBinary.called.should.equal true
  49. it 'should process update as doc', ->
  50. @updateMerger.p.processDoc
  51. .calledWith(@project_id, @user_id, @fsPath, @docPath, @source)
  52. .should.equal true
  53. it 'removes the temp file from disk', ->
  54. @fs.unlink.calledWith(@fsPath).should.equal true
  55. describe "file updates for a new file ", ->
  56. beforeEach ->
  57. @FileTypeManager.isBinary = sinon.stub().yields(null, true)
  58. @updateMerger.p.processFile = sinon.stub().yields()
  59. @updateMerger.mergeUpdate @user_id, @project_id, @filePath, @updateRequest, @source, @callback
  60. it 'should look at the file contents', ->
  61. @FileTypeManager.isBinary.called.should.equal true
  62. it 'should process update as file', ->
  63. @updateMerger.p.processFile
  64. .calledWith(@project_id, @fsPath, @filePath, @source, @user_id)
  65. .should.equal true
  66. it 'removes the temp file from disk', ->
  67. @fs.unlink.calledWith(@fsPath).should.equal true
  68. describe "doc updates for an existing doc", ->
  69. beforeEach ->
  70. @FileTypeManager.isBinary = sinon.stub()
  71. @updateMerger.p.processDoc = sinon.stub().yields()
  72. @updateMerger.mergeUpdate @user_id, @project_id, @existingDocPath, @updateRequest, @source, @callback
  73. it 'should not look at the file contents', ->
  74. @FileTypeManager.isBinary.called.should.equal false
  75. it 'should process update as doc', ->
  76. @updateMerger.p.processDoc
  77. .calledWith(@project_id, @user_id, @fsPath, @existingDocPath, @source)
  78. .should.equal true
  79. it 'removes the temp file from disk', ->
  80. @fs.unlink.calledWith(@fsPath).should.equal true
  81. describe "file updates for an existing file", ->
  82. beforeEach ->
  83. @FileTypeManager.isBinary = sinon.stub()
  84. @updateMerger.p.processFile = sinon.stub().yields()
  85. @updateMerger.mergeUpdate @user_id, @project_id, @existingFilePath, @updateRequest, @source, @callback
  86. it 'should not look at the file contents', ->
  87. @FileTypeManager.isBinary.called.should.equal false
  88. it 'should process update as file', ->
  89. @updateMerger.p.processFile
  90. .calledWith(@project_id, @fsPath, @existingFilePath, @source, @user_id)
  91. .should.equal true
  92. it 'removes the temp file from disk', ->
  93. @fs.unlink.calledWith(@fsPath).should.equal true
  94. describe 'deleteUpdate', ->
  95. beforeEach ->
  96. @EditorController.deleteEntityWithPath = sinon.stub().yields()
  97. @updateMerger.deleteUpdate @user_id, @project_id, @docPath, @source, @callback
  98. it 'should delete the entity in the editor controller', ->
  99. @EditorController.deleteEntityWithPath
  100. .calledWith(@project_id, @docPath, @source, @user_id)
  101. .should.equal true
  102. describe 'private methods', ->
  103. describe 'processDoc', ->
  104. beforeEach ->
  105. @docLines = "\\documentclass{article}\n\\usepackage[utf8]{inputenc}\n\n\\title{42}\n\\author{Jane Doe}\n\\date{June 2011}"
  106. @updateMerger.p.readFileIntoTextArray = sinon.stub().yields(null, @docLines)
  107. @EditorController.upsertDocWithPath = sinon.stub().yields()
  108. @updateMerger.p.processDoc @project_id, @user_id, @fsPath, @docPath, @source, @callback
  109. it 'reads the temp file from disk', ->
  110. @updateMerger.p.readFileIntoTextArray
  111. .calledWith(@fsPath)
  112. .should.equal true
  113. it 'should upsert the doc in the editor controller', ->
  114. @EditorController.upsertDocWithPath
  115. .calledWith(@project_id, @docPath, @docLines, @source, @user_id)
  116. .should.equal true
  117. describe 'processFile', ->
  118. beforeEach ->
  119. @EditorController.upsertFileWithPath = sinon.stub().yields()
  120. @updateMerger.p.processFile @project_id, @fsPath, @filePath, @source, @user_id, @callback
  121. it 'should upsert the file in the editor controller', ->
  122. @EditorController.upsertFileWithPath
  123. .calledWith(@project_id, @filePath, @fsPath, null, @source, @user_id)
  124. .should.equal true