PersistorFactoryTests.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. wrappedMethod() {
  11. return 'FSPersistor'
  12. }
  13. }
  14. S3Persistor = class {
  15. wrappedMethod() {
  16. return 'S3Persistor'
  17. }
  18. }
  19. GcsPersistor = class {
  20. wrappedMethod() {
  21. return 'GcsPersistor'
  22. }
  23. }
  24. Settings = {}
  25. const requires = {
  26. './GcsPersistor': GcsPersistor,
  27. './S3Persistor': S3Persistor,
  28. './FSPersistor': FSPersistor,
  29. '@overleaf/logger': {
  30. info() {},
  31. err() {},
  32. },
  33. 'stream/promises': StreamPromises,
  34. }
  35. PersistorFactory = SandboxedModule.require(MODULE_PATH, { requires })
  36. })
  37. it('should implement the S3 wrapped method when S3 is configured', function () {
  38. Settings.backend = 's3'
  39. expect(PersistorFactory(Settings)).to.respondTo('wrappedMethod')
  40. expect(PersistorFactory(Settings).wrappedMethod()).to.equal('S3Persistor')
  41. })
  42. it("should implement the S3 wrapped method when 'aws-sdk' is configured", function () {
  43. Settings.backend = 'aws-sdk'
  44. expect(PersistorFactory(Settings)).to.respondTo('wrappedMethod')
  45. expect(PersistorFactory(Settings).wrappedMethod()).to.equal('S3Persistor')
  46. })
  47. it('should implement the FS wrapped method when FS is configured', function () {
  48. Settings.backend = 'fs'
  49. expect(PersistorFactory(Settings)).to.respondTo('wrappedMethod')
  50. expect(PersistorFactory(Settings).wrappedMethod()).to.equal('FSPersistor')
  51. })
  52. it('should throw an error when the backend is not configured', function () {
  53. try {
  54. PersistorFactory(Settings)
  55. } catch (err) {
  56. expect(err.message).to.equal('no backend specified - config incomplete')
  57. return
  58. }
  59. expect('should have caught an error').not.to.exist
  60. })
  61. it('should throw an error when the backend is unknown', function () {
  62. Settings.backend = 'magic'
  63. try {
  64. PersistorFactory(Settings)
  65. } catch (err) {
  66. expect(err.message).to.equal('unknown backend')
  67. expect(err.info.backend).to.equal('magic')
  68. return
  69. }
  70. expect('should have caught an error').not.to.exist
  71. })
  72. })