setDocTests.coffee 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/HttpController.js"
  5. SandboxedModule = require('sandboxed-module')
  6. Errors = require "../../../../app/js/Errors.js"
  7. describe "HttpController.setDoc", ->
  8. beforeEach ->
  9. @HttpController = SandboxedModule.require modulePath, requires:
  10. "./DocumentManager": @DocumentManager = {}
  11. "./ProjectManager": {}
  12. "logger-sharelatex" : @logger = { log: sinon.stub() }
  13. "./Metrics": @Metrics = {}
  14. @Metrics.Timer = class Timer
  15. done: sinon.stub()
  16. @project_id = "project-id-123"
  17. @doc_id = "doc-id-123"
  18. @lines = ["one", "two", "three"]
  19. @source = "dropbox"
  20. @user_id = "user-id-123"
  21. @res =
  22. send: sinon.stub()
  23. @req =
  24. headers: {}
  25. params:
  26. project_id: @project_id
  27. doc_id: @doc_id
  28. body:
  29. lines: @lines
  30. source: @source
  31. user_id: @user_id
  32. @next = sinon.stub()
  33. describe "successfully", ->
  34. beforeEach ->
  35. @DocumentManager.setDocWithLock = sinon.stub().callsArgWith(5)
  36. @HttpController.setDoc(@req, @res, @next)
  37. it "should set the doc", ->
  38. @DocumentManager.setDocWithLock
  39. .calledWith(@project_id, @doc_id, @lines, @source, @user_id)
  40. .should.equal true
  41. it "should return a successful No Content response", ->
  42. @res.send
  43. .calledWith(204)
  44. .should.equal true
  45. it "should log the request", ->
  46. @logger.log
  47. .calledWith(doc_id: @doc_id, project_id: @project_id, lines: @lines, source: @source, user_id: @user_id, "setting doc via http")
  48. .should.equal true
  49. it "should time the request", ->
  50. @Metrics.Timer::done.called.should.equal true
  51. describe "when an errors occurs", ->
  52. beforeEach ->
  53. @DocumentManager.setDocWithLock = sinon.stub().callsArgWith(5, new Error("oops"))
  54. @HttpController.setDoc(@req, @res, @next)
  55. it "should call next with the error", ->
  56. @next
  57. .calledWith(new Error("oops"))
  58. .should.equal true
  59. describe "when the payload is too large", ->
  60. beforeEach ->
  61. lines = []
  62. for _ in [0..200000]
  63. lines.push "test test test"
  64. @req.body.lines = lines
  65. @DocumentManager.setDocWithLock = sinon.stub().callsArgWith(5)
  66. @HttpController.setDoc(@req, @res, @next)
  67. it 'should send back a 406 response', ->
  68. @res.send.calledWith(406).should.equal true
  69. it 'should not call setDocWithLock', ->
  70. @DocumentManager.setDocWithLock.callCount.should.equal 0