ApplyUpdateTests.coffee 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. async = require "async"
  2. chai = require("chai")
  3. expect = chai.expect
  4. chai.should()
  5. RealTimeClient = require "./helpers/RealTimeClient"
  6. FixturesManager = require "./helpers/FixturesManager"
  7. settings = require "settings-sharelatex"
  8. redis = require "redis-sharelatex"
  9. rclient = redis.createClient(settings.redis.websessions)
  10. redisSettings = settings.redis
  11. describe "applyOtUpdate", ->
  12. before ->
  13. @update = {
  14. op: [{i: "foo", p: 42}]
  15. }
  16. describe "when authorized", ->
  17. before (done) ->
  18. async.series [
  19. (cb) =>
  20. FixturesManager.setUpProject {
  21. privilegeLevel: "readAndWrite"
  22. }, (e, {@project_id, @user_id}) =>
  23. cb(e)
  24. (cb) =>
  25. FixturesManager.setUpDoc @project_id, {@lines, @version, @ops}, (e, {@doc_id}) =>
  26. cb(e)
  27. (cb) =>
  28. @client = RealTimeClient.connect()
  29. @client.on "connectionAccepted", cb
  30. (cb) =>
  31. @client.emit "joinProject", project_id: @project_id, cb
  32. (cb) =>
  33. @client.emit "joinDoc", @doc_id, cb
  34. (cb) =>
  35. @client.emit "applyOtUpdate", @doc_id, @update, cb
  36. ], done
  37. it "should push the doc into the pending updates list", (done) ->
  38. rclient.lrange "pending-updates-list", 0, -1, (error, [doc_id]) =>
  39. doc_id.should.equal "#{@project_id}:#{@doc_id}"
  40. done()
  41. it "should push the update into redis", (done) ->
  42. rclient.lrange redisSettings.documentupdater.key_schema.pendingUpdates({@doc_id}), 0, -1, (error, [update]) =>
  43. update = JSON.parse(update)
  44. update.op.should.deep.equal @update.op
  45. update.meta.should.deep.equal {
  46. source: @client.socket.sessionid
  47. user_id: @user_id
  48. }
  49. done()
  50. after (done) ->
  51. async.series [
  52. (cb) => rclient.del "pending-updates-list", cb
  53. (cb) => rclient.del "DocsWithPendingUpdates", "#{@project_id}:#{@doc_id}", cb
  54. (cb) => rclient.del redisSettings.documentupdater.key_schema.pendingUpdates(@doc_id), cb
  55. ], done
  56. describe "when authorized to read-only with an edit update", ->
  57. before (done) ->
  58. async.series [
  59. (cb) =>
  60. FixturesManager.setUpProject {
  61. privilegeLevel: "readOnly"
  62. }, (e, {@project_id, @user_id}) =>
  63. cb(e)
  64. (cb) =>
  65. FixturesManager.setUpDoc @project_id, {@lines, @version, @ops}, (e, {@doc_id}) =>
  66. cb(e)
  67. (cb) =>
  68. @client = RealTimeClient.connect()
  69. @client.on "connectionAccepted", cb
  70. (cb) =>
  71. @client.emit "joinProject", project_id: @project_id, cb
  72. (cb) =>
  73. @client.emit "joinDoc", @doc_id, cb
  74. (cb) =>
  75. @client.emit "applyOtUpdate", @doc_id, @update, (@error) =>
  76. cb()
  77. ], done
  78. it "should return an error", ->
  79. expect(@error).to.exist
  80. it "should disconnect the client", (done) ->
  81. setTimeout () =>
  82. @client.socket.connected.should.equal false
  83. done()
  84. , 300
  85. it "should not put the update in redis", (done) ->
  86. rclient.llen redisSettings.documentupdater.key_schema.pendingUpdates({@doc_id}), (error, len) =>
  87. len.should.equal 0
  88. done()
  89. describe "when authorized to read-only with a comment update", ->
  90. before (done) ->
  91. @comment_update = {
  92. op: [{c: "foo", p: 42}]
  93. }
  94. async.series [
  95. (cb) =>
  96. FixturesManager.setUpProject {
  97. privilegeLevel: "readOnly"
  98. }, (e, {@project_id, @user_id}) =>
  99. cb(e)
  100. (cb) =>
  101. FixturesManager.setUpDoc @project_id, {@lines, @version, @ops}, (e, {@doc_id}) =>
  102. cb(e)
  103. (cb) =>
  104. @client = RealTimeClient.connect()
  105. @client.on "connectionAccepted", cb
  106. (cb) =>
  107. @client.emit "joinProject", project_id: @project_id, cb
  108. (cb) =>
  109. @client.emit "joinDoc", @doc_id, cb
  110. (cb) =>
  111. @client.emit "applyOtUpdate", @doc_id, @comment_update, cb
  112. ], done
  113. it "should push the doc into the pending updates list", (done) ->
  114. rclient.lrange "pending-updates-list", 0, -1, (error, [doc_id]) =>
  115. doc_id.should.equal "#{@project_id}:#{@doc_id}"
  116. done()
  117. it "should push the update into redis", (done) ->
  118. rclient.lrange redisSettings.documentupdater.key_schema.pendingUpdates({@doc_id}), 0, -1, (error, [update]) =>
  119. update = JSON.parse(update)
  120. update.op.should.deep.equal @comment_update.op
  121. update.meta.should.deep.equal {
  122. source: @client.socket.sessionid
  123. user_id: @user_id
  124. }
  125. done()
  126. after (done) ->
  127. async.series [
  128. (cb) => rclient.del "pending-updates-list", cb
  129. (cb) => rclient.del "DocsWithPendingUpdates", "#{@project_id}:#{@doc_id}", cb
  130. (cb) => rclient.del redisSettings.documentupdater.key_schema.pendingUpdates({@doc_id}), cb
  131. ], done