LocalFileWriterTests.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. 'settings-sharelatex': settings,
  27. 'metrics-sharelatex': {
  28. inc: sinon.stub(),
  29. Timer: sinon.stub().returns({ done: sinon.stub() })
  30. }
  31. }
  32. })
  33. })
  34. describe('writeStream', function() {
  35. it('writes the stream to the upload folder', function(done) {
  36. LocalFileWriter.writeStream(readStream, filename, (err, path) => {
  37. expect(err).not.to.exist
  38. expect(fs.createWriteStream).to.have.been.calledWith(fsPath)
  39. expect(stream.pipeline).to.have.been.calledWith(readStream, writeStream)
  40. expect(path).to.equal(fsPath)
  41. done()
  42. })
  43. })
  44. describe('when there is an error', function() {
  45. const error = new Error('not enough ketchup')
  46. beforeEach(function() {
  47. stream.pipeline.yields(error)
  48. })
  49. it('should wrap the error', function() {
  50. LocalFileWriter.writeStream(readStream, filename, err => {
  51. expect(err).to.exist
  52. expect(err.cause).to.equal(error)
  53. })
  54. })
  55. it('should delete the temporary file', function() {
  56. LocalFileWriter.writeStream(readStream, filename, () => {
  57. expect(fs.unlink).to.have.been.calledWith(fsPath)
  58. })
  59. })
  60. })
  61. })
  62. describe('deleteFile', function() {
  63. it('should unlink the file', function(done) {
  64. LocalFileWriter.deleteFile(fsPath, err => {
  65. expect(err).not.to.exist
  66. expect(fs.unlink).to.have.been.calledWith(fsPath)
  67. done()
  68. })
  69. })
  70. it('should not call unlink with an empty path', function(done) {
  71. LocalFileWriter.deleteFile('', err => {
  72. expect(err).not.to.exist
  73. expect(fs.unlink).not.to.have.been.called
  74. done()
  75. })
  76. })
  77. it('should not throw a error if the file does not exist', function(done) {
  78. const error = new Error('file not found')
  79. error.code = 'ENOENT'
  80. fs.unlink = sinon.stub().yields(error)
  81. LocalFileWriter.deleteFile(fsPath, err => {
  82. expect(err).not.to.exist
  83. done()
  84. })
  85. })
  86. it('should wrap the error', function(done) {
  87. const error = new Error('failed to reticulate splines')
  88. fs.unlink = sinon.stub().yields(error)
  89. LocalFileWriter.deleteFile(fsPath, err => {
  90. expect(err).to.exist
  91. expect(err.cause).to.equal(error)
  92. done()
  93. })
  94. })
  95. })
  96. })