|
|
@@ -1,18 +1,4 @@
|
|
|
-/* eslint-disable
|
|
|
- max-len,
|
|
|
- no-return-assign,
|
|
|
- no-unused-vars,
|
|
|
-*/
|
|
|
-// TODO: This file was created by bulk-decaffeinate.
|
|
|
-// Fix any style issues and re-enable lint.
|
|
|
-/*
|
|
|
- * decaffeinate suggestions:
|
|
|
- * DS102: Remove unnecessary code created because of implicit returns
|
|
|
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
- */
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
-const assert = require('assert')
|
|
|
-const { expect } = require('chai')
|
|
|
const sinon = require('sinon')
|
|
|
const modulePath = require('path').join(
|
|
|
__dirname,
|
|
|
@@ -21,6 +7,7 @@ const modulePath = require('path').join(
|
|
|
const Errors = require('../../../../app/src/Features/Errors/Errors')
|
|
|
const tk = require('timekeeper')
|
|
|
const moment = require('moment')
|
|
|
+const { expect } = require('chai')
|
|
|
|
|
|
describe('RestoreManager', function () {
|
|
|
beforeEach(function () {
|
|
|
@@ -28,62 +15,62 @@ describe('RestoreManager', function () {
|
|
|
this.RestoreManager = SandboxedModule.require(modulePath, {
|
|
|
requires: {
|
|
|
'@overleaf/settings': {},
|
|
|
- '../../infrastructure/FileWriter': (this.FileWriter = {}),
|
|
|
- '../Uploads/FileSystemImportManager': (this.FileSystemImportManager =
|
|
|
- {}),
|
|
|
- '../Project/ProjectEntityHandler': (this.ProjectEntityHandler = {}),
|
|
|
- '../Editor/EditorController': (this.EditorController = {}),
|
|
|
+ '../../infrastructure/FileWriter': (this.FileWriter = { promises: {} }),
|
|
|
+ '../Uploads/FileSystemImportManager': (this.FileSystemImportManager = {
|
|
|
+ promises: {},
|
|
|
+ }),
|
|
|
+ '../Editor/EditorController': (this.EditorController = {
|
|
|
+ promises: {},
|
|
|
+ }),
|
|
|
},
|
|
|
})
|
|
|
this.user_id = 'mock-user-id'
|
|
|
this.project_id = 'mock-project-id'
|
|
|
this.version = 42
|
|
|
- return (this.callback = sinon.stub())
|
|
|
})
|
|
|
|
|
|
afterEach(function () {
|
|
|
- return tk.reset()
|
|
|
+ tk.reset()
|
|
|
})
|
|
|
|
|
|
describe('restoreFileFromV2', function () {
|
|
|
beforeEach(function () {
|
|
|
- this.RestoreManager._writeFileVersionToDisk = sinon
|
|
|
+ this.RestoreManager.promises._writeFileVersionToDisk = sinon
|
|
|
.stub()
|
|
|
- .yields(null, (this.fsPath = '/tmp/path/on/disk'))
|
|
|
- this.RestoreManager._findOrCreateFolder = sinon
|
|
|
+ .resolves((this.fsPath = '/tmp/path/on/disk'))
|
|
|
+ this.RestoreManager.promises._findOrCreateFolder = sinon
|
|
|
.stub()
|
|
|
- .yields(null, (this.folder_id = 'mock-folder-id'))
|
|
|
- return (this.FileSystemImportManager.addEntity = sinon
|
|
|
+ .resolves((this.folder_id = 'mock-folder-id'))
|
|
|
+ this.FileSystemImportManager.promises.addEntity = sinon
|
|
|
.stub()
|
|
|
- .yields(null, (this.entity = 'mock-entity')))
|
|
|
+ .resolves((this.entity = 'mock-entity'))
|
|
|
})
|
|
|
|
|
|
describe('with a file not in a folder', function () {
|
|
|
- beforeEach(function () {
|
|
|
+ beforeEach(async function () {
|
|
|
this.pathname = 'foo.tex'
|
|
|
- return this.RestoreManager.restoreFileFromV2(
|
|
|
+ this.result = await this.RestoreManager.promises.restoreFileFromV2(
|
|
|
this.user_id,
|
|
|
this.project_id,
|
|
|
this.version,
|
|
|
- this.pathname,
|
|
|
- this.callback
|
|
|
+ this.pathname
|
|
|
)
|
|
|
})
|
|
|
|
|
|
it('should write the file version to disk', function () {
|
|
|
- return this.RestoreManager._writeFileVersionToDisk
|
|
|
+ this.RestoreManager.promises._writeFileVersionToDisk
|
|
|
.calledWith(this.project_id, this.version, this.pathname)
|
|
|
.should.equal(true)
|
|
|
})
|
|
|
|
|
|
it('should find the root folder', function () {
|
|
|
- return this.RestoreManager._findOrCreateFolder
|
|
|
+ this.RestoreManager.promises._findOrCreateFolder
|
|
|
.calledWith(this.project_id, '')
|
|
|
.should.equal(true)
|
|
|
})
|
|
|
|
|
|
it('should add the entity', function () {
|
|
|
- return this.FileSystemImportManager.addEntity
|
|
|
+ this.FileSystemImportManager.promises.addEntity
|
|
|
.calledWith(
|
|
|
this.user_id,
|
|
|
this.project_id,
|
|
|
@@ -95,31 +82,30 @@ describe('RestoreManager', function () {
|
|
|
.should.equal(true)
|
|
|
})
|
|
|
|
|
|
- it('should call the callback with the entity', function () {
|
|
|
- return this.callback.calledWith(null, this.entity).should.equal(true)
|
|
|
+ it('should return the entity', function () {
|
|
|
+ expect(this.result).to.equal(this.entity)
|
|
|
})
|
|
|
})
|
|
|
|
|
|
describe('with a file in a folder', function () {
|
|
|
- beforeEach(function () {
|
|
|
+ beforeEach(async function () {
|
|
|
this.pathname = 'foo/bar.tex'
|
|
|
- return this.RestoreManager.restoreFileFromV2(
|
|
|
+ await this.RestoreManager.promises.restoreFileFromV2(
|
|
|
this.user_id,
|
|
|
this.project_id,
|
|
|
this.version,
|
|
|
- this.pathname,
|
|
|
- this.callback
|
|
|
+ this.pathname
|
|
|
)
|
|
|
})
|
|
|
|
|
|
it('should find the folder', function () {
|
|
|
- return this.RestoreManager._findOrCreateFolder
|
|
|
+ this.RestoreManager.promises._findOrCreateFolder
|
|
|
.calledWith(this.project_id, 'foo')
|
|
|
.should.equal(true)
|
|
|
})
|
|
|
|
|
|
it('should add the entity by its basename', function () {
|
|
|
- return this.FileSystemImportManager.addEntity
|
|
|
+ this.FileSystemImportManager.promises.addEntity
|
|
|
.calledWith(
|
|
|
this.user_id,
|
|
|
this.project_id,
|
|
|
@@ -134,81 +120,79 @@ describe('RestoreManager', function () {
|
|
|
})
|
|
|
|
|
|
describe('_findOrCreateFolder', function () {
|
|
|
- beforeEach(function () {
|
|
|
- this.EditorController.mkdirp = sinon
|
|
|
- .stub()
|
|
|
- .yields(null, [], { _id: (this.folder_id = 'mock-folder-id') })
|
|
|
- return this.RestoreManager._findOrCreateFolder(
|
|
|
+ beforeEach(async function () {
|
|
|
+ this.EditorController.promises.mkdirp = sinon.stub().resolves({
|
|
|
+ newFolders: [],
|
|
|
+ lastFolder: { _id: (this.folder_id = 'mock-folder-id') },
|
|
|
+ })
|
|
|
+ this.result = await this.RestoreManager.promises._findOrCreateFolder(
|
|
|
this.project_id,
|
|
|
- 'folder/name',
|
|
|
- this.callback
|
|
|
+ 'folder/name'
|
|
|
)
|
|
|
})
|
|
|
|
|
|
it('should look up or create the folder', function () {
|
|
|
- return this.EditorController.mkdirp
|
|
|
+ this.EditorController.promises.mkdirp
|
|
|
.calledWith(this.project_id, 'folder/name')
|
|
|
.should.equal(true)
|
|
|
})
|
|
|
|
|
|
it('should return the folder_id', function () {
|
|
|
- return this.callback.calledWith(null, this.folder_id).should.equal(true)
|
|
|
+ expect(this.result).to.equal(this.folder_id)
|
|
|
})
|
|
|
})
|
|
|
|
|
|
describe('_addEntityWithUniqueName', function () {
|
|
|
beforeEach(function () {
|
|
|
this.addEntityWithName = sinon.stub()
|
|
|
- return (this.name = 'foo.tex')
|
|
|
+ this.name = 'foo.tex'
|
|
|
})
|
|
|
|
|
|
describe('with a valid name', function () {
|
|
|
- beforeEach(function () {
|
|
|
- this.addEntityWithName.yields(null, (this.entity = 'mock-entity'))
|
|
|
- return this.RestoreManager._addEntityWithUniqueName(
|
|
|
- this.addEntityWithName,
|
|
|
- this.name,
|
|
|
- this.callback
|
|
|
- )
|
|
|
+ beforeEach(async function () {
|
|
|
+ this.addEntityWithName.resolves((this.entity = 'mock-entity'))
|
|
|
+ this.result =
|
|
|
+ await this.RestoreManager.promises._addEntityWithUniqueName(
|
|
|
+ this.addEntityWithName,
|
|
|
+ this.name
|
|
|
+ )
|
|
|
})
|
|
|
|
|
|
it('should add the entity', function () {
|
|
|
- return this.addEntityWithName.calledWith(this.name).should.equal(true)
|
|
|
+ this.addEntityWithName.calledWith(this.name).should.equal(true)
|
|
|
})
|
|
|
|
|
|
it('should return the entity', function () {
|
|
|
- return this.callback.calledWith(null, this.entity).should.equal(true)
|
|
|
+ expect(this.result).to.equal(this.entity)
|
|
|
})
|
|
|
})
|
|
|
|
|
|
describe('with an invalid name', function () {
|
|
|
- beforeEach(function () {
|
|
|
- this.addEntityWithName
|
|
|
- .onFirstCall()
|
|
|
- .yields(new Errors.InvalidNameError())
|
|
|
+ beforeEach(async function () {
|
|
|
+ this.addEntityWithName.rejects(new Errors.InvalidNameError())
|
|
|
this.addEntityWithName
|
|
|
.onSecondCall()
|
|
|
- .yields(null, (this.entity = 'mock-entity'))
|
|
|
- return this.RestoreManager._addEntityWithUniqueName(
|
|
|
- this.addEntityWithName,
|
|
|
- this.name,
|
|
|
- this.callback
|
|
|
- )
|
|
|
+ .resolves((this.entity = 'mock-entity'))
|
|
|
+ this.result =
|
|
|
+ await this.RestoreManager.promises._addEntityWithUniqueName(
|
|
|
+ this.addEntityWithName,
|
|
|
+ this.name
|
|
|
+ )
|
|
|
})
|
|
|
|
|
|
it('should try to add the entity with its original name', function () {
|
|
|
- return this.addEntityWithName.calledWith('foo.tex').should.equal(true)
|
|
|
+ this.addEntityWithName.calledWith('foo.tex').should.equal(true)
|
|
|
})
|
|
|
|
|
|
it('should try to add the entity with a unique name', function () {
|
|
|
const date = moment(new Date()).format('Do MMM YY H:mm:ss')
|
|
|
- return this.addEntityWithName
|
|
|
+ this.addEntityWithName
|
|
|
.calledWith(`foo (Restored on ${date}).tex`)
|
|
|
.should.equal(true)
|
|
|
})
|
|
|
|
|
|
it('should return the entity', function () {
|
|
|
- return this.callback.calledWith(null, this.entity).should.equal(true)
|
|
|
+ expect(this.result).to.equal(this.entity)
|
|
|
})
|
|
|
})
|
|
|
})
|