UpdateMergerTests.coffee 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. 'settings-sharelatex':{path:{dumpPath:"dump_here"}}
  18. @project_id = "project_id_here"
  19. @user_id = "mock-user-id"
  20. @docPath = "/folder/doc.tex"
  21. @filePath = "/folder/file.png"
  22. @fsPath = "/tmp/file/path"
  23. @source = "dropbox"
  24. @updateRequest = new BufferedStream()
  25. @updateMerger.p.writeStreamToDisk = sinon.stub().yields(null, @fsPath)
  26. @callback = sinon.stub()
  27. describe 'mergeUpdate', ->
  28. describe "doc updates", ->
  29. beforeEach ->
  30. @FileTypeManager.isBinary = sinon.stub().yields(null, false)
  31. @updateMerger.p.processDoc = sinon.stub().yields()
  32. @updateMerger.mergeUpdate @user_id, @project_id, @docPath, @updateRequest, @source, @callback
  33. it 'should process update as doc', ->
  34. @updateMerger.p.processDoc
  35. .calledWith(@project_id, @user_id, @fsPath, @docPath, @source)
  36. .should.equal true
  37. it 'removes the temp file from disk', ->
  38. @fs.unlink.calledWith(@fsPath).should.equal true
  39. describe "file updates", ->
  40. beforeEach ->
  41. @FileTypeManager.isBinary = sinon.stub().yields(null, true)
  42. @updateMerger.p.processFile = sinon.stub().yields()
  43. @updateMerger.mergeUpdate @user_id, @project_id, @filePath, @updateRequest, @source, @callback
  44. it 'should process update as file', ->
  45. @updateMerger.p.processFile
  46. .calledWith(@project_id, @fsPath, @filePath, @source, @user_id)
  47. .should.equal true
  48. it 'removes the temp file from disk', ->
  49. @fs.unlink.calledWith(@fsPath).should.equal true
  50. describe 'deleteUpdate', ->
  51. beforeEach ->
  52. @EditorController.deleteEntityWithPath = sinon.stub().yields()
  53. @updateMerger.deleteUpdate @user_id, @project_id, @docPath, @source, @callback
  54. it 'should delete the entity in the editor controller', ->
  55. @EditorController.deleteEntityWithPath
  56. .calledWith(@project_id, @docPath, @source, @user_id)
  57. .should.equal true
  58. describe 'private methods', ->
  59. describe 'processDoc', ->
  60. beforeEach ->
  61. @docLines = "\\documentclass{article}\n\\usepackage[utf8]{inputenc}\n\n\\title{42}\n\\author{Jane Doe}\n\\date{June 2011}"
  62. @updateMerger.p.readFileIntoTextArray = sinon.stub().yields(null, @docLines)
  63. @EditorController.upsertDocWithPath = sinon.stub().yields()
  64. @updateMerger.p.processDoc @project_id, @user_id, @fsPath, @docPath, @source, @callback
  65. it 'reads the temp file from disk', ->
  66. @updateMerger.p.readFileIntoTextArray
  67. .calledWith(@fsPath)
  68. .should.equal true
  69. it 'should upsert the doc in the editor controller', ->
  70. @EditorController.upsertDocWithPath
  71. .calledWith(@project_id, @docPath, @docLines, @source, @user_id)
  72. .should.equal true
  73. describe 'processFile', ->
  74. beforeEach ->
  75. @EditorController.upsertFileWithPath = sinon.stub().yields()
  76. @updateMerger.p.processFile @project_id, @fsPath, @filePath, @source, @user_id, @callback
  77. it 'should upsert the file in the editor controller', ->
  78. @EditorController.upsertFileWithPath
  79. .calledWith(@project_id, @filePath, @fsPath, @source, @user_id)
  80. .should.equal true