2019-12-16 11:20:29 +00:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const chai = require('chai')
|
|
|
|
const { expect } = chai
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2014-02-25 15:52:30 +00:00
|
|
|
|
2020-01-02 16:37:47 +00:00
|
|
|
const modulePath = '../../../app/js/PersistorManager.js'
|
2014-02-25 15:52:30 +00:00
|
|
|
|
2020-01-02 16:37:47 +00:00
|
|
|
describe('PersistorManager', function() {
|
|
|
|
let PersistorManager,
|
|
|
|
FSPersistorManager,
|
|
|
|
S3PersistorManager,
|
|
|
|
settings,
|
|
|
|
requires
|
2014-02-25 15:52:30 +00:00
|
|
|
|
2020-01-02 16:37:47 +00:00
|
|
|
beforeEach(function() {
|
|
|
|
FSPersistorManager = {
|
|
|
|
wrappedMethod: sinon.stub().returns('FSPersistorManager')
|
|
|
|
}
|
|
|
|
S3PersistorManager = {
|
|
|
|
wrappedMethod: sinon.stub().returns('S3PersistorManager')
|
|
|
|
}
|
2014-02-25 15:52:30 +00:00
|
|
|
|
2020-01-02 16:37:47 +00:00
|
|
|
settings = {
|
|
|
|
filestore: {}
|
|
|
|
}
|
2014-02-25 15:52:30 +00:00
|
|
|
|
2020-01-02 16:37:47 +00:00
|
|
|
requires = {
|
|
|
|
'./S3PersistorManager': S3PersistorManager,
|
|
|
|
'./FSPersistorManager': FSPersistorManager,
|
|
|
|
'settings-sharelatex': settings,
|
|
|
|
'logger-sharelatex': {
|
|
|
|
log() {},
|
|
|
|
err() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2014-02-25 15:52:30 +00:00
|
|
|
|
2020-01-02 16:37:47 +00:00
|
|
|
it('should implement the S3 wrapped method when S3 is configured', function() {
|
|
|
|
settings.filestore.backend = 's3'
|
|
|
|
PersistorManager = SandboxedModule.require(modulePath, { requires })
|
2014-02-25 15:52:30 +00:00
|
|
|
|
2020-01-02 16:37:47 +00:00
|
|
|
expect(PersistorManager).to.respondTo('wrappedMethod')
|
|
|
|
expect(PersistorManager.wrappedMethod()).to.equal('S3PersistorManager')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should implement the FS wrapped method when FS is configured', function() {
|
|
|
|
settings.filestore.backend = 'fs'
|
|
|
|
PersistorManager = SandboxedModule.require(modulePath, { requires })
|
2014-02-25 15:52:30 +00:00
|
|
|
|
2020-01-02 16:37:47 +00:00
|
|
|
expect(PersistorManager).to.respondTo('wrappedMethod')
|
|
|
|
expect(PersistorManager.wrappedMethod()).to.equal('FSPersistorManager')
|
2019-12-16 11:20:29 +00:00
|
|
|
})
|
2014-02-25 15:52:30 +00:00
|
|
|
|
2020-01-02 16:37:47 +00:00
|
|
|
it('should throw an error when the backend is not configured', function() {
|
|
|
|
try {
|
|
|
|
SandboxedModule.require(modulePath, { requires })
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).to.equal('no backend specified - config incomplete')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
expect('should have caught an error').not.to.exist
|
|
|
|
})
|
2014-02-25 15:52:30 +00:00
|
|
|
|
2020-01-02 16:37:47 +00:00
|
|
|
it('should throw an error when the backend is unknown', function() {
|
|
|
|
settings.filestore.backend = 'magic'
|
|
|
|
try {
|
|
|
|
SandboxedModule.require(modulePath, { requires })
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).to.equal('unknown filestore backend: magic')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
expect('should have caught an error').not.to.exist
|
|
|
|
})
|
2019-12-16 11:20:29 +00:00
|
|
|
})
|