FileConverterTests.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. 'metrics-sharelatex': {
  25. inc: sinon.stub(),
  26. Timer: sinon.stub().returns({ done: sinon.stub() })
  27. },
  28. 'settings-sharelatex': Settings
  29. }
  30. })
  31. })
  32. describe('convert', function() {
  33. it('should convert the source to the requested format', async function() {
  34. await FileConverter.promises.convert(sourcePath, format)
  35. const args = SafeExec.promises.args[0][0]
  36. expect(args).to.include(`${sourcePath}[0]`)
  37. expect(args).to.include(`${sourcePath}.${format}`)
  38. })
  39. it('should return the dest path', async function() {
  40. const destPath = await FileConverter.promises.convert(sourcePath, format)
  41. destPath.should.equal(`${sourcePath}.${format}`)
  42. })
  43. it('should wrap the error from convert', async function() {
  44. SafeExec.promises.rejects(errorMessage)
  45. try {
  46. await FileConverter.promises.convert(sourcePath, format)
  47. expect('error should have been thrown').not.to.exist
  48. } catch (err) {
  49. expect(err.name).to.equal('ConversionError')
  50. expect(err.cause.toString()).to.equal(errorMessage)
  51. }
  52. })
  53. it('should not accept an non approved format', async function() {
  54. try {
  55. await FileConverter.promises.convert(sourcePath, 'potato')
  56. expect('error should have been thrown').not.to.exist
  57. } catch (err) {
  58. expect(err.name).to.equal('ConversionError')
  59. }
  60. })
  61. it('should prefix the command with Settings.commands.convertCommandPrefix', async function() {
  62. Settings.commands.convertCommandPrefix = ['nice']
  63. await FileConverter.promises.convert(sourcePath, format)
  64. })
  65. it('should convert the file when called as a callback', function(done) {
  66. FileConverter.convert(sourcePath, format, (err, destPath) => {
  67. expect(err).not.to.exist
  68. destPath.should.equal(`${sourcePath}.${format}`)
  69. const args = SafeExec.promises.args[0][0]
  70. expect(args).to.include(`${sourcePath}[0]`)
  71. expect(args).to.include(`${sourcePath}.${format}`)
  72. done()
  73. })
  74. })
  75. })
  76. describe('thumbnail', function() {
  77. it('should call converter resize with args', async function() {
  78. await FileConverter.promises.thumbnail(sourcePath)
  79. const args = SafeExec.promises.args[0][0]
  80. expect(args).to.include(`${sourcePath}[0]`)
  81. })
  82. })
  83. describe('preview', function() {
  84. it('should call converter resize with args', async function() {
  85. await FileConverter.promises.preview(sourcePath)
  86. const args = SafeExec.promises.args[0][0]
  87. expect(args).to.include(`${sourcePath}[0]`)
  88. })
  89. })
  90. })