overleaf/libraries/object-persistor/test/unit/PersistorFactoryTests.js
Simon Detheridge e92b75a2f8 Create new module from overleaf/filestore persistors (#1)
* Create new module from overleaf/filestore persistors

* Convert persistors to ES6 classes with local settings

* Update README.md

Co-authored-by: Eric Mc Sween <eric.mcsween@overleaf.com>

* Update README.md

Co-authored-by: Eric Mc Sween <eric.mcsween@overleaf.com>

* Update .gitignore

Co-authored-by: Eric Mc Sween <eric.mcsween@overleaf.com>

* Switch to AGPL license

* Paginate S3 list-object results

* Remove S3 client caching

* Clean up S3 md5-verification mechanism

* Update README for recent changes

* Update README.md

Co-authored-by: Eric Mc Sween <eric.mcsween@overleaf.com>

* Remove package-lock

* Remove comment about FileHandler

* Add directory marker to FSPersistor.deleteDirectory

* Don't copy opts in GcsPersistor.getObjectStream

* Use Date.now instead of getTime

* Catch errors in migration persistor

* Check that settings.buckets exists

* Don't mutate options in ObserverStream constructor

* Update src/PersistorHelper.js

Co-authored-by: Eric Mc Sween <eric.mcsween@overleaf.com>

* Lint and format fixes

Co-authored-by: Eric Mc Sween <eric.mcsween@overleaf.com>
2020-07-02 14:19:45 +01:00

82 lines
2.3 KiB
JavaScript

const chai = require('chai')
const { expect } = chai
const SandboxedModule = require('sandboxed-module')
const modulePath = '../../src/PersistorFactory.js'
describe('PersistorManager', function () {
let PersistorFactory, FSPersistor, S3Persistor, Settings, GcsPersistor
beforeEach(function () {
FSPersistor = class {
wrappedMethod() {
return 'FSPersistor'
}
}
S3Persistor = class {
wrappedMethod() {
return 'S3Persistor'
}
}
GcsPersistor = class {
wrappedMethod() {
return 'GcsPersistor'
}
}
Settings = {}
const requires = {
'./GcsPersistor': GcsPersistor,
'./S3Persistor': S3Persistor,
'./FSPersistor': FSPersistor,
'logger-sharelatex': {
info() {},
err() {}
}
}
PersistorFactory = SandboxedModule.require(modulePath, { requires })
})
it('should implement the S3 wrapped method when S3 is configured', function () {
Settings.backend = 's3'
expect(PersistorFactory(Settings)).to.respondTo('wrappedMethod')
expect(PersistorFactory(Settings).wrappedMethod()).to.equal('S3Persistor')
})
it("should implement the S3 wrapped method when 'aws-sdk' is configured", function () {
Settings.backend = 'aws-sdk'
expect(PersistorFactory(Settings)).to.respondTo('wrappedMethod')
expect(PersistorFactory(Settings).wrappedMethod()).to.equal('S3Persistor')
})
it('should implement the FS wrapped method when FS is configured', function () {
Settings.backend = 'fs'
expect(PersistorFactory(Settings)).to.respondTo('wrappedMethod')
expect(PersistorFactory(Settings).wrappedMethod()).to.equal('FSPersistor')
})
it('should throw an error when the backend is not configured', function () {
try {
PersistorFactory(Settings)
} catch (err) {
expect(err.message).to.equal('no backend specified - config incomplete')
return
}
expect('should have caught an error').not.to.exist
})
it('should throw an error when the backend is unknown', function () {
Settings.backend = 'magic'
try {
PersistorFactory(Settings)
} catch (err) {
expect(err.message).to.equal('unknown backend')
expect(err.info.backend).to.equal('magic')
return
}
expect('should have caught an error').not.to.exist
})
})