JoinDocTests.coffee 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. chai = require("chai")
  2. expect = chai.expect
  3. chai.should()
  4. RealTimeClient = require "./helpers/RealTimeClient"
  5. MockDocUpdaterServer = require "./helpers/MockDocUpdaterServer"
  6. FixturesManager = require "./helpers/FixturesManager"
  7. async = require "async"
  8. describe "joinDoc", ->
  9. before ->
  10. @lines = ["test", "doc", "lines"]
  11. @version = 42
  12. @ops = ["mock", "doc", "ops"]
  13. describe "when authorised readAndWrite", ->
  14. before (done) ->
  15. async.series [
  16. (cb) =>
  17. FixturesManager.setUpProject {
  18. privilegeLevel: "readAndWrite"
  19. }, (e, {@project_id, @user_id}) =>
  20. cb(e)
  21. (cb) =>
  22. FixturesManager.setUpDoc @project_id, {@lines, @version, @ops}, (e, {@doc_id}) =>
  23. cb(e)
  24. (cb) =>
  25. @client = RealTimeClient.connect()
  26. @client.on "connect", cb
  27. (cb) =>
  28. @client.emit "joinProject", project_id: @project_id, cb
  29. (cb) =>
  30. @client.emit "joinDoc", @doc_id, (error, @returnedArgs...) => cb(error)
  31. ], done
  32. it "should get the doc from the doc updater", ->
  33. MockDocUpdaterServer.getDocument
  34. .calledWith(@project_id, @doc_id, -1)
  35. .should.equal true
  36. it "should return the doc lines, version and ops", ->
  37. @returnedArgs.should.deep.equal [@lines, @version, @ops]
  38. it "should have joined the doc room", (done) ->
  39. RealTimeClient.getConnectedClient @client.socket.sessionid, (error, client) =>
  40. expect(@doc_id in client.rooms).to.equal true
  41. done()
  42. describe "when authorised readOnly", ->
  43. before (done) ->
  44. async.series [
  45. (cb) =>
  46. FixturesManager.setUpProject {
  47. privilegeLevel: "readOnly"
  48. }, (e, {@project_id, @user_id}) =>
  49. cb(e)
  50. (cb) =>
  51. FixturesManager.setUpDoc @project_id, {@lines, @version, @ops}, (e, {@doc_id}) =>
  52. cb(e)
  53. (cb) =>
  54. @client = RealTimeClient.connect()
  55. @client.on "connect", cb
  56. (cb) =>
  57. @client.emit "joinProject", project_id: @project_id, cb
  58. (cb) =>
  59. @client.emit "joinDoc", @doc_id, (error, @returnedArgs...) => cb(error)
  60. ], done
  61. it "should get the doc from the doc updater", ->
  62. MockDocUpdaterServer.getDocument
  63. .calledWith(@project_id, @doc_id, -1)
  64. .should.equal true
  65. it "should return the doc lines, version and ops", ->
  66. @returnedArgs.should.deep.equal [@lines, @version, @ops]
  67. it "should have joined the doc room", (done) ->
  68. RealTimeClient.getConnectedClient @client.socket.sessionid, (error, client) =>
  69. expect(@doc_id in client.rooms).to.equal true
  70. done()
  71. describe "when authorised as owner", ->
  72. before (done) ->
  73. async.series [
  74. (cb) =>
  75. FixturesManager.setUpProject {
  76. privilegeLevel: "owner"
  77. }, (e, {@project_id, @user_id}) =>
  78. cb(e)
  79. (cb) =>
  80. FixturesManager.setUpDoc @project_id, {@lines, @version, @ops}, (e, {@doc_id}) =>
  81. cb(e)
  82. (cb) =>
  83. @client = RealTimeClient.connect()
  84. @client.on "connect", cb
  85. (cb) =>
  86. @client.emit "joinProject", project_id: @project_id, cb
  87. (cb) =>
  88. @client.emit "joinDoc", @doc_id, (error, @returnedArgs...) => cb(error)
  89. ], done
  90. it "should get the doc from the doc updater", ->
  91. MockDocUpdaterServer.getDocument
  92. .calledWith(@project_id, @doc_id, -1)
  93. .should.equal true
  94. it "should return the doc lines, version and ops", ->
  95. @returnedArgs.should.deep.equal [@lines, @version, @ops]
  96. it "should have joined the doc room", (done) ->
  97. RealTimeClient.getConnectedClient @client.socket.sessionid, (error, client) =>
  98. expect(@doc_id in client.rooms).to.equal true
  99. done()
  100. # It is impossible to write an acceptance test to test joining an unauthorized
  101. # project, since joinProject already catches that. If you can join a project,
  102. # then you can join a doc in that project.
  103. describe "with a fromVersion", ->
  104. before (done) ->
  105. @fromVersion = 36
  106. async.series [
  107. (cb) =>
  108. FixturesManager.setUpProject {
  109. privilegeLevel: "readAndWrite"
  110. }, (e, {@project_id, @user_id}) =>
  111. cb(e)
  112. (cb) =>
  113. FixturesManager.setUpDoc @project_id, {@lines, @version, @ops}, (e, {@doc_id}) =>
  114. cb(e)
  115. (cb) =>
  116. @client = RealTimeClient.connect()
  117. @client.on "connect", cb
  118. (cb) =>
  119. @client.emit "joinProject", project_id: @project_id, cb
  120. (cb) =>
  121. @client.emit "joinDoc", @doc_id, @fromVersion, (error, @returnedArgs...) => cb(error)
  122. ], done
  123. it "should get the doc from the doc updater with the fromVersion", ->
  124. MockDocUpdaterServer.getDocument
  125. .calledWith(@project_id, @doc_id, @fromVersion)
  126. .should.equal true
  127. it "should return the doc lines, version and ops", ->
  128. @returnedArgs.should.deep.equal [@lines, @version, @ops]
  129. it "should have joined the doc room", (done) ->
  130. RealTimeClient.getConnectedClient @client.socket.sessionid, (error, client) =>
  131. expect(@doc_id in client.rooms).to.equal true
  132. done()