PersistorFactoryTests.js 2.3 KB

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