PersistorFactoryTests.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const chai = require('chai')
  2. const { expect } = chai
  3. const SandboxedModule = require('sandboxed-module')
  4. const StreamPromises = require('stream/promises')
  5. const MODULE_PATH = '../../src/PersistorFactory.js'
  6. describe('PersistorManager', function () {
  7. let PersistorFactory, FSPersistor, S3Persistor, Settings, GcsPersistor
  8. beforeEach(function () {
  9. FSPersistor = class {
  10. constructor(settings) {
  11. this.settings = settings
  12. }
  13. wrappedMethod() {
  14. return 'FSPersistor'
  15. }
  16. }
  17. S3Persistor = class {
  18. wrappedMethod() {
  19. return 'S3Persistor'
  20. }
  21. }
  22. GcsPersistor = class {
  23. wrappedMethod() {
  24. return 'GcsPersistor'
  25. }
  26. }
  27. Settings = {}
  28. const requires = {
  29. './GcsPersistor': GcsPersistor,
  30. './S3Persistor': { S3Persistor },
  31. './FSPersistor': FSPersistor,
  32. '@overleaf/logger': {
  33. info() {},
  34. err() {},
  35. },
  36. 'stream/promises': StreamPromises,
  37. }
  38. PersistorFactory = SandboxedModule.require(MODULE_PATH, { requires })
  39. })
  40. it('should implement the S3 wrapped method when S3 is configured', function () {
  41. Settings.backend = 's3'
  42. expect(PersistorFactory(Settings)).to.respondTo('wrappedMethod')
  43. expect(PersistorFactory(Settings).wrappedMethod()).to.equal('S3Persistor')
  44. })
  45. it("should implement the S3 wrapped method when 'aws-sdk' is configured", function () {
  46. Settings.backend = 'aws-sdk'
  47. expect(PersistorFactory(Settings)).to.respondTo('wrappedMethod')
  48. expect(PersistorFactory(Settings).wrappedMethod()).to.equal('S3Persistor')
  49. })
  50. it('should implement the FS wrapped method when FS is configured', function () {
  51. Settings.backend = 'fs'
  52. expect(PersistorFactory(Settings)).to.respondTo('wrappedMethod')
  53. expect(PersistorFactory(Settings).wrappedMethod()).to.equal('FSPersistor')
  54. })
  55. it('should forward useSubdirectories=true to FSPersistor', function () {
  56. Settings.backend = 'fs'
  57. Settings.useSubdirectories = true
  58. expect(PersistorFactory(Settings).settings.useSubdirectories).to.be.true
  59. })
  60. it('should forward useSubdirectories=false to FSPersistor', function () {
  61. Settings.backend = 'fs'
  62. Settings.useSubdirectories = false
  63. expect(PersistorFactory(Settings).settings.useSubdirectories).to.be.false
  64. })
  65. it('should throw an error when the backend is not configured', function () {
  66. try {
  67. PersistorFactory(Settings)
  68. } catch (err) {
  69. expect(err.message).to.equal('no backend specified - config incomplete')
  70. return
  71. }
  72. expect('should have caught an error').not.to.exist
  73. })
  74. it('should throw an error when the backend is unknown', function () {
  75. Settings.backend = 'magic'
  76. try {
  77. PersistorFactory(Settings)
  78. } catch (err) {
  79. expect(err.message).to.equal('unknown backend')
  80. expect(err.info.backend).to.equal('magic')
  81. return
  82. }
  83. expect('should have caught an error').not.to.exist
  84. })
  85. })