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 ->
|
|
|
|
@editorController = {}
|
|
|
|
@updateReciver = {}
|
|
|
|
@projectLocator = {}
|
|
|
|
@projectEntityHandler = {}
|
2015-04-22 10:08:29 -04:00
|
|
|
@fs =
|
|
|
|
unlink:sinon.stub().callsArgWith(1)
|
2014-02-12 05:23:40 -05:00
|
|
|
@FileTypeManager = {}
|
|
|
|
@updateMerger = SandboxedModule.require modulePath, requires:
|
|
|
|
'../Editor/EditorController': @editorController
|
|
|
|
'../Project/ProjectLocator': @projectLocator
|
|
|
|
'../Project/ProjectEntityHandler': @projectEntityHandler
|
|
|
|
'fs': @fs
|
|
|
|
'../Uploads/FileTypeManager':@FileTypeManager
|
2016-03-09 07:51:19 -05:00
|
|
|
'settings-sharelatex':{path:{dumpPath:"dump_here"}}
|
2014-02-12 05:23:40 -05:00
|
|
|
'logger-sharelatex':
|
|
|
|
log: ->
|
|
|
|
err: ->
|
2017-04-03 11:18:30 -04:00
|
|
|
"metrics-sharelatex":
|
2016-03-09 07:51:19 -05:00
|
|
|
Timer:->
|
|
|
|
done:->
|
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"
|
2014-10-15 09:11:02 -04:00
|
|
|
@source = "dropbox"
|
2014-02-12 05:23:40 -05:00
|
|
|
@update = new BufferedStream()
|
|
|
|
@update.headers = {}
|
|
|
|
|
|
|
|
describe 'mergeUpdate :', ->
|
|
|
|
beforeEach ->
|
|
|
|
@path = "/doc1"
|
|
|
|
@fsPath = "file/system/path.tex"
|
|
|
|
@updateMerger.p.writeStreamToDisk = sinon.stub().callsArgWith(3, null, @fsPath)
|
|
|
|
@FileTypeManager.isBinary = sinon.stub()
|
|
|
|
|
|
|
|
it 'should get the element id', (done)->
|
2014-11-27 06:46:17 -05:00
|
|
|
@projectLocator.findElementByPath = sinon.spy()
|
2016-02-04 09:26:50 -05:00
|
|
|
@updateMerger.mergeUpdate @user_id, @project_id, @path, @update, @source, =>
|
2014-11-27 06:46:17 -05:00
|
|
|
@projectLocator.findElementByPath.calledWith(@project_id, @path).should.equal true
|
|
|
|
done()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
it 'should process update as doc when it is a doc', (done)->
|
|
|
|
doc_id = "231312s"
|
2014-09-04 08:00:41 -04:00
|
|
|
@FileTypeManager.isBinary.callsArgWith(2, null, false)
|
2014-02-12 05:23:40 -05:00
|
|
|
@projectLocator.findElementByPath = (_, __, cb)->cb(null, {_id:doc_id})
|
2016-02-04 09:26:50 -05:00
|
|
|
@updateMerger.p.processDoc = sinon.stub().callsArgWith(6)
|
2014-02-12 05:23:40 -05:00
|
|
|
filePath = "/folder/doc.tex"
|
|
|
|
|
2016-02-04 09:26:50 -05:00
|
|
|
@updateMerger.mergeUpdate @user_id, @project_id, filePath, @update, @source, =>
|
2014-09-04 08:00:41 -04:00
|
|
|
@FileTypeManager.isBinary.calledWith(filePath, @fsPath).should.equal true
|
2016-02-04 09:26:50 -05:00
|
|
|
@updateMerger.p.processDoc.calledWith(@project_id, doc_id, @user_id, @fsPath, filePath, @source).should.equal true
|
2015-04-22 10:08:29 -04:00
|
|
|
@fs.unlink.calledWith(@fsPath).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should process update as file when it is not a doc', (done)->
|
|
|
|
file_id = "1231"
|
|
|
|
@projectLocator.findElementByPath = (_, __, cb)->cb(null, {_id:file_id})
|
2014-09-04 08:00:41 -04:00
|
|
|
@FileTypeManager.isBinary.callsArgWith(2, null, true)
|
2017-11-13 07:20:14 -05:00
|
|
|
@updateMerger.p.processFile = sinon.stub().callsArgWith(6)
|
2014-02-12 05:23:40 -05:00
|
|
|
filePath = "/folder/file1.png"
|
|
|
|
|
2016-02-04 09:26:50 -05:00
|
|
|
@updateMerger.mergeUpdate @user_id, @project_id, filePath, @update, @source, =>
|
2017-11-13 07:20:14 -05:00
|
|
|
@updateMerger.p.processFile.calledWith(@project_id, file_id, @fsPath, filePath, @source, @user_id).should.equal true
|
2014-09-04 08:00:41 -04:00
|
|
|
@FileTypeManager.isBinary.calledWith(filePath, @fsPath).should.equal true
|
2015-04-22 10:08:29 -04:00
|
|
|
@fs.unlink.calledWith(@fsPath).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
|
|
|
|
describe 'processDoc :', (done)->
|
|
|
|
beforeEach ->
|
|
|
|
@doc_id = "312312klnkld"
|
|
|
|
@docLines = "\\documentclass{article}\n\\usepackage[utf8]{inputenc}\n\n\\title{42}\n\\author{Jane Doe}\n\\date{June 2011}"
|
|
|
|
@splitDocLines = @docLines.split("\n")
|
|
|
|
@fs.readFile = sinon.stub().callsArgWith(2, null, @docLines)
|
|
|
|
|
|
|
|
it 'should set the doc text in the editor controller', (done)->
|
|
|
|
@editorController.setDoc = ->
|
2016-02-04 09:26:50 -05:00
|
|
|
mock = sinon.mock(@editorController).expects("setDoc").withArgs(@project_id, @doc_id, @user_id, @splitDocLines, @source).callsArg(5)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
@update.write(@docLines)
|
|
|
|
@update.end()
|
|
|
|
|
2016-02-04 09:26:50 -05:00
|
|
|
@updateMerger.p.processDoc @project_id, @doc_id, @user_id, @update, "path", @source, ->
|
2014-02-12 05:23:40 -05:00
|
|
|
mock.verify()
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should create a new doc when it doesnt exist', (done)->
|
|
|
|
folder = {_id:"adslkjioj"}
|
|
|
|
docName = "main.tex"
|
|
|
|
path = "folder1/folder2/#{docName}"
|
2014-11-27 06:46:17 -05:00
|
|
|
@editorController.mkdirp = sinon.stub().withArgs(@project_id).callsArgWith(2, null, [folder], folder)
|
|
|
|
@editorController.addDoc = ->
|
|
|
|
mock = sinon.mock(@editorController).expects("addDoc").withArgs(@project_id, folder._id, docName, @splitDocLines, @source).callsArg(5)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
@update.write(@docLines)
|
|
|
|
@update.end()
|
|
|
|
|
2016-02-04 09:26:50 -05:00
|
|
|
@updateMerger.p.processDoc @project_id, undefined, @user_id, @update, path, @source, ->
|
2014-02-12 05:23:40 -05:00
|
|
|
mock.verify()
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'processFile :', (done)->
|
|
|
|
beforeEach ->
|
|
|
|
@file_id = "file_id_here"
|
|
|
|
@folder_id = "folder_id_here"
|
|
|
|
@path = "folder/file.png"
|
|
|
|
@folder = _id: @folder_id
|
|
|
|
@fileName = "file.png"
|
|
|
|
@fsPath = "fs/path.tex"
|
2017-11-13 07:20:14 -05:00
|
|
|
@editorController.addFile = sinon.stub().callsArg(6)
|
2017-11-24 06:32:34 -05:00
|
|
|
@editorController.replaceFile = sinon.stub().callsArg(5)
|
2014-11-27 06:46:17 -05:00
|
|
|
@editorController.deleteEntity = sinon.stub()
|
|
|
|
@editorController.mkdirp = sinon.stub().withArgs(@project_id).callsArgWith(2, null, [@folder], @folder)
|
2014-02-12 05:23:40 -05:00
|
|
|
@updateMerger.p.writeStreamToDisk = sinon.stub().withArgs(@project_id, @file_id, @update).callsArgWith(3, null, @fsPath)
|
|
|
|
|
|
|
|
it 'should replace file if the file already exists', (done)->
|
2017-11-13 07:20:14 -05:00
|
|
|
@updateMerger.p.processFile @project_id, @file_id, @fsPath, @path, @source, @user_id, =>
|
2014-11-27 06:46:17 -05:00
|
|
|
@editorController.addFile.called.should.equal false
|
2017-11-24 06:32:34 -05:00
|
|
|
@editorController.replaceFile.calledWith(@project_id, @file_id, @fsPath, @source, @user_id).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should call add file if the file does not exist', (done)->
|
2017-11-13 07:20:14 -05:00
|
|
|
@updateMerger.p.processFile @project_id, undefined, @fsPath, @path, @source, @user_id, =>
|
2014-11-27 06:46:17 -05:00
|
|
|
@editorController.mkdirp.calledWith(@project_id, "folder/").should.equal true
|
2017-11-13 07:20:14 -05:00
|
|
|
@editorController.addFile.calledWith(@project_id, @folder_id, @fileName, @fsPath, @source, @user_id).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
@editorController.replaceFile.called.should.equal false
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'delete entity :', (done)->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@path = "folder/doc1"
|
2016-09-19 09:32:58 -04:00
|
|
|
@type = "mock-type"
|
2014-11-27 06:46:17 -05:00
|
|
|
@editorController.deleteEntity = ->
|
2014-02-12 05:23:40 -05:00
|
|
|
@entity_id = "entity_id_here"
|
|
|
|
@entity = _id:@entity_id
|
2016-09-19 09:32:58 -04:00
|
|
|
@projectLocator.findElementByPath = (project_id, path, cb)=> cb(null, @entity, @type)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
it 'should get the element id', ->
|
|
|
|
@projectLocator.findElementByPath = sinon.spy()
|
2014-10-15 10:18:31 -04:00
|
|
|
@updateMerger.deleteUpdate @project_id, @path, @source, ->
|
2014-02-12 05:23:40 -05:00
|
|
|
@projectLocator.findElementByPath.calledWith(@project_id, @path).should.equal true
|
|
|
|
|
2016-09-19 09:32:58 -04:00
|
|
|
it 'should delete the entity in the editor controller with the correct type', (done)->
|
2014-02-12 05:23:40 -05:00
|
|
|
@entity.lines = []
|
2016-09-19 09:32:58 -04:00
|
|
|
mock = sinon.mock(@editorController).expects("deleteEntity").withArgs(@project_id, @entity_id, @type, @source).callsArg(4)
|
2014-10-15 10:18:31 -04:00
|
|
|
@updateMerger.deleteUpdate @project_id, @path, @source, ->
|
2014-02-12 05:23:40 -05:00
|
|
|
mock.verify()
|
|
|
|
done()
|
|
|
|
|
|
|
|
|