ConnectedUsersManagerTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* eslint-disable
  2. camelcase,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module')
  14. const assert = require('assert')
  15. const path = require('path')
  16. const sinon = require('sinon')
  17. const modulePath = path.join(__dirname, '../../../app/js/ConnectedUsersManager')
  18. const { expect } = require('chai')
  19. const tk = require('timekeeper')
  20. describe('ConnectedUsersManager', function () {
  21. beforeEach(function () {
  22. this.settings = {
  23. redis: {
  24. realtime: {
  25. key_schema: {
  26. clientsInProject({ project_id }) {
  27. return `clients_in_project:${project_id}`
  28. },
  29. connectedUser({ project_id, client_id }) {
  30. return `connected_user:${project_id}:${client_id}`
  31. },
  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: () => {
  49. return this.rClient
  50. },
  51. }
  52. tk.freeze(new Date())
  53. this.ConnectedUsersManager = SandboxedModule.require(modulePath, {
  54. requires: {
  55. '@overleaf/settings': this.settings,
  56. '@overleaf/redis-wrapper': {
  57. createClient: () => {
  58. return this.rClient
  59. },
  60. },
  61. },
  62. })
  63. this.client_id = '32132132'
  64. this.project_id = 'dskjh2u21321'
  65. this.user = {
  66. _id: 'user-id-123',
  67. first_name: 'Joe',
  68. last_name: 'Bloggs',
  69. email: 'joe@example.com',
  70. }
  71. return (this.cursorData = {
  72. row: 12,
  73. column: 9,
  74. doc_id: '53c3b8c85fee64000023dc6e',
  75. })
  76. })
  77. afterEach(function () {
  78. return tk.reset()
  79. })
  80. describe('updateUserPosition', function () {
  81. beforeEach(function () {
  82. return this.rClient.exec.callsArgWith(0)
  83. })
  84. it('should set a key with the date and give it a ttl', function (done) {
  85. return this.ConnectedUsersManager.updateUserPosition(
  86. this.project_id,
  87. this.client_id,
  88. this.user,
  89. null,
  90. err => {
  91. if (err) return done(err)
  92. this.rClient.hset
  93. .calledWith(
  94. `connected_user:${this.project_id}:${this.client_id}`,
  95. 'last_updated_at',
  96. Date.now()
  97. )
  98. .should.equal(true)
  99. return done()
  100. }
  101. )
  102. })
  103. it('should set a key with the user_id', function (done) {
  104. return this.ConnectedUsersManager.updateUserPosition(
  105. this.project_id,
  106. this.client_id,
  107. this.user,
  108. null,
  109. err => {
  110. if (err) return done(err)
  111. this.rClient.hset
  112. .calledWith(
  113. `connected_user:${this.project_id}:${this.client_id}`,
  114. 'user_id',
  115. this.user._id
  116. )
  117. .should.equal(true)
  118. return done()
  119. }
  120. )
  121. })
  122. it('should set a key with the first_name', function (done) {
  123. return this.ConnectedUsersManager.updateUserPosition(
  124. this.project_id,
  125. this.client_id,
  126. this.user,
  127. null,
  128. err => {
  129. if (err) return done(err)
  130. this.rClient.hset
  131. .calledWith(
  132. `connected_user:${this.project_id}:${this.client_id}`,
  133. 'first_name',
  134. this.user.first_name
  135. )
  136. .should.equal(true)
  137. return done()
  138. }
  139. )
  140. })
  141. it('should set a key with the last_name', function (done) {
  142. return this.ConnectedUsersManager.updateUserPosition(
  143. this.project_id,
  144. this.client_id,
  145. this.user,
  146. null,
  147. err => {
  148. if (err) return done(err)
  149. this.rClient.hset
  150. .calledWith(
  151. `connected_user:${this.project_id}:${this.client_id}`,
  152. 'last_name',
  153. this.user.last_name
  154. )
  155. .should.equal(true)
  156. return done()
  157. }
  158. )
  159. })
  160. it('should set a key with the email', function (done) {
  161. return this.ConnectedUsersManager.updateUserPosition(
  162. this.project_id,
  163. this.client_id,
  164. this.user,
  165. null,
  166. err => {
  167. if (err) return done(err)
  168. this.rClient.hset
  169. .calledWith(
  170. `connected_user:${this.project_id}:${this.client_id}`,
  171. 'email',
  172. this.user.email
  173. )
  174. .should.equal(true)
  175. return done()
  176. }
  177. )
  178. })
  179. it('should push the client_id on to the project list', function (done) {
  180. return this.ConnectedUsersManager.updateUserPosition(
  181. this.project_id,
  182. this.client_id,
  183. this.user,
  184. null,
  185. err => {
  186. if (err) return done(err)
  187. this.rClient.sadd
  188. .calledWith(`clients_in_project:${this.project_id}`, this.client_id)
  189. .should.equal(true)
  190. return done()
  191. }
  192. )
  193. })
  194. it('should add a ttl to the project set so it stays clean', function (done) {
  195. return this.ConnectedUsersManager.updateUserPosition(
  196. this.project_id,
  197. this.client_id,
  198. this.user,
  199. null,
  200. err => {
  201. if (err) return done(err)
  202. this.rClient.expire
  203. .calledWith(
  204. `clients_in_project:${this.project_id}`,
  205. 24 * 4 * 60 * 60
  206. )
  207. .should.equal(true)
  208. return done()
  209. }
  210. )
  211. })
  212. it('should add a ttl to the connected user so it stays clean', function (done) {
  213. return this.ConnectedUsersManager.updateUserPosition(
  214. this.project_id,
  215. this.client_id,
  216. this.user,
  217. null,
  218. err => {
  219. if (err) return done(err)
  220. this.rClient.expire
  221. .calledWith(
  222. `connected_user:${this.project_id}:${this.client_id}`,
  223. 60 * 15
  224. )
  225. .should.equal(true)
  226. return done()
  227. }
  228. )
  229. })
  230. return it('should set the cursor position when provided', function (done) {
  231. return this.ConnectedUsersManager.updateUserPosition(
  232. this.project_id,
  233. this.client_id,
  234. this.user,
  235. this.cursorData,
  236. err => {
  237. if (err) return done(err)
  238. this.rClient.hset
  239. .calledWith(
  240. `connected_user:${this.project_id}:${this.client_id}`,
  241. 'cursorData',
  242. JSON.stringify(this.cursorData)
  243. )
  244. .should.equal(true)
  245. return done()
  246. }
  247. )
  248. })
  249. })
  250. describe('markUserAsDisconnected', function () {
  251. beforeEach(function () {
  252. return this.rClient.exec.callsArgWith(0)
  253. })
  254. it('should remove the user from the set', function (done) {
  255. return this.ConnectedUsersManager.markUserAsDisconnected(
  256. this.project_id,
  257. this.client_id,
  258. err => {
  259. if (err) return done(err)
  260. this.rClient.srem
  261. .calledWith(`clients_in_project:${this.project_id}`, this.client_id)
  262. .should.equal(true)
  263. return done()
  264. }
  265. )
  266. })
  267. it('should delete the connected_user string', function (done) {
  268. return this.ConnectedUsersManager.markUserAsDisconnected(
  269. this.project_id,
  270. this.client_id,
  271. err => {
  272. if (err) return done(err)
  273. this.rClient.del
  274. .calledWith(`connected_user:${this.project_id}:${this.client_id}`)
  275. .should.equal(true)
  276. return done()
  277. }
  278. )
  279. })
  280. return it('should add a ttl to the connected user set so it stays clean', function (done) {
  281. return this.ConnectedUsersManager.markUserAsDisconnected(
  282. this.project_id,
  283. this.client_id,
  284. err => {
  285. if (err) return done(err)
  286. this.rClient.expire
  287. .calledWith(
  288. `clients_in_project:${this.project_id}`,
  289. 24 * 4 * 60 * 60
  290. )
  291. .should.equal(true)
  292. return done()
  293. }
  294. )
  295. })
  296. })
  297. describe('_getConnectedUser', function () {
  298. it('should return a connected user if there is a user object', function (done) {
  299. const cursorData = JSON.stringify({ cursorData: { row: 1 } })
  300. this.rClient.hgetall.callsArgWith(1, null, {
  301. connected_at: new Date(),
  302. user_id: this.user._id,
  303. last_updated_at: `${Date.now()}`,
  304. cursorData,
  305. })
  306. return this.ConnectedUsersManager._getConnectedUser(
  307. this.project_id,
  308. this.client_id,
  309. (err, result) => {
  310. if (err) return done(err)
  311. result.connected.should.equal(true)
  312. result.client_id.should.equal(this.client_id)
  313. return done()
  314. }
  315. )
  316. })
  317. it('should return a not connected user if there is no object', function (done) {
  318. this.rClient.hgetall.callsArgWith(1, null, null)
  319. return this.ConnectedUsersManager._getConnectedUser(
  320. this.project_id,
  321. this.client_id,
  322. (err, result) => {
  323. if (err) return done(err)
  324. result.connected.should.equal(false)
  325. result.client_id.should.equal(this.client_id)
  326. return done()
  327. }
  328. )
  329. })
  330. return it('should return a not connected user if there is an empty object', function (done) {
  331. this.rClient.hgetall.callsArgWith(1, null, {})
  332. return this.ConnectedUsersManager._getConnectedUser(
  333. this.project_id,
  334. this.client_id,
  335. (err, result) => {
  336. if (err) return done(err)
  337. result.connected.should.equal(false)
  338. result.client_id.should.equal(this.client_id)
  339. return done()
  340. }
  341. )
  342. })
  343. })
  344. return describe('getConnectedUsers', function () {
  345. beforeEach(function () {
  346. this.users = ['1234', '5678', '9123', '8234']
  347. this.rClient.smembers.callsArgWith(1, null, this.users)
  348. this.ConnectedUsersManager._getConnectedUser = sinon.stub()
  349. this.ConnectedUsersManager._getConnectedUser
  350. .withArgs(this.project_id, this.users[0])
  351. .callsArgWith(2, null, {
  352. connected: true,
  353. client_age: 2,
  354. client_id: this.users[0],
  355. })
  356. this.ConnectedUsersManager._getConnectedUser
  357. .withArgs(this.project_id, this.users[1])
  358. .callsArgWith(2, null, {
  359. connected: false,
  360. client_age: 1,
  361. client_id: this.users[1],
  362. })
  363. this.ConnectedUsersManager._getConnectedUser
  364. .withArgs(this.project_id, this.users[2])
  365. .callsArgWith(2, null, {
  366. connected: true,
  367. client_age: 3,
  368. client_id: this.users[2],
  369. })
  370. return this.ConnectedUsersManager._getConnectedUser
  371. .withArgs(this.project_id, this.users[3])
  372. .callsArgWith(2, null, {
  373. connected: true,
  374. client_age: 11,
  375. client_id: this.users[3],
  376. })
  377. }) // connected but old
  378. return it('should only return the users in the list which are still in redis and recently updated', function (done) {
  379. return this.ConnectedUsersManager.getConnectedUsers(
  380. this.project_id,
  381. (err, users) => {
  382. if (err) return done(err)
  383. users.length.should.equal(2)
  384. users[0].should.deep.equal({
  385. client_id: this.users[0],
  386. client_age: 2,
  387. connected: true,
  388. })
  389. users[1].should.deep.equal({
  390. client_id: this.users[2],
  391. client_age: 3,
  392. connected: true,
  393. })
  394. return done()
  395. }
  396. )
  397. })
  398. })
  399. })