setDocTests.coffee 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. params:
  25. project_id: @project_id
  26. doc_id: @doc_id
  27. body:
  28. lines: @lines
  29. source: @source
  30. user_id: @user_id
  31. @next = sinon.stub()
  32. describe "successfully", ->
  33. beforeEach ->
  34. @DocumentManager.setDocWithLock = sinon.stub().callsArgWith(5)
  35. @HttpController.setDoc(@req, @res, @next)
  36. it "should set the doc", ->
  37. @DocumentManager.setDocWithLock
  38. .calledWith(@project_id, @doc_id, @lines, @source, @user_id)
  39. .should.equal true
  40. it "should return a successful No Content response", ->
  41. @res.send
  42. .calledWith(204)
  43. .should.equal true
  44. it "should log the request", ->
  45. @logger.log
  46. .calledWith(doc_id: @doc_id, project_id: @project_id, lines: @lines, source: @source, user_id: @user_id, "setting doc via http")
  47. .should.equal true
  48. it "should time the request", ->
  49. @Metrics.Timer::done.called.should.equal true
  50. describe "when an errors occurs", ->
  51. beforeEach ->
  52. @DocumentManager.setDocWithLock = sinon.stub().callsArgWith(5, new Error("oops"))
  53. @HttpController.setDoc(@req, @res, @next)
  54. it "should call next with the error", ->
  55. @next
  56. .calledWith(new Error("oops"))
  57. .should.equal true