2020-07-02 09:19:45 -04:00
|
|
|
const chai = require('chai')
|
|
|
|
const { expect } = chai
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2024-11-08 05:21:56 -05:00
|
|
|
const StreamPromises = require('node:stream/promises')
|
2020-07-02 09:19:45 -04:00
|
|
|
|
2022-11-10 07:06:08 -05:00
|
|
|
const MODULE_PATH = '../../src/PersistorFactory.js'
|
2020-07-02 09:19:45 -04:00
|
|
|
|
|
|
|
describe('PersistorManager', function () {
|
|
|
|
let PersistorFactory, FSPersistor, S3Persistor, Settings, GcsPersistor
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
FSPersistor = class {
|
2023-02-13 09:35:17 -05:00
|
|
|
constructor(settings) {
|
|
|
|
this.settings = settings
|
|
|
|
}
|
|
|
|
|
2020-07-02 09:19:45 -04:00
|
|
|
wrappedMethod() {
|
|
|
|
return 'FSPersistor'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
S3Persistor = class {
|
|
|
|
wrappedMethod() {
|
|
|
|
return 'S3Persistor'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GcsPersistor = class {
|
|
|
|
wrappedMethod() {
|
|
|
|
return 'GcsPersistor'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings = {}
|
|
|
|
const requires = {
|
|
|
|
'./GcsPersistor': GcsPersistor,
|
2024-11-08 03:31:26 -05:00
|
|
|
'./S3Persistor': { S3Persistor },
|
2020-07-02 09:19:45 -04:00
|
|
|
'./FSPersistor': FSPersistor,
|
2022-05-16 10:25:37 -04:00
|
|
|
'@overleaf/logger': {
|
2020-07-02 09:19:45 -04:00
|
|
|
info() {},
|
2021-12-16 04:04:32 -05:00
|
|
|
err() {},
|
|
|
|
},
|
2022-11-10 07:06:08 -05:00
|
|
|
'stream/promises': StreamPromises,
|
2020-07-02 09:19:45 -04:00
|
|
|
}
|
2022-11-10 07:06:08 -05:00
|
|
|
PersistorFactory = SandboxedModule.require(MODULE_PATH, { requires })
|
2020-07-02 09:19:45 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
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')
|
|
|
|
})
|
|
|
|
|
2023-02-13 09:35:17 -05:00
|
|
|
it('should forward useSubdirectories=true to FSPersistor', function () {
|
|
|
|
Settings.backend = 'fs'
|
|
|
|
Settings.useSubdirectories = true
|
|
|
|
|
|
|
|
expect(PersistorFactory(Settings).settings.useSubdirectories).to.be.true
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should forward useSubdirectories=false to FSPersistor', function () {
|
|
|
|
Settings.backend = 'fs'
|
|
|
|
Settings.useSubdirectories = false
|
|
|
|
|
|
|
|
expect(PersistorFactory(Settings).settings.useSubdirectories).to.be.false
|
|
|
|
})
|
|
|
|
|
2020-07-02 09:19:45 -04:00
|
|
|
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
|
|
|
|
})
|
|
|
|
})
|