2019-12-16 06:20:29 -05:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const chai = require('chai')
|
|
|
|
const { expect } = chai
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2014-02-25 10:52:30 -05:00
|
|
|
|
2020-01-02 11:37:47 -05:00
|
|
|
const modulePath = '../../../app/js/PersistorManager.js'
|
2014-02-25 10:52:30 -05:00
|
|
|
|
2020-01-02 11:37:47 -05:00
|
|
|
describe('PersistorManager', function() {
|
2020-02-17 09:04:42 -05:00
|
|
|
let PersistorManager, FSPersistor, S3Persistor, settings, requires
|
2014-02-25 10:52:30 -05:00
|
|
|
|
2020-01-02 11:37:47 -05:00
|
|
|
beforeEach(function() {
|
2020-02-17 09:04:42 -05:00
|
|
|
FSPersistor = {
|
|
|
|
wrappedMethod: sinon.stub().returns('FSPersistor')
|
2020-01-02 11:37:47 -05:00
|
|
|
}
|
2020-02-17 09:04:42 -05:00
|
|
|
S3Persistor = {
|
|
|
|
wrappedMethod: sinon.stub().returns('S3Persistor')
|
2020-01-02 11:37:47 -05:00
|
|
|
}
|
2014-02-25 10:52:30 -05:00
|
|
|
|
2020-01-02 11:37:47 -05:00
|
|
|
settings = {
|
|
|
|
filestore: {}
|
|
|
|
}
|
2014-02-25 10:52:30 -05:00
|
|
|
|
2020-01-02 11:37:47 -05:00
|
|
|
requires = {
|
2020-02-17 09:04:42 -05:00
|
|
|
'./S3Persistor': S3Persistor,
|
|
|
|
'./FSPersistor': FSPersistor,
|
2020-01-02 11:37:47 -05:00
|
|
|
'settings-sharelatex': settings,
|
|
|
|
'logger-sharelatex': {
|
|
|
|
log() {},
|
|
|
|
err() {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2014-02-25 10:52:30 -05:00
|
|
|
|
2020-01-02 11:37:47 -05: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 10:52:30 -05:00
|
|
|
|
2020-01-04 12:00:04 -05:00
|
|
|
expect(PersistorManager).to.respondTo('wrappedMethod')
|
2020-02-17 09:04:42 -05:00
|
|
|
expect(PersistorManager.wrappedMethod()).to.equal('S3Persistor')
|
2020-01-04 12:00:04 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should implement the S3 wrapped method when 'aws-sdk' is configured", function() {
|
|
|
|
settings.filestore.backend = 'aws-sdk'
|
|
|
|
PersistorManager = SandboxedModule.require(modulePath, { requires })
|
|
|
|
|
2020-01-02 11:37:47 -05:00
|
|
|
expect(PersistorManager).to.respondTo('wrappedMethod')
|
2020-02-17 09:04:42 -05:00
|
|
|
expect(PersistorManager.wrappedMethod()).to.equal('S3Persistor')
|
2020-01-02 11:37:47 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should implement the FS wrapped method when FS is configured', function() {
|
|
|
|
settings.filestore.backend = 'fs'
|
|
|
|
PersistorManager = SandboxedModule.require(modulePath, { requires })
|
2014-02-25 10:52:30 -05:00
|
|
|
|
2020-01-02 11:37:47 -05:00
|
|
|
expect(PersistorManager).to.respondTo('wrappedMethod')
|
2020-02-17 09:04:42 -05:00
|
|
|
expect(PersistorManager.wrappedMethod()).to.equal('FSPersistor')
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
2014-02-25 10:52:30 -05:00
|
|
|
|
2020-01-02 11:37:47 -05: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 10:52:30 -05:00
|
|
|
|
2020-01-02 11:37:47 -05: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 06:20:29 -05:00
|
|
|
})
|