FileControllerTests.coffee 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. setHeader: ->
  44. @fileStream = {}
  45. describe "getFile", ->
  46. it "should pipe the stream", (done)->
  47. @FileHandler.getFile.callsArgWith(3, null, @fileStream)
  48. @fileStream.pipe = (res)=>
  49. res.should.equal @res
  50. done()
  51. @controller.getFile @req, @res
  52. it "should send a 200 if the cacheWarm param is true", (done)->
  53. @req.query.cacheWarm = true
  54. @FileHandler.getFile.callsArgWith(3, null, @fileStream)
  55. @res.send = (statusCode)=>
  56. statusCode.should.equal 200
  57. done()
  58. @controller.getFile @req, @res
  59. it "should send a 500 if there is a problem", (done)->
  60. @FileHandler.getFile.callsArgWith(3, "error")
  61. @res.send = (code)=>
  62. code.should.equal 500
  63. done()
  64. @controller.getFile @req, @res
  65. describe "insertFile", ->
  66. it "should send bucket name key and res to PersistorManager", (done)->
  67. @FileHandler.insertFile.callsArgWith(3)
  68. @res.send = =>
  69. @FileHandler.insertFile.calledWith(@bucket, @key, @req).should.equal true
  70. done()
  71. @controller.insertFile @req, @res
  72. describe "copyFile", ->
  73. beforeEach ->
  74. @oldFile_id = "old_file_id"
  75. @oldProject_id = "old_project_id"
  76. @req.body =
  77. source:
  78. project_id: @oldProject_id
  79. file_id: @oldFile_id
  80. it "should send bucket name and both keys to PersistorManager", (done)->
  81. @PersistorManager.copyFile.callsArgWith(3)
  82. @res.send = (code)=>
  83. code.should.equal 200
  84. @PersistorManager.copyFile.calledWith(@bucket, "#{@oldProject_id}/#{@oldFile_id}", @key).should.equal true
  85. done()
  86. @controller.copyFile @req, @res
  87. it "should send a 500 if there was an error", (done)->
  88. @PersistorManager.copyFile.callsArgWith(3, "error")
  89. @res.send = (code)=>
  90. code.should.equal 500
  91. done()
  92. @controller.copyFile @req, @res
  93. describe "delete file", ->
  94. it "should tell the file handler", (done)->
  95. @FileHandler.deleteFile.callsArgWith(2)
  96. @res.send = (code)=>
  97. code.should.equal 204
  98. @FileHandler.deleteFile.calledWith(@bucket, @key).should.equal true
  99. done()
  100. @controller.deleteFile @req, @res
  101. it "should send a 500 if there was an error", (done)->
  102. @FileHandler.deleteFile.callsArgWith(2, "error")
  103. @res.send = (code)->
  104. code.should.equal 500
  105. done()
  106. @controller.deleteFile @req, @res