FileControllerTests.coffee 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. assert = require("chai").assert
  2. sinon = require('sinon')
  3. chai = require('chai')
  4. should = chai.should()
  5. expect = chai.expect
  6. modulePath = "../../../app/js/FileController.js"
  7. SandboxedModule = require('sandboxed-module')
  8. describe "FileController", ->
  9. beforeEach ->
  10. @PersistorManager =
  11. sendStream: sinon.stub()
  12. copyFile: sinon.stub()
  13. deleteFile:sinon.stub()
  14. @settings =
  15. s3:
  16. buckets:
  17. user_files:"user_files"
  18. @FileHandler =
  19. getFile: sinon.stub()
  20. deleteFile: sinon.stub()
  21. insertFile: sinon.stub()
  22. @LocalFileWriter = {}
  23. @controller = SandboxedModule.require modulePath, requires:
  24. "./LocalFileWriter":@LocalFileWriter
  25. "./FileHandler": @FileHandler
  26. "./PersistorManager":@PersistorManager
  27. "settings-sharelatex": @settings
  28. "logger-sharelatex":
  29. log:->
  30. err:->
  31. @project_id = "project_id"
  32. @file_id = "file_id"
  33. @bucket = "user_files"
  34. @key = "#{@project_id}/#{@file_id}"
  35. @req =
  36. key:@key
  37. bucket:@bucket
  38. query:{}
  39. params:
  40. project_id:@project_id
  41. file_id:@file_id
  42. @res =
  43. header: sinon.stub()
  44. @fileStream = {}
  45. describe "getFile", ->
  46. it "should pipe the stream", (done)->
  47. @FileHandler.getFile.callsArgWith(3, null, @fileStream, @size = 42)
  48. @fileStream.pipe = (res)=>
  49. res.should.equal @res
  50. res.header.calledWith("Content-Length", @size).should.equal true
  51. done()
  52. @controller.getFile @req, @res
  53. it "should send a 200 if the cacheWarm param is true", (done)->
  54. @req.query.cacheWarm = true
  55. @FileHandler.getFile.callsArgWith(3, null, @fileStream)
  56. @res.send = (statusCode)=>
  57. statusCode.should.equal 200
  58. done()
  59. @controller.getFile @req, @res
  60. it "should send a 500 if there is a problem", (done)->
  61. @FileHandler.getFile.callsArgWith(3, "error")
  62. @res.send = (code)=>
  63. code.should.equal 500
  64. done()
  65. @controller.getFile @req, @res
  66. describe "insertFile", ->
  67. it "should send bucket name key and res to PersistorManager", (done)->
  68. @FileHandler.insertFile.callsArgWith(3)
  69. @res.send = =>
  70. @FileHandler.insertFile.calledWith(@bucket, @key, @req).should.equal true
  71. done()
  72. @controller.insertFile @req, @res
  73. describe "copyFile", ->
  74. beforeEach ->
  75. @oldFile_id = "old_file_id"
  76. @oldProject_id = "old_project_id"
  77. @req.body =
  78. source:
  79. project_id: @oldProject_id
  80. file_id: @oldFile_id
  81. it "should send bucket name and both keys to PersistorManager", (done)->
  82. @PersistorManager.copyFile.callsArgWith(3)
  83. @res.send = (code)=>
  84. code.should.equal 200
  85. @PersistorManager.copyFile.calledWith(@bucket, "#{@oldProject_id}/#{@oldFile_id}", @key).should.equal true
  86. done()
  87. @controller.copyFile @req, @res
  88. it "should send a 500 if there was an error", (done)->
  89. @PersistorManager.copyFile.callsArgWith(3, "error")
  90. @res.send = (code)=>
  91. code.should.equal 500
  92. done()
  93. @controller.copyFile @req, @res
  94. describe "delete file", ->
  95. it "should tell the file handler", (done)->
  96. @FileHandler.deleteFile.callsArgWith(2)
  97. @res.send = (code)=>
  98. code.should.equal 204
  99. @FileHandler.deleteFile.calledWith(@bucket, @key).should.equal true
  100. done()
  101. @controller.deleteFile @req, @res
  102. it "should send a 500 if there was an error", (done)->
  103. @FileHandler.deleteFile.callsArgWith(2, "error")
  104. @res.send = (code)->
  105. code.should.equal 500
  106. done()
  107. @controller.deleteFile @req, @res