JoinDocTests.coffee 5.0 KB

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