ConnectedUsersManagerTests.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  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 should = require('chai').should();
  15. const SandboxedModule = require('sandboxed-module');
  16. const assert = require('assert');
  17. const path = require('path');
  18. const sinon = require('sinon');
  19. const modulePath = path.join(__dirname, "../../../app/js/ConnectedUsersManager");
  20. const {
  21. expect
  22. } = require("chai");
  23. const tk = require("timekeeper");
  24. describe("ConnectedUsersManager", function() {
  25. beforeEach(function() {
  26. this.settings = {
  27. redis: {
  28. realtime: {
  29. key_schema: {
  30. clientsInProject({project_id}) { return `clients_in_project:${project_id}`; },
  31. connectedUser({project_id, client_id}){ return `connected_user:${project_id}:${client_id}`; }
  32. }
  33. }
  34. }
  35. };
  36. this.rClient = {
  37. auth() {},
  38. setex:sinon.stub(),
  39. sadd:sinon.stub(),
  40. get: sinon.stub(),
  41. srem:sinon.stub(),
  42. del:sinon.stub(),
  43. smembers:sinon.stub(),
  44. expire:sinon.stub(),
  45. hset:sinon.stub(),
  46. hgetall:sinon.stub(),
  47. exec:sinon.stub(),
  48. multi: () => { return this.rClient; }
  49. };
  50. tk.freeze(new Date());
  51. this.ConnectedUsersManager = SandboxedModule.require(modulePath, { requires: {
  52. "settings-sharelatex":this.settings,
  53. "logger-sharelatex": { log() {}
  54. },
  55. "redis-sharelatex": { createClient:() => {
  56. return this.rClient;
  57. }
  58. }
  59. }
  60. }
  61. );
  62. this.client_id = "32132132";
  63. this.project_id = "dskjh2u21321";
  64. this.user = {
  65. _id: "user-id-123",
  66. first_name: "Joe",
  67. last_name: "Bloggs",
  68. email: "joe@example.com"
  69. };
  70. return this.cursorData = { row: 12, column: 9, doc_id: '53c3b8c85fee64000023dc6e' };});
  71. afterEach(function() { return tk.reset(); });
  72. describe("updateUserPosition", function() {
  73. beforeEach(function() {
  74. return this.rClient.exec.callsArgWith(0);
  75. });
  76. it("should set a key with the date and give it a ttl", function(done){
  77. return this.ConnectedUsersManager.updateUserPosition(this.project_id, this.client_id, this.user, null, err=> {
  78. this.rClient.hset.calledWith(`connected_user:${this.project_id}:${this.client_id}`, "last_updated_at", Date.now()).should.equal(true);
  79. return done();
  80. });
  81. });
  82. it("should set a key with the user_id", function(done){
  83. return this.ConnectedUsersManager.updateUserPosition(this.project_id, this.client_id, this.user, null, err=> {
  84. this.rClient.hset.calledWith(`connected_user:${this.project_id}:${this.client_id}`, "user_id", this.user._id).should.equal(true);
  85. return done();
  86. });
  87. });
  88. it("should set a key with the first_name", function(done){
  89. return this.ConnectedUsersManager.updateUserPosition(this.project_id, this.client_id, this.user, null, err=> {
  90. this.rClient.hset.calledWith(`connected_user:${this.project_id}:${this.client_id}`, "first_name", this.user.first_name).should.equal(true);
  91. return done();
  92. });
  93. });
  94. it("should set a key with the last_name", function(done){
  95. return this.ConnectedUsersManager.updateUserPosition(this.project_id, this.client_id, this.user, null, err=> {
  96. this.rClient.hset.calledWith(`connected_user:${this.project_id}:${this.client_id}`, "last_name", this.user.last_name).should.equal(true);
  97. return done();
  98. });
  99. });
  100. it("should set a key with the email", function(done){
  101. return this.ConnectedUsersManager.updateUserPosition(this.project_id, this.client_id, this.user, null, err=> {
  102. this.rClient.hset.calledWith(`connected_user:${this.project_id}:${this.client_id}`, "email", this.user.email).should.equal(true);
  103. return done();
  104. });
  105. });
  106. it("should push the client_id on to the project list", function(done){
  107. return this.ConnectedUsersManager.updateUserPosition(this.project_id, this.client_id, this.user, null, err=> {
  108. this.rClient.sadd.calledWith(`clients_in_project:${this.project_id}`, this.client_id).should.equal(true);
  109. return done();
  110. });
  111. });
  112. it("should add a ttl to the project set so it stays clean", function(done){
  113. return this.ConnectedUsersManager.updateUserPosition(this.project_id, this.client_id, this.user, null, err=> {
  114. this.rClient.expire.calledWith(`clients_in_project:${this.project_id}`, 24 * 4 * 60 * 60).should.equal(true);
  115. return done();
  116. });
  117. });
  118. it("should add a ttl to the connected user so it stays clean", function(done) {
  119. return this.ConnectedUsersManager.updateUserPosition(this.project_id, this.client_id, this.user, null, err=> {
  120. this.rClient.expire.calledWith(`connected_user:${this.project_id}:${this.client_id}`, 60 * 15).should.equal(true);
  121. return done();
  122. });
  123. });
  124. return it("should set the cursor position when provided", function(done){
  125. return this.ConnectedUsersManager.updateUserPosition(this.project_id, this.client_id, this.user, this.cursorData, err=> {
  126. this.rClient.hset.calledWith(`connected_user:${this.project_id}:${this.client_id}`, "cursorData", JSON.stringify(this.cursorData)).should.equal(true);
  127. return done();
  128. });
  129. });
  130. });
  131. describe("markUserAsDisconnected", function() {
  132. beforeEach(function() {
  133. return this.rClient.exec.callsArgWith(0);
  134. });
  135. it("should remove the user from the set", function(done){
  136. return this.ConnectedUsersManager.markUserAsDisconnected(this.project_id, this.client_id, err=> {
  137. this.rClient.srem.calledWith(`clients_in_project:${this.project_id}`, this.client_id).should.equal(true);
  138. return done();
  139. });
  140. });
  141. it("should delete the connected_user string", function(done){
  142. return this.ConnectedUsersManager.markUserAsDisconnected(this.project_id, this.client_id, err=> {
  143. this.rClient.del.calledWith(`connected_user:${this.project_id}:${this.client_id}`).should.equal(true);
  144. return done();
  145. });
  146. });
  147. return it("should add a ttl to the connected user set so it stays clean", function(done){
  148. return this.ConnectedUsersManager.markUserAsDisconnected(this.project_id, this.client_id, err=> {
  149. this.rClient.expire.calledWith(`clients_in_project:${this.project_id}`, 24 * 4 * 60 * 60).should.equal(true);
  150. return done();
  151. });
  152. });
  153. });
  154. describe("_getConnectedUser", function() {
  155. it("should return a connected user if there is a user object", function(done){
  156. const cursorData = JSON.stringify({cursorData:{row:1}});
  157. this.rClient.hgetall.callsArgWith(1, null, {connected_at:new Date(), user_id: this.user._id, last_updated_at: `${Date.now()}`, cursorData});
  158. return this.ConnectedUsersManager._getConnectedUser(this.project_id, this.client_id, (err, result)=> {
  159. result.connected.should.equal(true);
  160. result.client_id.should.equal(this.client_id);
  161. return done();
  162. });
  163. });
  164. it("should return a not connected user if there is no object", function(done){
  165. this.rClient.hgetall.callsArgWith(1, null, null);
  166. return this.ConnectedUsersManager._getConnectedUser(this.project_id, this.client_id, (err, result)=> {
  167. result.connected.should.equal(false);
  168. result.client_id.should.equal(this.client_id);
  169. return done();
  170. });
  171. });
  172. return it("should return a not connected user if there is an empty object", function(done){
  173. this.rClient.hgetall.callsArgWith(1, null, {});
  174. return this.ConnectedUsersManager._getConnectedUser(this.project_id, this.client_id, (err, result)=> {
  175. result.connected.should.equal(false);
  176. result.client_id.should.equal(this.client_id);
  177. return done();
  178. });
  179. });
  180. });
  181. return describe("getConnectedUsers", function() {
  182. beforeEach(function() {
  183. this.users = ["1234", "5678", "9123", "8234"];
  184. this.rClient.smembers.callsArgWith(1, null, this.users);
  185. this.ConnectedUsersManager._getConnectedUser = sinon.stub();
  186. this.ConnectedUsersManager._getConnectedUser.withArgs(this.project_id, this.users[0]).callsArgWith(2, null, {connected:true, client_age: 2, client_id:this.users[0]});
  187. this.ConnectedUsersManager._getConnectedUser.withArgs(this.project_id, this.users[1]).callsArgWith(2, null, {connected:false, client_age: 1, client_id:this.users[1]});
  188. this.ConnectedUsersManager._getConnectedUser.withArgs(this.project_id, this.users[2]).callsArgWith(2, null, {connected:true, client_age: 3, client_id:this.users[2]});
  189. return this.ConnectedUsersManager._getConnectedUser.withArgs(this.project_id, this.users[3]).callsArgWith(2, null, {connected:true, client_age: 11, client_id:this.users[3]});
  190. }); // connected but old
  191. return it("should only return the users in the list which are still in redis and recently updated", function(done){
  192. return this.ConnectedUsersManager.getConnectedUsers(this.project_id, (err, users)=> {
  193. users.length.should.equal(2);
  194. users[0].should.deep.equal({client_id:this.users[0], client_age: 2, connected:true});
  195. users[1].should.deep.equal({client_id:this.users[2], client_age: 3, connected:true});
  196. return done();
  197. });
  198. });
  199. });
  200. });