| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- const SandboxedModule = require('sandboxed-module')
- const sinon = require('sinon')
- const modulePath = require('path').join(
- __dirname,
- '../../../../app/src/Features/ThirdPartyDataStore/UpdateMerger.js'
- )
- const BufferedStream = require('bufferedstream')
- describe('UpdateMerger :', function () {
- beforeEach(function () {
- this.updateMerger = SandboxedModule.require(modulePath, {
- requires: {
- fs: (this.fs = { unlink: sinon.stub().callsArgWith(1) }),
- '../Editor/EditorController': (this.EditorController = {}),
- '../Uploads/FileTypeManager': (this.FileTypeManager = {}),
- '../../infrastructure/FileWriter': (this.FileWriter = {}),
- '../Project/ProjectEntityHandler': (this.ProjectEntityHandler = {}),
- '@overleaf/settings': { path: { dumpPath: 'dump_here' } },
- },
- })
- this.project_id = 'project_id_here'
- this.user_id = 'mock-user-id'
- this.docPath = this.newDocPath = '/folder/doc.tex'
- this.filePath = this.newFilePath = '/folder/file.png'
- this.existingDocPath = '/folder/other.tex'
- this.existingFilePath = '/folder/fig1.pdf'
- this.linkedFileData = { provider: 'url' }
- this.existingDocs = [{ path: '/main.tex' }, { path: '/folder/other.tex' }]
- this.existingFiles = [{ path: '/figure.pdf' }, { path: '/folder/fig1.pdf' }]
- this.ProjectEntityHandler.getAllEntities = sinon
- .stub()
- .callsArgWith(1, null, this.existingDocs, this.existingFiles)
- this.fsPath = '/tmp/file/path'
- this.source = 'dropbox'
- this.updateRequest = new BufferedStream()
- this.FileWriter.writeStreamToDisk = sinon.stub().yields(null, this.fsPath)
- this.callback = sinon.stub()
- })
- describe('mergeUpdate', function () {
- describe('doc updates for a new doc', function () {
- beforeEach(function () {
- this.FileTypeManager.getType = sinon
- .stub()
- .yields(null, { binary: false, encoding: 'utf-8' })
- this.updateMerger.p.processDoc = sinon.stub().yields()
- this.updateMerger.mergeUpdate(
- this.user_id,
- this.project_id,
- this.docPath,
- this.updateRequest,
- this.source,
- this.callback
- )
- })
- it('should look at the file contents', function () {
- this.FileTypeManager.getType.called.should.equal(true)
- })
- it('should process update as doc', function () {
- this.updateMerger.p.processDoc
- .calledWith(
- this.project_id,
- this.user_id,
- this.fsPath,
- this.docPath,
- this.source
- )
- .should.equal(true)
- })
- it('removes the temp file from disk', function () {
- this.fs.unlink.calledWith(this.fsPath).should.equal(true)
- })
- })
- describe('file updates for a new file ', function () {
- beforeEach(function () {
- this.FileTypeManager.getType = sinon
- .stub()
- .yields(null, { binary: true })
- this.updateMerger.p.processFile = sinon.stub().yields()
- this.updateMerger.mergeUpdate(
- this.user_id,
- this.project_id,
- this.filePath,
- this.updateRequest,
- this.source,
- this.callback
- )
- })
- it('should look at the file contents', function () {
- this.FileTypeManager.getType.called.should.equal(true)
- })
- it('should process update as file', function () {
- this.updateMerger.p.processFile
- .calledWith(
- this.project_id,
- this.fsPath,
- this.filePath,
- this.source,
- this.user_id
- )
- .should.equal(true)
- })
- it('removes the temp file from disk', function () {
- this.fs.unlink.calledWith(this.fsPath).should.equal(true)
- })
- })
- describe('doc updates for an existing doc', function () {
- beforeEach(function () {
- this.FileTypeManager.getType = sinon
- .stub()
- .yields(null, { binary: false, encoding: 'utf-8' })
- this.updateMerger.p.processDoc = sinon.stub().yields()
- this.updateMerger.mergeUpdate(
- this.user_id,
- this.project_id,
- this.existingDocPath,
- this.updateRequest,
- this.source,
- this.callback
- )
- })
- it('should look at the file contents', function () {
- this.FileTypeManager.getType.called.should.equal(true)
- })
- it('should process update as doc', function () {
- this.updateMerger.p.processDoc
- .calledWith(
- this.project_id,
- this.user_id,
- this.fsPath,
- this.existingDocPath,
- this.source
- )
- .should.equal(true)
- })
- it('removes the temp file from disk', function () {
- this.fs.unlink.calledWith(this.fsPath).should.equal(true)
- })
- })
- describe('file updates for an existing file', function () {
- beforeEach(function () {
- this.FileTypeManager.getType = sinon
- .stub()
- .yields(null, { binary: true })
- this.updateMerger.p.processFile = sinon.stub().yields()
- this.updateMerger.mergeUpdate(
- this.user_id,
- this.project_id,
- this.existingFilePath,
- this.updateRequest,
- this.source,
- this.callback
- )
- })
- it('should look at the file contents', function () {
- this.FileTypeManager.getType.called.should.equal(true)
- })
- it('should process update as file', function () {
- this.updateMerger.p.processFile
- .calledWith(
- this.project_id,
- this.fsPath,
- this.existingFilePath,
- this.source,
- this.user_id
- )
- .should.equal(true)
- })
- it('removes the temp file from disk', function () {
- this.fs.unlink.calledWith(this.fsPath).should.equal(true)
- })
- })
- })
- describe('file updates for an existing doc', function () {
- beforeEach(function () {
- this.FileTypeManager.getType = sinon.stub().yields(null, { binary: true })
- this.updateMerger.deleteUpdate = sinon.stub().yields()
- this.updateMerger.p.processFile = sinon.stub().yields()
- this.updateMerger.mergeUpdate(
- this.user_id,
- this.project_id,
- this.existingDocPath,
- this.updateRequest,
- this.source,
- this.callback
- )
- })
- it('should look at the file contents', function () {
- this.FileTypeManager.getType.called.should.equal(true)
- })
- it('should delete the existing doc', function () {
- this.updateMerger.deleteUpdate
- .calledWith(
- this.user_id,
- this.project_id,
- this.existingDocPath,
- this.source
- )
- .should.equal(true)
- })
- it('should process update as file', function () {
- this.updateMerger.p.processFile
- .calledWith(
- this.project_id,
- this.fsPath,
- this.existingDocPath,
- this.source,
- this.user_id
- )
- .should.equal(true)
- })
- it('removes the temp file from disk', function () {
- this.fs.unlink.calledWith(this.fsPath).should.equal(true)
- })
- })
- describe('doc updates for an existing file', function () {
- beforeEach(function () {
- this.FileTypeManager.getType = sinon.stub().yields(null, { binary: true })
- this.updateMerger.deleteUpdate = sinon.stub().yields()
- this.updateMerger.p.processFile = sinon.stub().yields()
- this.updateMerger.mergeUpdate(
- this.user_id,
- this.project_id,
- this.existingFilePath,
- this.updateRequest,
- this.source,
- this.callback
- )
- })
- it('should look at the file contents', function () {
- this.FileTypeManager.getType.called.should.equal(true)
- })
- it('should not delete the existing file', function () {
- this.updateMerger.deleteUpdate.called.should.equal(false)
- })
- it('should process update as file', function () {
- this.updateMerger.p.processFile
- .calledWith(
- this.project_id,
- this.fsPath,
- this.existingFilePath,
- this.source,
- this.user_id
- )
- .should.equal(true)
- })
- it('removes the temp file from disk', function () {
- this.fs.unlink.calledWith(this.fsPath).should.equal(true)
- })
- })
- describe('deleteUpdate', function () {
- beforeEach(function () {
- this.EditorController.deleteEntityWithPath = sinon.stub().yields()
- this.updateMerger.deleteUpdate(
- this.user_id,
- this.project_id,
- this.docPath,
- this.source,
- this.callback
- )
- })
- it('should delete the entity in the editor controller', function () {
- this.EditorController.deleteEntityWithPath
- .calledWith(this.project_id, this.docPath, this.source, this.user_id)
- .should.equal(true)
- })
- })
- describe('private methods', function () {
- describe('processDoc', function () {
- beforeEach(function () {
- this.docLines =
- '\\documentclass{article}\n\\usepackage[utf8]{inputenc}\n\n\\title{42}\n\\author{Jane Doe}\n\\date{June 2011}'
- this.updateMerger.p.readFileIntoTextArray = sinon
- .stub()
- .yields(null, this.docLines)
- this.EditorController.upsertDocWithPath = sinon.stub().yields()
- this.updateMerger.p.processDoc(
- this.project_id,
- this.user_id,
- this.fsPath,
- this.docPath,
- this.source,
- this.callback
- )
- })
- it('reads the temp file from disk', function () {
- this.updateMerger.p.readFileIntoTextArray
- .calledWith(this.fsPath)
- .should.equal(true)
- })
- it('should upsert the doc in the editor controller', function () {
- this.EditorController.upsertDocWithPath
- .calledWith(
- this.project_id,
- this.docPath,
- this.docLines,
- this.source,
- this.user_id
- )
- .should.equal(true)
- })
- })
- describe('processFile', function () {
- beforeEach(function () {
- this.EditorController.upsertFileWithPath = sinon.stub().yields()
- this.updateMerger.p.processFile(
- this.project_id,
- this.fsPath,
- this.filePath,
- this.source,
- this.user_id,
- this.callback
- )
- })
- it('should upsert the file in the editor controller', function () {
- this.EditorController.upsertFileWithPath
- .calledWith(
- this.project_id,
- this.filePath,
- this.fsPath,
- null,
- this.source,
- this.user_id
- )
- .should.equal(true)
- })
- })
- })
- })
|