FileControllerTests.coffee 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. @s3Wrapper =
  11. sendStreamToS3: sinon.stub()
  12. getAndPipe: sinon.stub()
  13. copyFile: sinon.stub()
  14. deleteFile:sinon.stub()
  15. @settings =
  16. s3:
  17. buckets:
  18. user_files:"user_files"
  19. @FileHandler =
  20. getFile: sinon.stub()
  21. deleteFile: sinon.stub()
  22. insertFile: sinon.stub()
  23. @LocalFileWriter = {}
  24. @controller = SandboxedModule.require modulePath, requires:
  25. "./LocalFileWriter":@LocalFileWriter
  26. "./FileHandler": @FileHandler
  27. "./s3Wrapper":@s3Wrapper
  28. "settings-sharelatex": @settings
  29. "logger-sharelatex":
  30. log:->
  31. err:->
  32. @project_id = "project_id"
  33. @file_id = "file_id"
  34. @bucket = "user_files"
  35. @key = "#{@project_id}/#{@file_id}"
  36. @req =
  37. key:@key
  38. bucket:@bucket
  39. query:{}
  40. params:
  41. project_id:@project_id
  42. file_id:@file_id
  43. @res =
  44. setHeader: ->
  45. @fileStream = {}
  46. describe "getFile", ->
  47. it "should pipe the stream", (done)->
  48. @FileHandler.getFile.callsArgWith(3, null, @fileStream)
  49. @fileStream.pipe = (res)=>
  50. res.should.equal @res
  51. done()
  52. @controller.getFile @req, @res
  53. it "should send a 200 if the cacheWarm param is true", (done)->
  54. @req.params.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 s3Wrapper", (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 s3Wrapper", (done)->
  82. @s3Wrapper.copyFile.callsArgWith(3)
  83. @res.send = (code)=>
  84. code.should.equal 200
  85. @s3Wrapper.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. @s3Wrapper.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