LocalFileWriterTests.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* eslint-disable
  2. handle-callback-err,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const { assert } = require('chai')
  14. const sinon = require('sinon')
  15. const chai = require('chai')
  16. const should = chai.should()
  17. const { expect } = chai
  18. const modulePath = '../../../app/js/LocalFileWriter.js'
  19. const SandboxedModule = require('sandboxed-module')
  20. describe('LocalFileWriter', function() {
  21. beforeEach(function() {
  22. this.writeStream = {
  23. on(type, cb) {
  24. if (type === 'finish') {
  25. return cb()
  26. }
  27. }
  28. }
  29. this.readStream = { on() {} }
  30. this.fs = {
  31. createWriteStream: sinon.stub().returns(this.writeStream),
  32. createReadStream: sinon.stub().returns(this.readStream),
  33. unlink: sinon.stub()
  34. }
  35. this.settings = {
  36. path: {
  37. uploadFolder: 'somewhere'
  38. }
  39. }
  40. this.writer = SandboxedModule.require(modulePath, {
  41. requires: {
  42. fs: this.fs,
  43. 'logger-sharelatex': {
  44. log() {},
  45. err() {}
  46. },
  47. 'settings-sharelatex': this.settings,
  48. 'metrics-sharelatex': {
  49. inc() {},
  50. Timer() {
  51. return { done() {} }
  52. }
  53. }
  54. }
  55. })
  56. return (this.stubbedFsPath = 'something/uploads/eio2k1j3')
  57. })
  58. describe('writeStrem', function() {
  59. beforeEach(function() {
  60. return (this.writer._getPath = sinon.stub().returns(this.stubbedFsPath))
  61. })
  62. it('write the stream to ./uploads', function(done) {
  63. const stream = {
  64. pipe: dest => {
  65. dest.should.equal(this.writeStream)
  66. return done()
  67. },
  68. on() {}
  69. }
  70. return this.writer.writeStream(stream, null, () => {})
  71. })
  72. return it('should send the path in the callback', function(done) {
  73. const stream = {
  74. pipe: dest => {},
  75. on(type, cb) {
  76. if (type === 'end') {
  77. return cb()
  78. }
  79. }
  80. }
  81. return this.writer.writeStream(stream, null, (err, fsPath) => {
  82. fsPath.should.equal(this.stubbedFsPath)
  83. return done()
  84. })
  85. })
  86. })
  87. describe('getStream', function() {
  88. it('should read the stream from the file ', function(done) {
  89. return this.writer.getStream(this.stubbedFsPath, (err, stream) => {
  90. this.fs.createReadStream
  91. .calledWith(this.stubbedFsPath)
  92. .should.equal(true)
  93. return done()
  94. })
  95. })
  96. return it('should send the stream in the callback', function(done) {
  97. return this.writer.getStream(this.stubbedFsPath, (err, readStream) => {
  98. readStream.should.equal(this.readStream)
  99. return done()
  100. })
  101. })
  102. })
  103. return describe('delete file', () =>
  104. it('should unlink the file', function(done) {
  105. const error = 'my error'
  106. this.fs.unlink.callsArgWith(1, error)
  107. return this.writer.deleteFile(this.stubbedFsPath, err => {
  108. this.fs.unlink.calledWith(this.stubbedFsPath).should.equal(true)
  109. err.should.equal(error)
  110. return done()
  111. })
  112. }))
  113. })