SettingADocumentTests.coffee 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. sinon = require "sinon"
  2. chai = require("chai")
  3. chai.should()
  4. expect = require("chai").expect
  5. {db, ObjectId} = require "../../../app/js/mongojs"
  6. rclient = require("redis").createClient()
  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, "setDocumentLines"
  27. after ->
  28. MockWebApi.setDocumentLines.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
  34. db.docOps.insert {
  35. doc_id: ObjectId(@doc_id)
  36. version: @version
  37. }, (error) =>
  38. throw error if error?
  39. DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) =>
  40. throw error if error?
  41. DocUpdaterClient.sendUpdate @project_id, @doc_id, @update, (error) =>
  42. throw error if error?
  43. setTimeout () =>
  44. DocUpdaterClient.setDocLines @project_id, @doc_id, @newLines, @source, @user_id, (error, res, body) =>
  45. @statusCode = res.statusCode
  46. done()
  47. , 200
  48. it "should return a 204 status code", ->
  49. @statusCode.should.equal 204
  50. it "should send the updated doc lines to the web api", ->
  51. MockWebApi.setDocumentLines
  52. .calledWith(@project_id, @doc_id, @newLines)
  53. .should.equal true
  54. it "should update the lines in the doc updater", (done) ->
  55. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  56. doc.lines.should.deep.equal @newLines
  57. done()
  58. it "should bump the version in the doc updater", (done) ->
  59. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  60. doc.version.should.equal @version + 2
  61. done()
  62. it "should leave the document in redis", (done) ->
  63. rclient.get "doclines:#{@doc_id}", (error, lines) =>
  64. throw error if error?
  65. expect(JSON.parse(lines)).to.deep.equal @newLines
  66. done()
  67. describe "when the updated doc does not exist in the doc updater", ->
  68. before (done) ->
  69. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  70. MockWebApi.insertDoc @project_id, @doc_id, lines: @lines
  71. db.docOps.insert {
  72. doc_id: ObjectId(@doc_id)
  73. version: @version
  74. }, (error) =>
  75. throw error if error?
  76. DocUpdaterClient.setDocLines @project_id, @doc_id, @newLines, @source, @user_id, (error, res, body) =>
  77. @statusCode = res.statusCode
  78. setTimeout done, 200
  79. it "should return a 204 status code", ->
  80. @statusCode.should.equal 204
  81. it "should send the updated doc lines to the web api", ->
  82. MockWebApi.setDocumentLines
  83. .calledWith(@project_id, @doc_id, @newLines)
  84. .should.equal true
  85. it "should flush track changes", ->
  86. MockTrackChangesApi.flushDoc.calledWith(@doc_id).should.equal true
  87. it "should remove the document from redis", (done) ->
  88. rclient.get "doclines:#{@doc_id}", (error, lines) =>
  89. throw error if error?
  90. expect(lines).to.not.exist
  91. done()