DocumentUpdaterManagerTests.coffee 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/DocumentUpdaterManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe "DocumentUpdaterManager", ->
  8. beforeEach ->
  9. @DocumentUpdaterManager = SandboxedModule.require modulePath, requires:
  10. "request": @request = {}
  11. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  12. 'settings-sharelatex': @settings =
  13. apis : documentupdater: url : "http://example.com"
  14. @callback = sinon.stub()
  15. @lines = ["one", "two", "three"]
  16. @version = 42
  17. describe "getDocument", ->
  18. describe "successfully", ->
  19. beforeEach ->
  20. @body = JSON.stringify
  21. lines: @lines
  22. version: @version
  23. ops: []
  24. @request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
  25. @DocumentUpdaterManager.getDocument @project_id, @doc_id, @callback
  26. it 'should get the document from the document updater', ->
  27. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}"
  28. @request.get.calledWith(url).should.equal true
  29. it "should call the callback with the content and version", ->
  30. @callback.calledWith(null, @lines.join("\n"), @version).should.equal true
  31. describe "when the document updater API returns an error", ->
  32. beforeEach ->
  33. @request.get = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  34. @DocumentUpdaterManager.getDocument @project_id, @doc_id, @callback
  35. it "should return an error to the callback", ->
  36. @callback.calledWith(@error).should.equal true
  37. describe "when the document updater returns a failure error code", ->
  38. beforeEach ->
  39. @request.get = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  40. @DocumentUpdaterManager.getDocument @project_id, @doc_id, @callback
  41. it "should return the callback with an error", ->
  42. @callback
  43. .calledWith(sinon.match.has('message', "doc updater returned a non-success status code: 500"))
  44. .should.equal true
  45. describe "setDocument", ->
  46. beforeEach ->
  47. @content = "mock content"
  48. @user_id = "user-id-123"
  49. describe "successfully", ->
  50. beforeEach ->
  51. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 200})
  52. @DocumentUpdaterManager.setDocument @project_id, @doc_id, @content, @user_id, @callback
  53. it 'should set the document in the document updater', ->
  54. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}"
  55. @request.post
  56. .calledWith({
  57. url: url
  58. json:
  59. lines: @content.split("\n")
  60. source: "restore"
  61. user_id: @user_id
  62. undoing: true
  63. }).should.equal true
  64. it "should call the callback", ->
  65. @callback.calledWith(null).should.equal true
  66. describe "when the document updater API returns an error", ->
  67. beforeEach ->
  68. @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  69. @DocumentUpdaterManager.setDocument @project_id, @doc_id, @content, @user_id, @callback
  70. it "should return an error to the callback", ->
  71. @callback.calledWith(@error).should.equal true
  72. describe "when the document updater returns a failure error code", ->
  73. beforeEach ->
  74. @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  75. @DocumentUpdaterManager.setDocument @project_id, @doc_id, @content, @user_id, @callback
  76. it "should return the callback with an error", ->
  77. @callback
  78. .calledWith(sinon.match.has('message', "doc updater returned a non-success status code: 500"))
  79. .should.equal true