LocalFileWriterTests.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const sinon = require('sinon')
  2. const chai = require('chai')
  3. const { expect } = chai
  4. const modulePath = '../../../app/js/LocalFileWriter.js'
  5. const SandboxedModule = require('sandboxed-module')
  6. chai.use(require('sinon-chai'))
  7. describe('LocalFileWriter', function() {
  8. const writeStream = 'writeStream'
  9. const readStream = 'readStream'
  10. const settings = { path: { uploadFolder: '/uploads' } }
  11. const fsPath = '/uploads/wombat'
  12. const filename = 'wombat'
  13. let stream, fs, LocalFileWriter
  14. beforeEach(function() {
  15. fs = {
  16. createWriteStream: sinon.stub().returns(writeStream),
  17. unlink: sinon.stub().yields()
  18. }
  19. stream = {
  20. pipeline: sinon.stub().yields()
  21. }
  22. LocalFileWriter = SandboxedModule.require(modulePath, {
  23. requires: {
  24. fs,
  25. stream,
  26. 'logger-sharelatex': {
  27. log() {},
  28. err() {}
  29. },
  30. 'settings-sharelatex': settings,
  31. 'metrics-sharelatex': {
  32. inc: sinon.stub(),
  33. Timer: sinon.stub().returns({ done: sinon.stub() })
  34. }
  35. }
  36. })
  37. })
  38. describe('writeStream', function() {
  39. it('writes the stream to the upload folder', function(done) {
  40. LocalFileWriter.writeStream(readStream, filename, (err, path) => {
  41. expect(err).not.to.exist
  42. expect(fs.createWriteStream).to.have.been.calledWith(fsPath)
  43. expect(stream.pipeline).to.have.been.calledWith(readStream, writeStream)
  44. expect(path).to.equal(fsPath)
  45. done()
  46. })
  47. })
  48. describe('when there is an error', function() {
  49. const error = new Error('not enough ketchup')
  50. beforeEach(function() {
  51. stream.pipeline.yields(error)
  52. })
  53. it('should wrap the error', function() {
  54. LocalFileWriter.writeStream(readStream, filename, err => {
  55. expect(err).to.exist
  56. expect(err.cause).to.equal(error)
  57. })
  58. })
  59. it('should delete the temporary file', function() {
  60. LocalFileWriter.writeStream(readStream, filename, () => {
  61. expect(fs.unlink).to.have.been.calledWith(fsPath)
  62. })
  63. })
  64. })
  65. })
  66. describe('deleteFile', function() {
  67. it('should unlink the file', function(done) {
  68. LocalFileWriter.deleteFile(fsPath, err => {
  69. expect(err).not.to.exist
  70. expect(fs.unlink).to.have.been.calledWith(fsPath)
  71. done()
  72. })
  73. })
  74. it('should not call unlink with an empty path', function(done) {
  75. LocalFileWriter.deleteFile('', err => {
  76. expect(err).not.to.exist
  77. expect(fs.unlink).not.to.have.been.called
  78. done()
  79. })
  80. })
  81. it('should not throw a error if the file does not exist', function(done) {
  82. const error = new Error('file not found')
  83. error.code = 'ENOENT'
  84. fs.unlink = sinon.stub().yields(error)
  85. LocalFileWriter.deleteFile(fsPath, err => {
  86. expect(err).not.to.exist
  87. done()
  88. })
  89. })
  90. it('should wrap the error', function(done) {
  91. const error = new Error('failed to reticulate splines')
  92. fs.unlink = sinon.stub().yields(error)
  93. LocalFileWriter.deleteFile(fsPath, err => {
  94. expect(err).to.exist
  95. expect(err.cause).to.equal(error)
  96. done()
  97. })
  98. })
  99. })
  100. })