LocalFileWriterTests.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 {
  14. assert
  15. } = require("chai");
  16. const sinon = require('sinon');
  17. const chai = require('chai');
  18. const should = chai.should();
  19. const {
  20. expect
  21. } = chai;
  22. const modulePath = "../../../app/js/LocalFileWriter.js";
  23. const SandboxedModule = require('sandboxed-module');
  24. describe("LocalFileWriter", function() {
  25. beforeEach(function() {
  26. this.writeStream = {
  27. on(type, cb){
  28. if (type === "finish") {
  29. return cb();
  30. }
  31. }
  32. };
  33. this.readStream =
  34. {on() {}};
  35. this.fs = {
  36. createWriteStream : sinon.stub().returns(this.writeStream),
  37. createReadStream: sinon.stub().returns(this.readStream),
  38. unlink: sinon.stub()
  39. };
  40. this.settings = {
  41. path: {
  42. uploadFolder:"somewhere"
  43. }
  44. };
  45. this.writer = SandboxedModule.require(modulePath, { requires: {
  46. "fs": this.fs,
  47. "logger-sharelatex": {
  48. log() {},
  49. err() {}
  50. },
  51. "settings-sharelatex":this.settings,
  52. "metrics-sharelatex": {
  53. inc() {},
  54. Timer() {
  55. return {done() {}};
  56. }
  57. }
  58. }
  59. }
  60. );
  61. return this.stubbedFsPath = "something/uploads/eio2k1j3";
  62. });
  63. describe("writeStrem", function() {
  64. beforeEach(function() {
  65. return this.writer._getPath = sinon.stub().returns(this.stubbedFsPath);
  66. });
  67. it("write the stream to ./uploads", function(done){
  68. const stream = {
  69. pipe: dest=> {
  70. dest.should.equal(this.writeStream);
  71. return done();
  72. },
  73. on() {}
  74. };
  75. return this.writer.writeStream(stream, null, ()=> {});
  76. });
  77. return it("should send the path in the callback", function(done){
  78. const stream = {
  79. pipe: dest=> {},
  80. on(type, cb){
  81. if (type === "end") {
  82. return cb();
  83. }
  84. }
  85. };
  86. return this.writer.writeStream(stream, null, (err, fsPath)=> {
  87. fsPath.should.equal(this.stubbedFsPath);
  88. return done();
  89. });
  90. });
  91. });
  92. describe("getStream", function() {
  93. it("should read the stream from the file ", function(done){
  94. return this.writer.getStream(this.stubbedFsPath, (err, stream)=> {
  95. this.fs.createReadStream.calledWith(this.stubbedFsPath).should.equal(true);
  96. return done();
  97. });
  98. });
  99. return it("should send the stream in the callback", function(done){
  100. return this.writer.getStream(this.stubbedFsPath, (err, readStream)=> {
  101. readStream.should.equal(this.readStream);
  102. return done();
  103. });
  104. });
  105. });
  106. return describe("delete file", () => it("should unlink the file", function(done){
  107. const error = "my error";
  108. this.fs.unlink.callsArgWith(1, error);
  109. return this.writer.deleteFile(this.stubbedFsPath, err=> {
  110. this.fs.unlink.calledWith(this.stubbedFsPath).should.equal(true);
  111. err.should.equal(error);
  112. return done();
  113. });
  114. }));
  115. });