GettingADocumentTests.coffee 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. sinon = require "sinon"
  2. chai = require("chai")
  3. chai.should()
  4. expect = chai.expect
  5. MockWebApi = require "./helpers/MockWebApi"
  6. DocUpdaterClient = require "./helpers/DocUpdaterClient"
  7. DocUpdaterApp = require "./helpers/DocUpdaterApp"
  8. describe "Getting a document", ->
  9. before (done) ->
  10. @lines = ["one", "two", "three"]
  11. @version = 42
  12. DocUpdaterApp.ensureRunning(done)
  13. describe "when the document is not loaded", ->
  14. before (done) ->
  15. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  16. sinon.spy MockWebApi, "getDocument"
  17. MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
  18. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, @returnedDoc) => done()
  19. after ->
  20. MockWebApi.getDocument.restore()
  21. it "should load the document from the web API", ->
  22. MockWebApi.getDocument
  23. .calledWith(@project_id, @doc_id)
  24. .should.equal true
  25. it "should return the document lines", ->
  26. @returnedDoc.lines.should.deep.equal @lines
  27. it "should return the document at its current version", ->
  28. @returnedDoc.version.should.equal @version
  29. describe "when the document is already loaded", ->
  30. before (done) ->
  31. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  32. MockWebApi.insertDoc @project_id, @doc_id, {lines: @lines, version: @version}
  33. DocUpdaterClient.preloadDoc @project_id, @doc_id, (error) =>
  34. throw error if error?
  35. sinon.spy MockWebApi, "getDocument"
  36. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, @returnedDoc) => done()
  37. after ->
  38. MockWebApi.getDocument.restore()
  39. it "should not load the document from the web API", ->
  40. MockWebApi.getDocument.called.should.equal false
  41. it "should return the document lines", ->
  42. @returnedDoc.lines.should.deep.equal @lines
  43. describe "when the request asks for some recent ops", ->
  44. before (done) ->
  45. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  46. MockWebApi.insertDoc @project_id, @doc_id, {
  47. lines: @lines = ["one", "two", "three"]
  48. }
  49. @updates = for v in [0..199]
  50. doc_id: @doc_id,
  51. op: [i: v.toString(), p: 0]
  52. v: v
  53. DocUpdaterClient.sendUpdates @project_id, @doc_id, @updates, (error) =>
  54. throw error if error?
  55. sinon.spy MockWebApi, "getDocument"
  56. done()
  57. after ->
  58. MockWebApi.getDocument.restore()
  59. describe "when the ops are loaded", ->
  60. before (done) ->
  61. DocUpdaterClient.getDocAndRecentOps @project_id, @doc_id, 190, (error, res, @returnedDoc) => done()
  62. it "should return the recent ops", ->
  63. @returnedDoc.ops.length.should.equal 10
  64. for update, i in @updates.slice(190, -1)
  65. @returnedDoc.ops[i].op.should.deep.equal update.op
  66. describe "when the ops are not all loaded", ->
  67. before (done) ->
  68. # We only track 100 ops
  69. DocUpdaterClient.getDocAndRecentOps @project_id, @doc_id, 10, (error, @res, @returnedDoc) => done()
  70. it "should return UnprocessableEntity", ->
  71. @res.statusCode.should.equal 422
  72. describe "when the document does not exist", ->
  73. before (done) ->
  74. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  75. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  76. @statusCode = res.statusCode
  77. done()
  78. it "should return 404", ->
  79. @statusCode.should.equal 404
  80. describe "when the web api returns an error", ->
  81. before (done) ->
  82. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  83. sinon.stub MockWebApi, "getDocument", (project_id, doc_id, callback = (error, doc) ->) ->
  84. callback new Error("oops")
  85. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  86. @statusCode = res.statusCode
  87. done()
  88. after ->
  89. MockWebApi.getDocument.restore()
  90. it "should return 500", ->
  91. @statusCode.should.equal 500
  92. describe "when the web api http request takes a long time", ->
  93. before (done) ->
  94. @timeout = 10000
  95. [@project_id, @doc_id] = [DocUpdaterClient.randomId(), DocUpdaterClient.randomId()]
  96. sinon.stub MockWebApi, "getDocument", (project_id, doc_id, callback = (error, doc) ->) ->
  97. setTimeout callback, 30000
  98. done()
  99. after ->
  100. MockWebApi.getDocument.restore()
  101. it "should return quickly(ish)", (done) ->
  102. start = Date.now()
  103. DocUpdaterClient.getDoc @project_id, @doc_id, (error, res, doc) =>
  104. res.statusCode.should.equal 500
  105. delta = Date.now() - start
  106. expect(delta).to.be.below 20000
  107. done()