UpdateMergerTests.coffee 3.6 KB

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