LeaveProjectTests.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-throw-literal,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const RealTimeClient = require("./helpers/RealTimeClient");
  15. const MockDocUpdaterServer = require("./helpers/MockDocUpdaterServer");
  16. const FixturesManager = require("./helpers/FixturesManager");
  17. const async = require("async");
  18. const settings = require("settings-sharelatex");
  19. const redis = require("redis-sharelatex");
  20. const rclient = redis.createClient(settings.redis.pubsub);
  21. describe("leaveProject", function() {
  22. before(function(done) { return MockDocUpdaterServer.run(done); });
  23. describe("with other clients in the project", function() {
  24. before(function(done) {
  25. return async.series([
  26. cb => {
  27. return FixturesManager.setUpProject({
  28. privilegeLevel: "owner",
  29. project: {
  30. name: "Test Project"
  31. }
  32. }, (e, {project_id, user_id}) => { this.project_id = project_id; this.user_id = user_id; return cb(); });
  33. },
  34. cb => {
  35. this.clientA = RealTimeClient.connect();
  36. return this.clientA.on("connectionAccepted", cb);
  37. },
  38. cb => {
  39. this.clientB = RealTimeClient.connect();
  40. this.clientB.on("connectionAccepted", cb);
  41. this.clientBDisconnectMessages = [];
  42. return this.clientB.on("clientTracking.clientDisconnected", data => {
  43. return this.clientBDisconnectMessages.push(data);
  44. });
  45. },
  46. cb => {
  47. return this.clientA.emit("joinProject", {project_id: this.project_id}, (error, project, privilegeLevel, protocolVersion) => {
  48. this.project = project;
  49. this.privilegeLevel = privilegeLevel;
  50. this.protocolVersion = protocolVersion;
  51. return cb(error);
  52. });
  53. },
  54. cb => {
  55. return this.clientB.emit("joinProject", {project_id: this.project_id}, (error, project, privilegeLevel, protocolVersion) => {
  56. this.project = project;
  57. this.privilegeLevel = privilegeLevel;
  58. this.protocolVersion = protocolVersion;
  59. return cb(error);
  60. });
  61. },
  62. cb => {
  63. return FixturesManager.setUpDoc(this.project_id, {lines: this.lines, version: this.version, ops: this.ops}, (e, {doc_id}) => {
  64. this.doc_id = doc_id;
  65. return cb(e);
  66. });
  67. },
  68. cb => {
  69. return this.clientA.emit("joinDoc", this.doc_id, cb);
  70. },
  71. cb => {
  72. return this.clientB.emit("joinDoc", this.doc_id, cb);
  73. },
  74. cb => {
  75. // leaveProject is called when the client disconnects
  76. this.clientA.on("disconnect", () => cb());
  77. return this.clientA.disconnect();
  78. },
  79. cb => {
  80. // The API waits a little while before flushing changes
  81. return setTimeout(done, 1000);
  82. }
  83. ], done);
  84. });
  85. it("should emit a disconnect message to the room", function() {
  86. return this.clientBDisconnectMessages.should.deep.equal([this.clientA.publicId]);
  87. });
  88. it("should no longer list the client in connected users", function(done) {
  89. return this.clientB.emit("clientTracking.getConnectedUsers", (error, users) => {
  90. for (const user of Array.from(users)) {
  91. if (user.client_id === this.clientA.publicId) {
  92. throw "Expected clientA to not be listed in connected users";
  93. }
  94. }
  95. return done();
  96. });
  97. });
  98. it("should not flush the project to the document updater", function() {
  99. return MockDocUpdaterServer.deleteProject
  100. .calledWith(this.project_id)
  101. .should.equal(false);
  102. });
  103. it("should remain subscribed to the editor-events channels", function(done) {
  104. rclient.pubsub('CHANNELS', (err, resp) => {
  105. if (err) { return done(err); }
  106. resp.should.include(`editor-events:${this.project_id}`);
  107. return done();
  108. });
  109. return null;
  110. });
  111. return it("should remain subscribed to the applied-ops channels", function(done) {
  112. rclient.pubsub('CHANNELS', (err, resp) => {
  113. if (err) { return done(err); }
  114. resp.should.include(`applied-ops:${this.doc_id}`);
  115. return done();
  116. });
  117. return null;
  118. });
  119. });
  120. return describe("with no other clients in the project", function() {
  121. before(function(done) {
  122. return async.series([
  123. cb => {
  124. return FixturesManager.setUpProject({
  125. privilegeLevel: "owner",
  126. project: {
  127. name: "Test Project"
  128. }
  129. }, (e, {project_id, user_id}) => { this.project_id = project_id; this.user_id = user_id; return cb(); });
  130. },
  131. cb => {
  132. this.clientA = RealTimeClient.connect();
  133. return this.clientA.on("connect", cb);
  134. },
  135. cb => {
  136. return this.clientA.emit("joinProject", {project_id: this.project_id}, (error, project, privilegeLevel, protocolVersion) => {
  137. this.project = project;
  138. this.privilegeLevel = privilegeLevel;
  139. this.protocolVersion = protocolVersion;
  140. return cb(error);
  141. });
  142. },
  143. cb => {
  144. return FixturesManager.setUpDoc(this.project_id, {lines: this.lines, version: this.version, ops: this.ops}, (e, {doc_id}) => {
  145. this.doc_id = doc_id;
  146. return cb(e);
  147. });
  148. },
  149. cb => {
  150. return this.clientA.emit("joinDoc", this.doc_id, cb);
  151. },
  152. cb => {
  153. // leaveProject is called when the client disconnects
  154. this.clientA.on("disconnect", () => cb());
  155. return this.clientA.disconnect();
  156. },
  157. cb => {
  158. // The API waits a little while before flushing changes
  159. return setTimeout(done, 1000);
  160. }
  161. ], done);
  162. });
  163. it("should flush the project to the document updater", function() {
  164. return MockDocUpdaterServer.deleteProject
  165. .calledWith(this.project_id)
  166. .should.equal(true);
  167. });
  168. it("should not subscribe to the editor-events channels anymore", function(done) {
  169. rclient.pubsub('CHANNELS', (err, resp) => {
  170. if (err) { return done(err); }
  171. resp.should.not.include(`editor-events:${this.project_id}`);
  172. return done();
  173. });
  174. return null;
  175. });
  176. return it("should not subscribe to the applied-ops channels anymore", function(done) {
  177. rclient.pubsub('CHANNELS', (err, resp) => {
  178. if (err) { return done(err); }
  179. resp.should.not.include(`applied-ops:${this.doc_id}`);
  180. return done();
  181. });
  182. return null;
  183. });
  184. });
  185. });