WebsocketLoadBalancerTests.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /* eslint-disable
  2. no-return-assign,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const SandboxedModule = require('sandboxed-module')
  13. const sinon = require('sinon')
  14. const expect = require('chai').expect
  15. const modulePath = require('node:path').join(
  16. __dirname,
  17. '../../../app/js/WebsocketLoadBalancer'
  18. )
  19. describe('WebsocketLoadBalancer', function () {
  20. beforeEach(function () {
  21. this.rclient = {}
  22. this.RoomEvents = { on: sinon.stub() }
  23. this.WebsocketLoadBalancer = SandboxedModule.require(modulePath, {
  24. requires: {
  25. '@overleaf/settings': (this.Settings = { redis: {} }),
  26. './RedisClientManager': {
  27. createClientList: () => [],
  28. },
  29. './SafeJsonParse': (this.SafeJsonParse = {
  30. parse: (data, cb) => cb(null, JSON.parse(data)),
  31. }),
  32. './EventLogger': { checkEventOrder: sinon.stub() },
  33. './HealthCheckManager': { check: sinon.stub() },
  34. './RoomManager': (this.RoomManager = {
  35. eventSource: sinon.stub().returns(this.RoomEvents),
  36. }),
  37. './ChannelManager': (this.ChannelManager = { publish: sinon.stub() }),
  38. './ConnectedUsersManager': (this.ConnectedUsersManager = {
  39. refreshClient: sinon.stub(),
  40. }),
  41. },
  42. })
  43. this.io = {}
  44. this.WebsocketLoadBalancer.rclientPubList = [{ publish: sinon.stub() }]
  45. this.WebsocketLoadBalancer.rclientSubList = [
  46. {
  47. subscribe: sinon.stub(),
  48. on: sinon.stub(),
  49. },
  50. ]
  51. this.room_id = 'room-id'
  52. this.message = 'otUpdateApplied'
  53. return (this.payload = ['argument one', 42])
  54. })
  55. describe('shouldDisconnectClient', function () {
  56. it('should return false for general messages', function () {
  57. const client = {
  58. ol_context: { user_id: 'abcd' },
  59. }
  60. const message = {
  61. message: 'someNiceMessage',
  62. payload: [{ data: 'whatever' }],
  63. }
  64. expect(
  65. this.WebsocketLoadBalancer.shouldDisconnectClient(client, message)
  66. ).to.equal(false)
  67. })
  68. describe('collaborator access level changed', function () {
  69. const messageName = 'project:collaboratorAccessLevel:changed'
  70. const client = {
  71. ol_context: { user_id: 'abcd' },
  72. }
  73. it('should return true if the user id matches', function () {
  74. const message = {
  75. message: messageName,
  76. payload: [
  77. {
  78. userId: 'abcd',
  79. },
  80. ],
  81. }
  82. expect(
  83. this.WebsocketLoadBalancer.shouldDisconnectClient(client, message)
  84. ).to.equal(true)
  85. })
  86. it('should return false if the user id does not match', function () {
  87. const message = {
  88. message: messageName,
  89. payload: [
  90. {
  91. userId: 'xyz',
  92. },
  93. ],
  94. }
  95. expect(
  96. this.WebsocketLoadBalancer.shouldDisconnectClient(client, message)
  97. ).to.equal(false)
  98. })
  99. })
  100. describe('user removed from project', function () {
  101. const messageName = 'userRemovedFromProject'
  102. const client = {
  103. ol_context: { user_id: 'abcd' },
  104. }
  105. it('should return false, when the user_id does not match', function () {
  106. const message = {
  107. message: messageName,
  108. payload: ['xyz'],
  109. }
  110. expect(
  111. this.WebsocketLoadBalancer.shouldDisconnectClient(client, message)
  112. ).to.equal(false)
  113. })
  114. it('should return true, if the user_id matches', function () {
  115. const message = {
  116. message: messageName,
  117. payload: [`${client.ol_context.user_id}`],
  118. }
  119. expect(
  120. this.WebsocketLoadBalancer.shouldDisconnectClient(client, message)
  121. ).to.equal(true)
  122. })
  123. })
  124. describe('link-sharing turned off', function () {
  125. const messageName = 'project:publicAccessLevel:changed'
  126. describe('when the new access level is set to "private"', function () {
  127. const message = {
  128. message: messageName,
  129. payload: [{ newAccessLevel: 'private' }],
  130. }
  131. describe('when the user is an invited member', function () {
  132. const client = {
  133. ol_context: {
  134. is_invited_member: true,
  135. },
  136. }
  137. it('should return false', function () {
  138. expect(
  139. this.WebsocketLoadBalancer.shouldDisconnectClient(client, message)
  140. ).to.equal(false)
  141. })
  142. })
  143. describe('when the user not an invited member', function () {
  144. const client = {
  145. ol_context: {
  146. is_invited_member: false,
  147. },
  148. }
  149. it('should return true', function () {
  150. expect(
  151. this.WebsocketLoadBalancer.shouldDisconnectClient(client, message)
  152. ).to.equal(true)
  153. })
  154. })
  155. })
  156. describe('when the new access level is "tokenBased"', function () {
  157. const message = {
  158. message: messageName,
  159. payload: [{ newAccessLevel: 'tokenBased' }],
  160. }
  161. describe('when the user is an invited member', function () {
  162. const client = {
  163. ol_context: {
  164. is_invited_member: true,
  165. },
  166. }
  167. it('should return false', function () {
  168. expect(
  169. this.WebsocketLoadBalancer.shouldDisconnectClient(client, message)
  170. ).to.equal(false)
  171. })
  172. })
  173. describe('when the user not an invited member', function () {
  174. const client = {
  175. ol_context: {
  176. is_invited_member: false,
  177. },
  178. }
  179. it('should return false', function () {
  180. expect(
  181. this.WebsocketLoadBalancer.shouldDisconnectClient(client, message)
  182. ).to.equal(false)
  183. })
  184. })
  185. })
  186. })
  187. })
  188. describe('emitToRoom', function () {
  189. beforeEach(function () {
  190. return this.WebsocketLoadBalancer.emitToRoom(
  191. this.room_id,
  192. this.message,
  193. ...Array.from(this.payload)
  194. )
  195. })
  196. return it('should publish the message to redis', function () {
  197. return this.ChannelManager.publish
  198. .calledWith(
  199. this.WebsocketLoadBalancer.rclientPubList[0],
  200. 'editor-events',
  201. this.room_id,
  202. JSON.stringify({
  203. room_id: this.room_id,
  204. message: this.message,
  205. payload: this.payload,
  206. })
  207. )
  208. .should.equal(true)
  209. })
  210. })
  211. describe('emitToAll', function () {
  212. beforeEach(function () {
  213. this.WebsocketLoadBalancer.emitToRoom = sinon.stub()
  214. return this.WebsocketLoadBalancer.emitToAll(
  215. this.message,
  216. ...Array.from(this.payload)
  217. )
  218. })
  219. return it("should emit to the room 'all'", function () {
  220. return this.WebsocketLoadBalancer.emitToRoom
  221. .calledWith('all', this.message, ...Array.from(this.payload))
  222. .should.equal(true)
  223. })
  224. })
  225. describe('listenForEditorEvents', function () {
  226. beforeEach(function () {
  227. this.WebsocketLoadBalancer._processEditorEvent = sinon.stub()
  228. return this.WebsocketLoadBalancer.listenForEditorEvents()
  229. })
  230. it('should subscribe to the editor-events channel', function () {
  231. return this.WebsocketLoadBalancer.rclientSubList[0].subscribe
  232. .calledWith('editor-events')
  233. .should.equal(true)
  234. })
  235. return it('should process the events with _processEditorEvent', function () {
  236. return this.WebsocketLoadBalancer.rclientSubList[0].on
  237. .calledWith('message', sinon.match.func)
  238. .should.equal(true)
  239. })
  240. })
  241. return describe('_processEditorEvent', function () {
  242. describe('with bad JSON', function () {
  243. beforeEach(function () {
  244. this.isRestrictedUser = false
  245. this.SafeJsonParse.parse = sinon
  246. .stub()
  247. .callsArgWith(1, new Error('oops'))
  248. return this.WebsocketLoadBalancer._processEditorEvent(
  249. this.io,
  250. 'editor-events',
  251. 'blah'
  252. )
  253. })
  254. return it('should log an error', function () {
  255. return this.logger.error.called.should.equal(true)
  256. })
  257. })
  258. describe('with a designated room', function () {
  259. beforeEach(function () {
  260. this.io.sockets = {
  261. clients: sinon.stub().returns([
  262. {
  263. id: 'client-id-1',
  264. emit: (this.emit1 = sinon.stub()),
  265. ol_context: {},
  266. },
  267. {
  268. id: 'client-id-2',
  269. emit: (this.emit2 = sinon.stub()),
  270. ol_context: {},
  271. },
  272. {
  273. id: 'client-id-1',
  274. emit: (this.emit3 = sinon.stub()),
  275. ol_context: {},
  276. }, // duplicate client
  277. ]),
  278. }
  279. const data = JSON.stringify({
  280. room_id: this.room_id,
  281. message: this.message,
  282. payload: this.payload,
  283. })
  284. return this.WebsocketLoadBalancer._processEditorEvent(
  285. this.io,
  286. 'editor-events',
  287. data
  288. )
  289. })
  290. return it('should send the message to all (unique) clients in the room', function () {
  291. this.io.sockets.clients.calledWith(this.room_id).should.equal(true)
  292. this.emit1
  293. .calledWith(this.message, ...Array.from(this.payload))
  294. .should.equal(true)
  295. this.emit2
  296. .calledWith(this.message, ...Array.from(this.payload))
  297. .should.equal(true)
  298. return this.emit3.called.should.equal(false)
  299. })
  300. }) // duplicate client should be ignored
  301. describe('with a designated room, and restricted clients, not restricted message', function () {
  302. beforeEach(function () {
  303. this.io.sockets = {
  304. clients: sinon.stub().returns([
  305. {
  306. id: 'client-id-1',
  307. emit: (this.emit1 = sinon.stub()),
  308. ol_context: {},
  309. },
  310. {
  311. id: 'client-id-2',
  312. emit: (this.emit2 = sinon.stub()),
  313. ol_context: {},
  314. },
  315. {
  316. id: 'client-id-1',
  317. emit: (this.emit3 = sinon.stub()),
  318. ol_context: {},
  319. }, // duplicate client
  320. {
  321. id: 'client-id-4',
  322. emit: (this.emit4 = sinon.stub()),
  323. ol_context: { is_restricted_user: true },
  324. },
  325. ]),
  326. }
  327. const data = JSON.stringify({
  328. room_id: this.room_id,
  329. message: this.message,
  330. payload: this.payload,
  331. })
  332. return this.WebsocketLoadBalancer._processEditorEvent(
  333. this.io,
  334. 'editor-events',
  335. data
  336. )
  337. })
  338. return it('should send the message to all (unique) clients in the room', function () {
  339. this.io.sockets.clients.calledWith(this.room_id).should.equal(true)
  340. this.emit1
  341. .calledWith(this.message, ...Array.from(this.payload))
  342. .should.equal(true)
  343. this.emit2
  344. .calledWith(this.message, ...Array.from(this.payload))
  345. .should.equal(true)
  346. this.emit3.called.should.equal(false) // duplicate client should be ignored
  347. return this.emit4.called.should.equal(true)
  348. })
  349. }) // restricted client, but should be called
  350. describe('with a designated room, and restricted clients, restricted message', function () {
  351. beforeEach(function () {
  352. this.io.sockets = {
  353. clients: sinon.stub().returns([
  354. {
  355. id: 'client-id-1',
  356. emit: (this.emit1 = sinon.stub()),
  357. ol_context: {},
  358. },
  359. {
  360. id: 'client-id-2',
  361. emit: (this.emit2 = sinon.stub()),
  362. ol_context: {},
  363. },
  364. {
  365. id: 'client-id-1',
  366. emit: (this.emit3 = sinon.stub()),
  367. ol_context: {},
  368. }, // duplicate client
  369. {
  370. id: 'client-id-4',
  371. emit: (this.emit4 = sinon.stub()),
  372. ol_context: { is_restricted_user: true },
  373. },
  374. ]),
  375. }
  376. const data = JSON.stringify({
  377. room_id: this.room_id,
  378. message: (this.restrictedMessage = 'new-comment'),
  379. payload: this.payload,
  380. })
  381. return this.WebsocketLoadBalancer._processEditorEvent(
  382. this.io,
  383. 'editor-events',
  384. data
  385. )
  386. })
  387. return it('should send the message to all (unique) clients in the room, who are not restricted', function () {
  388. this.io.sockets.clients.calledWith(this.room_id).should.equal(true)
  389. this.emit1
  390. .calledWith(this.restrictedMessage, ...Array.from(this.payload))
  391. .should.equal(true)
  392. this.emit2
  393. .calledWith(this.restrictedMessage, ...Array.from(this.payload))
  394. .should.equal(true)
  395. this.emit3.called.should.equal(false) // duplicate client should be ignored
  396. return this.emit4.called.should.equal(false)
  397. })
  398. }) // restricted client, should not be called
  399. describe('when emitting to all', function () {
  400. beforeEach(function () {
  401. this.io.sockets = { emit: (this.emit = sinon.stub()) }
  402. const data = JSON.stringify({
  403. room_id: 'all',
  404. message: this.message,
  405. payload: this.payload,
  406. })
  407. return this.WebsocketLoadBalancer._processEditorEvent(
  408. this.io,
  409. 'editor-events',
  410. data
  411. )
  412. })
  413. return it('should send the message to all clients', function () {
  414. return this.emit
  415. .calledWith(this.message, ...Array.from(this.payload))
  416. .should.equal(true)
  417. })
  418. })
  419. describe('when it should disconnect one of the clients', function () {
  420. const targetUserId = 'bbb'
  421. const message = 'userRemovedFromProject'
  422. const payload = [`${targetUserId}`]
  423. const clients = [
  424. {
  425. id: 'client-id-1',
  426. emit: sinon.stub(),
  427. ol_context: { user_id: 'aaa' },
  428. disconnect: sinon.stub(),
  429. },
  430. {
  431. id: 'client-id-2',
  432. emit: sinon.stub(),
  433. ol_context: { user_id: `${targetUserId}` },
  434. disconnect: sinon.stub(),
  435. },
  436. {
  437. id: 'client-id-3',
  438. emit: sinon.stub(),
  439. ol_context: { user_id: 'ccc' },
  440. disconnect: sinon.stub(),
  441. },
  442. ]
  443. beforeEach(function () {
  444. this.io.sockets = {
  445. clients: sinon.stub().returns(clients),
  446. }
  447. const data = JSON.stringify({
  448. room_id: this.room_id,
  449. message,
  450. payload,
  451. })
  452. return this.WebsocketLoadBalancer._processEditorEvent(
  453. this.io,
  454. 'editor-events',
  455. data
  456. )
  457. })
  458. it('should disconnect the matching client, while sending message to other clients', function () {
  459. this.io.sockets.clients.calledWith(this.room_id).should.equal(true)
  460. const [client1, client2, client3] = clients
  461. // disconnecting one client
  462. client1.disconnect.called.should.equal(false)
  463. client2.disconnect.called.should.equal(true)
  464. client3.disconnect.called.should.equal(false)
  465. // emitting to remaining clients
  466. client1.emit
  467. .calledWith(message, ...Array.from(payload))
  468. .should.equal(true)
  469. client2.emit.calledWith('project:access:revoked').should.equal(true) // disconnected client should get informative message
  470. client3.emit
  471. .calledWith(message, ...Array.from(payload))
  472. .should.equal(true)
  473. })
  474. })
  475. })
  476. })