2014-02-12 05:23:40 -05:00
|
|
|
Stream = require('stream')
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
sinon = require('sinon')
|
|
|
|
require('chai').should()
|
|
|
|
modulePath = require('path').join __dirname, '../../../../app/js/Features/ThirdPartyDataStore/UpdateMerger.js'
|
|
|
|
BufferedStream = require('bufferedstream')
|
|
|
|
|
|
|
|
describe 'UpdateMerger :', ->
|
|
|
|
beforeEach ->
|
|
|
|
@updateMerger = SandboxedModule.require modulePath, requires:
|
2018-02-01 10:31:42 -05:00
|
|
|
'fs': @fs =
|
|
|
|
unlink:sinon.stub().callsArgWith(1)
|
2014-02-12 05:23:40 -05:00
|
|
|
'logger-sharelatex':
|
|
|
|
log: ->
|
|
|
|
err: ->
|
2018-02-01 10:31:42 -05:00
|
|
|
'../Editor/EditorController': @EditorController = {}
|
|
|
|
'../Uploads/FileTypeManager':@FileTypeManager = {}
|
2018-02-20 07:52:57 -05:00
|
|
|
'../../infrastructure/FileWriter': @FileWriter = {}
|
2018-02-01 10:31:42 -05:00
|
|
|
'settings-sharelatex':{path:{dumpPath:"dump_here"}}
|
2014-02-12 05:23:40 -05:00
|
|
|
@project_id = "project_id_here"
|
2016-02-04 09:26:50 -05:00
|
|
|
@user_id = "mock-user-id"
|
2018-02-01 10:31:42 -05:00
|
|
|
|
|
|
|
@docPath = "/folder/doc.tex"
|
|
|
|
@filePath = "/folder/file.png"
|
2018-02-20 07:52:57 -05:00
|
|
|
@linkedFileData = {provider: 'url'}
|
2018-02-01 10:31:42 -05:00
|
|
|
|
|
|
|
@fsPath = "/tmp/file/path"
|
|
|
|
|
2014-10-15 09:11:02 -04:00
|
|
|
@source = "dropbox"
|
2018-02-01 10:31:42 -05:00
|
|
|
@updateRequest = new BufferedStream()
|
2018-02-20 07:52:57 -05:00
|
|
|
@FileWriter.writeStreamToDisk = sinon.stub().yields(null, @fsPath)
|
2018-02-01 10:31:42 -05:00
|
|
|
@callback = sinon.stub()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-01-29 07:46:21 -05:00
|
|
|
describe 'mergeUpdate', ->
|
2018-02-01 10:31:42 -05:00
|
|
|
describe "doc updates", ->
|
2018-01-29 08:08:39 -05:00
|
|
|
beforeEach ->
|
2018-02-01 10:31:42 -05:00
|
|
|
@FileTypeManager.isBinary = sinon.stub().yields(null, false)
|
|
|
|
@updateMerger.p.processDoc = sinon.stub().yields()
|
|
|
|
@updateMerger.mergeUpdate @user_id, @project_id, @docPath, @updateRequest, @source, @callback
|
|
|
|
|
|
|
|
it 'should process update as doc', ->
|
|
|
|
@updateMerger.p.processDoc
|
|
|
|
.calledWith(@project_id, @user_id, @fsPath, @docPath, @source)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it 'removes the temp file from disk', ->
|
|
|
|
@fs.unlink.calledWith(@fsPath).should.equal true
|
|
|
|
|
|
|
|
describe "file updates", ->
|
2018-01-29 08:08:39 -05:00
|
|
|
beforeEach ->
|
2018-02-01 10:31:42 -05:00
|
|
|
@FileTypeManager.isBinary = sinon.stub().yields(null, true)
|
|
|
|
@updateMerger.p.processFile = sinon.stub().yields()
|
|
|
|
@updateMerger.mergeUpdate @user_id, @project_id, @filePath, @updateRequest, @source, @callback
|
|
|
|
|
|
|
|
it 'should process update as file', ->
|
|
|
|
@updateMerger.p.processFile
|
|
|
|
.calledWith(@project_id, @fsPath, @filePath, @source, @user_id)
|
2018-01-29 07:44:38 -05:00
|
|
|
.should.equal true
|
2018-01-29 07:46:21 -05:00
|
|
|
|
2018-02-01 10:31:42 -05:00
|
|
|
it 'removes the temp file from disk', ->
|
|
|
|
@fs.unlink.calledWith(@fsPath).should.equal true
|
|
|
|
|
|
|
|
describe 'deleteUpdate', ->
|
|
|
|
beforeEach ->
|
|
|
|
@EditorController.deleteEntityWithPath = sinon.stub().yields()
|
|
|
|
@updateMerger.deleteUpdate @user_id, @project_id, @docPath, @source, @callback
|
|
|
|
|
|
|
|
it 'should delete the entity in the editor controller', ->
|
|
|
|
@EditorController.deleteEntityWithPath
|
|
|
|
.calledWith(@project_id, @docPath, @source, @user_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe 'private methods', ->
|
|
|
|
describe 'processDoc', ->
|
2018-01-29 07:46:21 -05:00
|
|
|
beforeEach ->
|
|
|
|
@docLines = "\\documentclass{article}\n\\usepackage[utf8]{inputenc}\n\n\\title{42}\n\\author{Jane Doe}\n\\date{June 2011}"
|
2018-02-01 10:31:42 -05:00
|
|
|
@updateMerger.p.readFileIntoTextArray = sinon.stub().yields(null, @docLines)
|
|
|
|
@EditorController.upsertDocWithPath = sinon.stub().yields()
|
|
|
|
|
|
|
|
@updateMerger.p.processDoc @project_id, @user_id, @fsPath, @docPath, @source, @callback
|
|
|
|
|
|
|
|
it 'reads the temp file from disk', ->
|
|
|
|
@updateMerger.p.readFileIntoTextArray
|
|
|
|
.calledWith(@fsPath)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it 'should upsert the doc in the editor controller', ->
|
|
|
|
@EditorController.upsertDocWithPath
|
|
|
|
.calledWith(@project_id, @docPath, @docLines, @source, @user_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe 'processFile', ->
|
2018-01-29 07:46:21 -05:00
|
|
|
beforeEach ->
|
2018-02-01 10:31:42 -05:00
|
|
|
@EditorController.upsertFileWithPath = sinon.stub().yields()
|
|
|
|
@updateMerger.p.processFile @project_id, @fsPath, @filePath, @source, @user_id, @callback
|
|
|
|
|
|
|
|
it 'should upsert the file in the editor controller', ->
|
|
|
|
@EditorController.upsertFileWithPath
|
2018-02-20 07:52:57 -05:00
|
|
|
.calledWith(@project_id, @filePath, @fsPath, null, @source, @user_id)
|
2018-02-01 10:31:42 -05:00
|
|
|
.should.equal true
|