SettingADocumentTests.coffee 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. sinon = require "sinon"
  2. chai = require("chai")
  3. chai.should()
  4. expect = require("chai").expect
  5. Settings = require('settings-sharelatex')
  6. rclient = require("redis-sharelatex").createClient(Settings.redis.web)
  7. MockTrackChangesApi = require "./helpers/MockTrackChangesApi"
  8. MockWebApi = require "./helpers/MockWebApi"
  9. DocUpdaterClient = require "./helpers/DocUpdaterClient"
  10. describe "Setting a document", ->
  11. before ->
  12. @lines = ["one", "two", "three"]
  13. @version = 42
  14. @update =
  15. doc: @doc_id
  16. op: [{
  17. i: "one and a half\n"
  18. p: 4
  19. }]
  20. v: @version
  21. @result = ["one", "one and a half", "two", "three"]
  22. @newLines = ["these", "are", "the", "new", "lines"]
  23. @source = "dropbox"
  24. @user_id = "user-id-123"
  25. sinon.spy MockTrackChangesApi, "flushDoc"
  26. sinon.spy MockWebApi, "setDocument"
  27. after ->
  28. MockWebApi.setDocument.restore()
  29. MockTrackChangesApi.flushDoc.restore()
  30. describe "when the updated doc exists in the doc updater", ->
  31. before (done) ->
  32. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  33. MockWebApi.insertDoc @project_id, @doc_id, lines: @lines, version: @version
  34. DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) =>
  35. throw error if error?
  36. DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) =>
  37. throw error if error?
  38. setTimeout () =>
  39. DocUpdaterClient.setDocLines @project_id, @doc_id, @newLines, @source, @user_id, (error, res, body) =>
  40. @statusCode = res.statusCode
  41. done()
  42. , 200
  43. it "should return a 204 status code", ->
  44. @statusCode.should.equal 204
  45. it "should send the updated doc lines and version to the web api", ->
  46. MockWebApi.setDocument
  47. .calledWith(@project_id, @doc_id, @newLines)
  48. .should.equal true
  49. it "should update the lines in the doc updater", (done) ->
  50. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  51. doc.lines.should.deep.equal @newLines
  52. done()
  53. it "should bump the version in the doc updater", (done) ->
  54. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  55. doc.version.should.equal @version + 2
  56. done()
  57. it "should leave the document in redis", (done) ->
  58. rclient.get "doclines:#{@doc_id}", (error, lines) =>
  59. throw error if error?
  60. expect(JSON.parse(lines)).to.deep.equal @newLines
  61. done()
  62. describe "when the updated doc does not exist in the doc updater", ->
  63. before (done) ->
  64. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  65. MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
  66. DocUpdaterClient.setDocLines @project_id, @doc_id, @newLines, @source, @user_id, (error, res, body) =>
  67. @statusCode = res.statusCode
  68. setTimeout done, 200
  69. it "should return a 204 status code", ->
  70. @statusCode.should.equal 204
  71. it "should send the updated doc lines to the web api", ->
  72. MockWebApi.setDocument
  73. .calledWith(@project_id, @doc_id, @newLines)
  74. .should.equal true
  75. it "should flush track changes", ->
  76. MockTrackChangesApi.flushDoc.calledWith(@doc_id).should.equal true
  77. it "should remove the document from redis", (done) ->
  78. rclient.get "doclines:#{@doc_id}", (error, lines) =>
  79. throw error if error?
  80. expect(lines).to.not.exist
  81. done()