FileConverterTests.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const sinon = require('sinon')
  2. const chai = require('chai')
  3. const { expect } = chai
  4. const SandboxedModule = require('sandboxed-module')
  5. const modulePath = '../../../app/js/FileConverter.js'
  6. describe('FileConverter', function() {
  7. let SafeExec, FileConverter
  8. const sourcePath = '/data/wombat.eps'
  9. const destPath = '/tmp/dest.png'
  10. const format = 'png'
  11. const errorMessage = 'guru meditation error'
  12. const Settings = {
  13. commands: {
  14. convertCommandPrefix: []
  15. }
  16. }
  17. beforeEach(function() {
  18. SafeExec = {
  19. promises: sinon.stub().resolves(destPath)
  20. }
  21. FileConverter = SandboxedModule.require(modulePath, {
  22. requires: {
  23. './SafeExec': SafeExec,
  24. 'logger-sharelatex': {
  25. log() {},
  26. err() {}
  27. },
  28. 'metrics-sharelatex': {
  29. inc: sinon.stub(),
  30. Timer: sinon.stub().returns({ done: sinon.stub() })
  31. },
  32. 'settings-sharelatex': Settings
  33. }
  34. })
  35. })
  36. describe('convert', function() {
  37. it('should convert the source to the requested format', async function() {
  38. await FileConverter.promises.convert(sourcePath, format)
  39. const args = SafeExec.promises.args[0][0]
  40. expect(args).to.include(`${sourcePath}[0]`)
  41. expect(args).to.include(`${sourcePath}.${format}`)
  42. })
  43. it('should return the dest path', async function() {
  44. const destPath = await FileConverter.promises.convert(sourcePath, format)
  45. destPath.should.equal(`${sourcePath}.${format}`)
  46. })
  47. it('should wrap the error from convert', async function() {
  48. SafeExec.promises.rejects(errorMessage)
  49. try {
  50. await FileConverter.promises.convert(sourcePath, format)
  51. expect('error should have been thrown').not.to.exist
  52. } catch (err) {
  53. expect(err.name).to.equal('ConversionError')
  54. expect(err.cause.toString()).to.equal(errorMessage)
  55. }
  56. })
  57. it('should not accept an non approved format', async function() {
  58. try {
  59. await FileConverter.promises.convert(sourcePath, 'potato')
  60. expect('error should have been thrown').not.to.exist
  61. } catch (err) {
  62. expect(err.name).to.equal('ConversionError')
  63. }
  64. })
  65. it('should prefix the command with Settings.commands.convertCommandPrefix', async function() {
  66. Settings.commands.convertCommandPrefix = ['nice']
  67. await FileConverter.promises.convert(sourcePath, format)
  68. })
  69. it('should convert the file when called as a callback', function(done) {
  70. FileConverter.convert(sourcePath, format, (err, destPath) => {
  71. expect(err).not.to.exist
  72. destPath.should.equal(`${sourcePath}.${format}`)
  73. const args = SafeExec.promises.args[0][0]
  74. expect(args).to.include(`${sourcePath}[0]`)
  75. expect(args).to.include(`${sourcePath}.${format}`)
  76. done()
  77. })
  78. })
  79. })
  80. describe('thumbnail', function() {
  81. it('should call converter resize with args', async function() {
  82. await FileConverter.promises.thumbnail(sourcePath)
  83. const args = SafeExec.promises.args[0][0]
  84. expect(args).to.include(`${sourcePath}[0]`)
  85. })
  86. })
  87. describe('preview', function() {
  88. it('should call converter resize with args', async function() {
  89. await FileConverter.promises.preview(sourcePath)
  90. const args = SafeExec.promises.args[0][0]
  91. expect(args).to.include(`${sourcePath}[0]`)
  92. })
  93. })
  94. })