FileHandlerTests.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/FileHandler.js";
  23. const SandboxedModule = require('sandboxed-module');
  24. describe("FileHandler", function() {
  25. beforeEach(function() {
  26. this.settings = {
  27. s3: {
  28. buckets: {
  29. user_files:"user_files"
  30. }
  31. }
  32. };
  33. this.PersistorManager = {
  34. getFileStream: sinon.stub(),
  35. checkIfFileExists: sinon.stub(),
  36. deleteFile: sinon.stub(),
  37. deleteDirectory: sinon.stub(),
  38. sendStream: sinon.stub(),
  39. insertFile: sinon.stub(),
  40. directorySize: sinon.stub()
  41. };
  42. this.LocalFileWriter = {
  43. writeStream: sinon.stub(),
  44. getStream: sinon.stub(),
  45. deleteFile: sinon.stub()
  46. };
  47. this.FileConverter = {
  48. convert: sinon.stub(),
  49. thumbnail: sinon.stub(),
  50. preview: sinon.stub()
  51. };
  52. this.keyBuilder = {
  53. addCachingToKey: sinon.stub(),
  54. getConvertedFolderKey: sinon.stub()
  55. };
  56. this.ImageOptimiser =
  57. {compressPng: sinon.stub()};
  58. this.handler = SandboxedModule.require(modulePath, { requires: {
  59. "settings-sharelatex": this.settings,
  60. "./PersistorManager":this.PersistorManager,
  61. "./LocalFileWriter":this.LocalFileWriter,
  62. "./FileConverter":this.FileConverter,
  63. "./KeyBuilder": this.keyBuilder,
  64. "./ImageOptimiser":this.ImageOptimiser,
  65. "logger-sharelatex": {
  66. log() {},
  67. err() {}
  68. }
  69. }
  70. }
  71. );
  72. this.bucket = "my_bucket";
  73. this.key = "key/here";
  74. this.stubbedPath = "/var/somewhere/path";
  75. this.format = "png";
  76. return this.formattedStubbedPath = `${this.stubbedPath}.${this.format}`;
  77. });
  78. describe("insertFile", function() {
  79. beforeEach(function() {
  80. this.stream = {};
  81. this.PersistorManager.deleteDirectory.callsArgWith(2);
  82. return this.PersistorManager.sendStream.callsArgWith(3);
  83. });
  84. it("should send file to the filestore", function(done){
  85. return this.handler.insertFile(this.bucket, this.key, this.stream, () => {
  86. this.PersistorManager.sendStream.calledWith(this.bucket, this.key, this.stream).should.equal(true);
  87. return done();
  88. });
  89. });
  90. return it("should delete the convetedKey folder", function(done){
  91. this.keyBuilder.getConvertedFolderKey.returns(this.stubbedConvetedKey);
  92. return this.handler.insertFile(this.bucket, this.key, this.stream, () => {
  93. this.PersistorManager.deleteDirectory.calledWith(this.bucket, this.stubbedConvetedKey).should.equal(true);
  94. return done();
  95. });
  96. });
  97. });
  98. describe("deleteFile", function() {
  99. beforeEach(function() {
  100. this.keyBuilder.getConvertedFolderKey.returns(this.stubbedConvetedKey);
  101. this.PersistorManager.deleteFile.callsArgWith(2);
  102. return this.PersistorManager.deleteDirectory.callsArgWith(2);
  103. });
  104. it("should tell the filestore manager to delete the file", function(done){
  105. return this.handler.deleteFile(this.bucket, this.key, () => {
  106. this.PersistorManager.deleteFile.calledWith(this.bucket, this.key).should.equal(true);
  107. return done();
  108. });
  109. });
  110. return it("should tell the filestore manager to delete the cached foler", function(done){
  111. return this.handler.deleteFile(this.bucket, this.key, () => {
  112. this.PersistorManager.deleteDirectory.calledWith(this.bucket, this.stubbedConvetedKey).should.equal(true);
  113. return done();
  114. });
  115. });
  116. });
  117. describe("getFile", function() {
  118. beforeEach(function() {
  119. this.handler._getStandardFile = sinon.stub().callsArgWith(3);
  120. return this.handler._getConvertedFile = sinon.stub().callsArgWith(3);
  121. });
  122. it("should call _getStandardFile if no format or style are defined", function(done){
  123. return this.handler.getFile(this.bucket, this.key, null, () => {
  124. this.handler._getStandardFile.called.should.equal(true);
  125. this.handler._getConvertedFile.called.should.equal(false);
  126. return done();
  127. });
  128. });
  129. it("should pass options to _getStandardFile", function(done) {
  130. const options = {start: 0, end: 8};
  131. return this.handler.getFile(this.bucket, this.key, options, () => {
  132. expect(this.handler._getStandardFile.lastCall.args[2].start).to.equal(0);
  133. expect(this.handler._getStandardFile.lastCall.args[2].end).to.equal(8);
  134. return done();
  135. });
  136. });
  137. return it("should call _getConvertedFile if a format is defined", function(done){
  138. return this.handler.getFile(this.bucket, this.key, {format:"png"}, () => {
  139. this.handler._getStandardFile.called.should.equal(false);
  140. this.handler._getConvertedFile.called.should.equal(true);
  141. return done();
  142. });
  143. });
  144. });
  145. describe("_getStandardFile", function() {
  146. beforeEach(function() {
  147. this.fileStream = {on() {}};
  148. return this.PersistorManager.getFileStream.callsArgWith(3, "err", this.fileStream);
  149. });
  150. it("should get the stream", function(done){
  151. return this.handler.getFile(this.bucket, this.key, null, () => {
  152. this.PersistorManager.getFileStream.calledWith(this.bucket, this.key).should.equal(true);
  153. return done();
  154. });
  155. });
  156. it("should return the stream and error", function(done){
  157. return this.handler.getFile(this.bucket, this.key, null, (err, stream)=> {
  158. err.should.equal("err");
  159. stream.should.equal(this.fileStream);
  160. return done();
  161. });
  162. });
  163. return it("should pass options to PersistorManager", function(done) {
  164. return this.handler.getFile(this.bucket, this.key, {start: 0, end: 8}, () => {
  165. expect(this.PersistorManager.getFileStream.lastCall.args[2].start).to.equal(0);
  166. expect(this.PersistorManager.getFileStream.lastCall.args[2].end).to.equal(8);
  167. return done();
  168. });
  169. });
  170. });
  171. describe("_getConvertedFile", function() {
  172. it("should getFileStream if it does exists", function(done){
  173. this.PersistorManager.checkIfFileExists.callsArgWith(2, null, true);
  174. this.PersistorManager.getFileStream.callsArgWith(3);
  175. return this.handler._getConvertedFile(this.bucket, this.key, {}, () => {
  176. this.PersistorManager.getFileStream.calledWith(this.bucket).should.equal(true);
  177. return done();
  178. });
  179. });
  180. return it("should call _getConvertedFileAndCache if it does exists", function(done){
  181. this.PersistorManager.checkIfFileExists.callsArgWith(2, null, false);
  182. this.handler._getConvertedFileAndCache = sinon.stub().callsArgWith(4);
  183. return this.handler._getConvertedFile(this.bucket, this.key, {}, () => {
  184. this.handler._getConvertedFileAndCache.calledWith(this.bucket, this.key).should.equal(true);
  185. return done();
  186. });
  187. });
  188. });
  189. describe("_getConvertedFileAndCache", () => it("should _convertFile ", function(done){
  190. this.stubbedStream = {"something":"here"};
  191. this.localStream = {
  192. on() {}
  193. };
  194. this.PersistorManager.sendFile = sinon.stub().callsArgWith(3);
  195. this.LocalFileWriter.getStream = sinon.stub().callsArgWith(1, null, this.localStream);
  196. this.convetedKey = this.key+"converted";
  197. this.handler._convertFile = sinon.stub().callsArgWith(3, null, this.stubbedPath);
  198. this.ImageOptimiser.compressPng = sinon.stub().callsArgWith(1);
  199. return this.handler._getConvertedFileAndCache(this.bucket, this.key, this.convetedKey, {}, (err, fsStream)=> {
  200. this.handler._convertFile.called.should.equal(true);
  201. this.PersistorManager.sendFile.calledWith(this.bucket, this.convetedKey, this.stubbedPath).should.equal(true);
  202. this.ImageOptimiser.compressPng.calledWith(this.stubbedPath).should.equal(true);
  203. this.LocalFileWriter.getStream.calledWith(this.stubbedPath).should.equal(true);
  204. fsStream.should.equal(this.localStream);
  205. return done();
  206. });
  207. }));
  208. describe("_convertFile", function() {
  209. beforeEach(function() {
  210. this.FileConverter.convert.callsArgWith(2, null, this.formattedStubbedPath);
  211. this.FileConverter.thumbnail.callsArgWith(1, null, this.formattedStubbedPath);
  212. this.FileConverter.preview.callsArgWith(1, null, this.formattedStubbedPath);
  213. this.handler._writeS3FileToDisk = sinon.stub().callsArgWith(3, null, this.stubbedPath);
  214. return this.LocalFileWriter.deleteFile.callsArgWith(1);
  215. });
  216. it("should call thumbnail on the writer path if style was thumbnail was specified", function(done){
  217. return this.handler._convertFile(this.bucket, this.key, {style:"thumbnail"}, (err, path)=> {
  218. path.should.equal(this.formattedStubbedPath);
  219. this.FileConverter.thumbnail.calledWith(this.stubbedPath).should.equal(true);
  220. this.LocalFileWriter.deleteFile.calledWith(this.stubbedPath).should.equal(true);
  221. return done();
  222. });
  223. });
  224. it("should call preview on the writer path if style was preview was specified", function(done){
  225. return this.handler._convertFile(this.bucket, this.key, {style:"preview"}, (err, path)=> {
  226. path.should.equal(this.formattedStubbedPath);
  227. this.FileConverter.preview.calledWith(this.stubbedPath).should.equal(true);
  228. this.LocalFileWriter.deleteFile.calledWith(this.stubbedPath).should.equal(true);
  229. return done();
  230. });
  231. });
  232. return it("should call convert on the writer path if a format was specified", function(done){
  233. return this.handler._convertFile(this.bucket, this.key, {format:this.format}, (err, path)=> {
  234. path.should.equal(this.formattedStubbedPath);
  235. this.FileConverter.convert.calledWith(this.stubbedPath, this.format).should.equal(true);
  236. this.LocalFileWriter.deleteFile.calledWith(this.stubbedPath).should.equal(true);
  237. return done();
  238. });
  239. });
  240. });
  241. return describe("getDirectorySize", function() {
  242. beforeEach(function() {
  243. return this.PersistorManager.directorySize.callsArgWith(2);
  244. });
  245. return it("should call the filestore manager to get directory size", function(done){
  246. return this.handler.getDirectorySize(this.bucket, this.key, () => {
  247. this.PersistorManager.directorySize.calledWith(this.bucket, this.key).should.equal(true);
  248. return done();
  249. });
  250. });
  251. });
  252. });