WebsocketLoadBalancerTests.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. require('chai').should()
  15. const modulePath = require('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. './RedisClientManager': {
  26. createClientList: () => []
  27. },
  28. 'logger-sharelatex': (this.logger = {
  29. log: sinon.stub(),
  30. error: sinon.stub()
  31. }),
  32. './SafeJsonParse': (this.SafeJsonParse = {
  33. parse: (data, cb) => cb(null, JSON.parse(data))
  34. }),
  35. './EventLogger': { checkEventOrder: sinon.stub() },
  36. './HealthCheckManager': { check: sinon.stub() },
  37. './RoomManager': (this.RoomManager = {
  38. eventSource: sinon.stub().returns(this.RoomEvents)
  39. }),
  40. './ChannelManager': (this.ChannelManager = { publish: sinon.stub() }),
  41. './ConnectedUsersManager': (this.ConnectedUsersManager = {
  42. refreshClient: sinon.stub()
  43. })
  44. }
  45. })
  46. this.io = {}
  47. this.WebsocketLoadBalancer.rclientPubList = [{ publish: sinon.stub() }]
  48. this.WebsocketLoadBalancer.rclientSubList = [
  49. {
  50. subscribe: sinon.stub(),
  51. on: sinon.stub()
  52. }
  53. ]
  54. this.room_id = 'room-id'
  55. this.message = 'otUpdateApplied'
  56. return (this.payload = ['argument one', 42])
  57. })
  58. describe('emitToRoom', function () {
  59. beforeEach(function () {
  60. return this.WebsocketLoadBalancer.emitToRoom(
  61. this.room_id,
  62. this.message,
  63. ...Array.from(this.payload)
  64. )
  65. })
  66. return it('should publish the message to redis', function () {
  67. return this.ChannelManager.publish
  68. .calledWith(
  69. this.WebsocketLoadBalancer.rclientPubList[0],
  70. 'editor-events',
  71. this.room_id,
  72. JSON.stringify({
  73. room_id: this.room_id,
  74. message: this.message,
  75. payload: this.payload
  76. })
  77. )
  78. .should.equal(true)
  79. })
  80. })
  81. describe('emitToAll', function () {
  82. beforeEach(function () {
  83. this.WebsocketLoadBalancer.emitToRoom = sinon.stub()
  84. return this.WebsocketLoadBalancer.emitToAll(
  85. this.message,
  86. ...Array.from(this.payload)
  87. )
  88. })
  89. return it("should emit to the room 'all'", function () {
  90. return this.WebsocketLoadBalancer.emitToRoom
  91. .calledWith('all', this.message, ...Array.from(this.payload))
  92. .should.equal(true)
  93. })
  94. })
  95. describe('listenForEditorEvents', function () {
  96. beforeEach(function () {
  97. this.WebsocketLoadBalancer._processEditorEvent = sinon.stub()
  98. return this.WebsocketLoadBalancer.listenForEditorEvents()
  99. })
  100. it('should subscribe to the editor-events channel', function () {
  101. return this.WebsocketLoadBalancer.rclientSubList[0].subscribe
  102. .calledWith('editor-events')
  103. .should.equal(true)
  104. })
  105. return it('should process the events with _processEditorEvent', function () {
  106. return this.WebsocketLoadBalancer.rclientSubList[0].on
  107. .calledWith('message', sinon.match.func)
  108. .should.equal(true)
  109. })
  110. })
  111. return describe('_processEditorEvent', function () {
  112. describe('with bad JSON', function () {
  113. beforeEach(function () {
  114. this.isRestrictedUser = false
  115. this.SafeJsonParse.parse = sinon
  116. .stub()
  117. .callsArgWith(1, new Error('oops'))
  118. return this.WebsocketLoadBalancer._processEditorEvent(
  119. this.io,
  120. 'editor-events',
  121. 'blah'
  122. )
  123. })
  124. return it('should log an error', function () {
  125. return this.logger.error.called.should.equal(true)
  126. })
  127. })
  128. describe('with a designated room', function () {
  129. beforeEach(function () {
  130. this.io.sockets = {
  131. clients: sinon.stub().returns([
  132. {
  133. id: 'client-id-1',
  134. emit: (this.emit1 = sinon.stub()),
  135. ol_context: {}
  136. },
  137. {
  138. id: 'client-id-2',
  139. emit: (this.emit2 = sinon.stub()),
  140. ol_context: {}
  141. },
  142. {
  143. id: 'client-id-1',
  144. emit: (this.emit3 = sinon.stub()),
  145. ol_context: {}
  146. } // duplicate client
  147. ])
  148. }
  149. const data = JSON.stringify({
  150. room_id: this.room_id,
  151. message: this.message,
  152. payload: this.payload
  153. })
  154. return this.WebsocketLoadBalancer._processEditorEvent(
  155. this.io,
  156. 'editor-events',
  157. data
  158. )
  159. })
  160. return it('should send the message to all (unique) clients in the room', function () {
  161. this.io.sockets.clients.calledWith(this.room_id).should.equal(true)
  162. this.emit1
  163. .calledWith(this.message, ...Array.from(this.payload))
  164. .should.equal(true)
  165. this.emit2
  166. .calledWith(this.message, ...Array.from(this.payload))
  167. .should.equal(true)
  168. return this.emit3.called.should.equal(false)
  169. })
  170. }) // duplicate client should be ignored
  171. describe('with a designated room, and restricted clients, not restricted message', function () {
  172. beforeEach(function () {
  173. this.io.sockets = {
  174. clients: sinon.stub().returns([
  175. {
  176. id: 'client-id-1',
  177. emit: (this.emit1 = sinon.stub()),
  178. ol_context: {}
  179. },
  180. {
  181. id: 'client-id-2',
  182. emit: (this.emit2 = sinon.stub()),
  183. ol_context: {}
  184. },
  185. {
  186. id: 'client-id-1',
  187. emit: (this.emit3 = sinon.stub()),
  188. ol_context: {}
  189. }, // duplicate client
  190. {
  191. id: 'client-id-4',
  192. emit: (this.emit4 = sinon.stub()),
  193. ol_context: { is_restricted_user: true }
  194. }
  195. ])
  196. }
  197. const data = JSON.stringify({
  198. room_id: this.room_id,
  199. message: this.message,
  200. payload: this.payload
  201. })
  202. return this.WebsocketLoadBalancer._processEditorEvent(
  203. this.io,
  204. 'editor-events',
  205. data
  206. )
  207. })
  208. return it('should send the message to all (unique) clients in the room', function () {
  209. this.io.sockets.clients.calledWith(this.room_id).should.equal(true)
  210. this.emit1
  211. .calledWith(this.message, ...Array.from(this.payload))
  212. .should.equal(true)
  213. this.emit2
  214. .calledWith(this.message, ...Array.from(this.payload))
  215. .should.equal(true)
  216. this.emit3.called.should.equal(false) // duplicate client should be ignored
  217. return this.emit4.called.should.equal(true)
  218. })
  219. }) // restricted client, but should be called
  220. describe('with a designated room, and restricted clients, restricted message', function () {
  221. beforeEach(function () {
  222. this.io.sockets = {
  223. clients: sinon.stub().returns([
  224. {
  225. id: 'client-id-1',
  226. emit: (this.emit1 = sinon.stub()),
  227. ol_context: {}
  228. },
  229. {
  230. id: 'client-id-2',
  231. emit: (this.emit2 = sinon.stub()),
  232. ol_context: {}
  233. },
  234. {
  235. id: 'client-id-1',
  236. emit: (this.emit3 = sinon.stub()),
  237. ol_context: {}
  238. }, // duplicate client
  239. {
  240. id: 'client-id-4',
  241. emit: (this.emit4 = sinon.stub()),
  242. ol_context: { is_restricted_user: true }
  243. }
  244. ])
  245. }
  246. const data = JSON.stringify({
  247. room_id: this.room_id,
  248. message: (this.restrictedMessage = 'new-comment'),
  249. payload: this.payload
  250. })
  251. return this.WebsocketLoadBalancer._processEditorEvent(
  252. this.io,
  253. 'editor-events',
  254. data
  255. )
  256. })
  257. return it('should send the message to all (unique) clients in the room, who are not restricted', function () {
  258. this.io.sockets.clients.calledWith(this.room_id).should.equal(true)
  259. this.emit1
  260. .calledWith(this.restrictedMessage, ...Array.from(this.payload))
  261. .should.equal(true)
  262. this.emit2
  263. .calledWith(this.restrictedMessage, ...Array.from(this.payload))
  264. .should.equal(true)
  265. this.emit3.called.should.equal(false) // duplicate client should be ignored
  266. return this.emit4.called.should.equal(false)
  267. })
  268. }) // restricted client, should not be called
  269. return describe('when emitting to all', function () {
  270. beforeEach(function () {
  271. this.io.sockets = { emit: (this.emit = sinon.stub()) }
  272. const data = JSON.stringify({
  273. room_id: 'all',
  274. message: this.message,
  275. payload: this.payload
  276. })
  277. return this.WebsocketLoadBalancer._processEditorEvent(
  278. this.io,
  279. 'editor-events',
  280. data
  281. )
  282. })
  283. return it('should send the message to all clients', function () {
  284. return this.emit
  285. .calledWith(this.message, ...Array.from(this.payload))
  286. .should.equal(true)
  287. })
  288. })
  289. })
  290. })