AppendingUpdatesTests.coffee 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. sinon = require "sinon"
  2. chai = require("chai")
  3. chai.should()
  4. mongojs = require "../../../app/js/mongojs"
  5. db = mongojs.db
  6. ObjectId = mongojs.ObjectId
  7. Settings = require "settings-sharelatex"
  8. request = require "request"
  9. describe "Appending doc ops to the history", ->
  10. describe "when the history does not exist yet", ->
  11. before (done) ->
  12. @doc_id = ObjectId().toString()
  13. @user_id = ObjectId().toString()
  14. updates = [{
  15. op: [{ i: "f", p: 3 }]
  16. meta: { ts: Date.now(), user_id: @user_id }
  17. }, {
  18. op: [{ i: "o", p: 4 }]
  19. meta: { ts: Date.now(), user_id: @user_id }
  20. }, {
  21. op: [{ i: "o", p: 5 }]
  22. meta: { ts: Date.now(), user_id: @user_id }
  23. }]
  24. @version = 3
  25. request.post {
  26. url: "http://localhost:#{Settings.port}/doc/#{@doc_id}/history"
  27. json:
  28. version: @version
  29. docOps: updates
  30. }, (@error, @response, @body) =>
  31. done()
  32. it "should return a successful response", ->
  33. @response.statusCode.should.equal 204
  34. describe "when the history has already been started", ->
  35. beforeEach (done) ->
  36. @doc_id = ObjectId().toString()
  37. @user_id = ObjectId().toString()
  38. updates = [{
  39. op: [{ i: "f", p: 3 }]
  40. meta: { ts: Date.now(), user_id: @user_id }
  41. }, {
  42. op: [{ i: "o", p: 4 }]
  43. meta: { ts: Date.now(), user_id: @user_id }
  44. }, {
  45. op: [{ i: "o", p: 5 }]
  46. meta: { ts: Date.now(), user_id: @user_id }
  47. }]
  48. @version = 3
  49. request.post {
  50. url: "http://localhost:#{Settings.port}/doc/#{@doc_id}/history"
  51. json:
  52. version: @version
  53. docOps: updates
  54. }, (@error, @response, @body) =>
  55. done()
  56. describe "when the updates are recent and from the same user", ->
  57. beforeEach (done) ->
  58. updates = [{
  59. op: [{ i: "b", p: 6 }]
  60. meta: { ts: Date.now(), user_id: @user_id }
  61. }, {
  62. op: [{ i: "a", p: 7 }]
  63. meta: { ts: Date.now(), user_id: @user_id }
  64. }, {
  65. op: [{ i: "r", p: 8 }]
  66. meta: { ts: Date.now(), user_id: @user_id }
  67. }]
  68. @version = 6
  69. request.post {
  70. url: "http://localhost:#{Settings.port}/doc/#{@doc_id}/history"
  71. json:
  72. version: @version
  73. docOps: updates
  74. }, (@error, @response, @body) =>
  75. done()
  76. it "should return a successful response", ->
  77. @response.statusCode.should.equal 204
  78. describe "when the updates are far apart", ->
  79. beforeEach (done) ->
  80. oneDay = 24 * 60 * 60 * 1000
  81. updates = [{
  82. op: [{ i: "b", p: 6 }]
  83. meta: { ts: Date.now() + oneDay, user_id: @user_id }
  84. }, {
  85. op: [{ i: "a", p: 7 }]
  86. meta: { ts: Date.now() + oneDay, user_id: @user_id }
  87. }, {
  88. op: [{ i: "r", p: 8 }]
  89. meta: { ts: Date.now() + oneDay, user_id: @user_id }
  90. }]
  91. @version = 6
  92. request.post {
  93. url: "http://localhost:#{Settings.port}/doc/#{@doc_id}/history"
  94. json:
  95. version: @version
  96. docOps: updates
  97. }, (@error, @response, @body) =>
  98. done()
  99. it "should return a successful response", ->
  100. @response.statusCode.should.equal 204