WebsocketControllerTests.js 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  1. /* eslint-disable
  2. no-return-assign,
  3. no-throw-literal,
  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. * 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 sinon = require('sinon')
  15. const { expect } = require('chai')
  16. const modulePath = '../../../app/js/WebsocketController.js'
  17. const SandboxedModule = require('sandboxed-module')
  18. const tk = require('timekeeper')
  19. const { UpdateTooLargeError } = require('../../../app/js/Errors')
  20. describe('WebsocketController', function () {
  21. beforeEach(function () {
  22. tk.freeze(new Date())
  23. this.project_id = 'project-id-123'
  24. this.user = {
  25. _id: (this.user_id = 'user-id-123'),
  26. first_name: 'James',
  27. last_name: 'Allen',
  28. email: 'james@example.com',
  29. signUpDate: new Date('2014-01-01'),
  30. loginCount: 42,
  31. }
  32. this.callback = sinon.stub()
  33. this.client = {
  34. disconnected: false,
  35. id: (this.client_id = 'mock-client-id-123'),
  36. publicId: `other-id-${Math.random()}`,
  37. ol_context: {},
  38. joinLeaveEpoch: 0,
  39. join: sinon.stub(),
  40. leave: sinon.stub(),
  41. }
  42. return (this.WebsocketController = SandboxedModule.require(modulePath, {
  43. requires: {
  44. './WebApiManager': (this.WebApiManager = {}),
  45. './AuthorizationManager': (this.AuthorizationManager = {}),
  46. './DocumentUpdaterManager': (this.DocumentUpdaterManager = {}),
  47. './ConnectedUsersManager': (this.ConnectedUsersManager = {}),
  48. './WebsocketLoadBalancer': (this.WebsocketLoadBalancer = {}),
  49. '@overleaf/metrics': (this.metrics = {
  50. inc: sinon.stub(),
  51. set: sinon.stub(),
  52. }),
  53. './RoomManager': (this.RoomManager = {}),
  54. },
  55. }))
  56. })
  57. afterEach(function () {
  58. return tk.reset()
  59. })
  60. describe('joinProject', function () {
  61. describe('when authorised', function () {
  62. beforeEach(function () {
  63. this.client.id = 'mock-client-id'
  64. this.project = {
  65. name: 'Test Project',
  66. owner: {
  67. _id: (this.owner_id = 'mock-owner-id-123'),
  68. },
  69. }
  70. this.privilegeLevel = 'owner'
  71. this.ConnectedUsersManager.updateUserPosition = sinon
  72. .stub()
  73. .callsArgAsync(4)
  74. this.isRestrictedUser = true
  75. this.WebApiManager.joinProject = sinon
  76. .stub()
  77. .callsArgWith(
  78. 2,
  79. null,
  80. this.project,
  81. this.privilegeLevel,
  82. this.isRestrictedUser
  83. )
  84. this.RoomManager.joinProject = sinon.stub().callsArg(2)
  85. return this.WebsocketController.joinProject(
  86. this.client,
  87. this.user,
  88. this.project_id,
  89. this.callback
  90. )
  91. })
  92. it('should load the project from web', function () {
  93. return this.WebApiManager.joinProject
  94. .calledWith(this.project_id, this.user)
  95. .should.equal(true)
  96. })
  97. it('should join the project room', function () {
  98. return this.RoomManager.joinProject
  99. .calledWith(this.client, this.project_id)
  100. .should.equal(true)
  101. })
  102. it('should set the privilege level on the client', function () {
  103. return this.client.ol_context.privilege_level.should.equal(
  104. this.privilegeLevel
  105. )
  106. })
  107. it("should set the user's id on the client", function () {
  108. return this.client.ol_context.user_id.should.equal(this.user._id)
  109. })
  110. it("should set the user's email on the client", function () {
  111. return this.client.ol_context.email.should.equal(this.user.email)
  112. })
  113. it("should set the user's first_name on the client", function () {
  114. return this.client.ol_context.first_name.should.equal(
  115. this.user.first_name
  116. )
  117. })
  118. it("should set the user's last_name on the client", function () {
  119. return this.client.ol_context.last_name.should.equal(
  120. this.user.last_name
  121. )
  122. })
  123. it("should set the user's sign up date on the client", function () {
  124. return this.client.ol_context.signup_date.should.equal(
  125. this.user.signUpDate
  126. )
  127. })
  128. it("should set the user's login_count on the client", function () {
  129. return this.client.ol_context.login_count.should.equal(
  130. this.user.loginCount
  131. )
  132. })
  133. it('should set the connected time on the client', function () {
  134. return this.client.ol_context.connected_time.should.equal(new Date())
  135. })
  136. it('should set the project_id on the client', function () {
  137. return this.client.ol_context.project_id.should.equal(this.project_id)
  138. })
  139. it('should set the project owner id on the client', function () {
  140. return this.client.ol_context.owner_id.should.equal(this.owner_id)
  141. })
  142. it('should set the is_restricted_user flag on the client', function () {
  143. return this.client.ol_context.is_restricted_user.should.equal(
  144. this.isRestrictedUser
  145. )
  146. })
  147. it('should call the callback with the project, privilegeLevel and protocolVersion', function () {
  148. return this.callback
  149. .calledWith(
  150. null,
  151. this.project,
  152. this.privilegeLevel,
  153. this.WebsocketController.PROTOCOL_VERSION
  154. )
  155. .should.equal(true)
  156. })
  157. it('should mark the user as connected in ConnectedUsersManager', function () {
  158. return this.ConnectedUsersManager.updateUserPosition
  159. .calledWith(this.project_id, this.client.publicId, this.user, null)
  160. .should.equal(true)
  161. })
  162. return it('should increment the join-project metric', function () {
  163. return this.metrics.inc
  164. .calledWith('editor.join-project')
  165. .should.equal(true)
  166. })
  167. })
  168. describe('when not authorized', function () {
  169. beforeEach(function () {
  170. this.WebApiManager.joinProject = sinon
  171. .stub()
  172. .callsArgWith(2, null, null, null)
  173. return this.WebsocketController.joinProject(
  174. this.client,
  175. this.user,
  176. this.project_id,
  177. this.callback
  178. )
  179. })
  180. it('should return an error', function () {
  181. return this.callback
  182. .calledWith(sinon.match({ message: 'not authorized' }))
  183. .should.equal(true)
  184. })
  185. return it('should not log an error', function () {
  186. return this.logger.error.called.should.equal(false)
  187. })
  188. })
  189. describe('when the subscribe failed', function () {
  190. beforeEach(function () {
  191. this.client.id = 'mock-client-id'
  192. this.project = {
  193. name: 'Test Project',
  194. owner: {
  195. _id: (this.owner_id = 'mock-owner-id-123'),
  196. },
  197. }
  198. this.privilegeLevel = 'owner'
  199. this.ConnectedUsersManager.updateUserPosition = sinon
  200. .stub()
  201. .callsArgAsync(4)
  202. this.isRestrictedUser = true
  203. this.WebApiManager.joinProject = sinon
  204. .stub()
  205. .callsArgWith(
  206. 2,
  207. null,
  208. this.project,
  209. this.privilegeLevel,
  210. this.isRestrictedUser
  211. )
  212. this.RoomManager.joinProject = sinon
  213. .stub()
  214. .callsArgWith(2, new Error('subscribe failed'))
  215. return this.WebsocketController.joinProject(
  216. this.client,
  217. this.user,
  218. this.project_id,
  219. this.callback
  220. )
  221. })
  222. return it('should return an error', function () {
  223. this.callback
  224. .calledWith(sinon.match({ message: 'subscribe failed' }))
  225. .should.equal(true)
  226. return this.callback.args[0][0].message.should.equal('subscribe failed')
  227. })
  228. })
  229. describe('when the client has disconnected', function () {
  230. beforeEach(function () {
  231. this.client.disconnected = true
  232. this.WebApiManager.joinProject = sinon.stub().callsArg(2)
  233. return this.WebsocketController.joinProject(
  234. this.client,
  235. this.user,
  236. this.project_id,
  237. this.callback
  238. )
  239. })
  240. it('should not call WebApiManager.joinProject', function () {
  241. return expect(this.WebApiManager.joinProject.called).to.equal(false)
  242. })
  243. it('should call the callback with no details', function () {
  244. return expect(this.callback.args[0]).to.deep.equal([])
  245. })
  246. return it('should increment the editor.join-project.disconnected metric with a status', function () {
  247. return expect(
  248. this.metrics.inc.calledWith('editor.join-project.disconnected', 1, {
  249. status: 'immediately',
  250. })
  251. ).to.equal(true)
  252. })
  253. })
  254. return describe('when the client disconnects while WebApiManager.joinProject is running', function () {
  255. beforeEach(function () {
  256. this.WebApiManager.joinProject = (project, user, cb) => {
  257. this.client.disconnected = true
  258. return cb(
  259. null,
  260. this.project,
  261. this.privilegeLevel,
  262. this.isRestrictedUser
  263. )
  264. }
  265. return this.WebsocketController.joinProject(
  266. this.client,
  267. this.user,
  268. this.project_id,
  269. this.callback
  270. )
  271. })
  272. it('should call the callback with no details', function () {
  273. return expect(this.callback.args[0]).to.deep.equal([])
  274. })
  275. return it('should increment the editor.join-project.disconnected metric with a status', function () {
  276. return expect(
  277. this.metrics.inc.calledWith('editor.join-project.disconnected', 1, {
  278. status: 'after-web-api-call',
  279. })
  280. ).to.equal(true)
  281. })
  282. })
  283. })
  284. describe('leaveProject', function () {
  285. beforeEach(function () {
  286. this.DocumentUpdaterManager.flushProjectToMongoAndDelete = sinon
  287. .stub()
  288. .callsArg(1)
  289. this.ConnectedUsersManager.markUserAsDisconnected = sinon
  290. .stub()
  291. .callsArg(2)
  292. this.WebsocketLoadBalancer.emitToRoom = sinon.stub()
  293. this.RoomManager.leaveProjectAndDocs = sinon.stub()
  294. this.clientsInRoom = []
  295. this.io = {
  296. sockets: {
  297. clients: roomId => {
  298. if (roomId !== this.project_id) {
  299. throw 'expected room_id to be project_id'
  300. }
  301. return this.clientsInRoom
  302. },
  303. },
  304. }
  305. this.client.ol_context.project_id = this.project_id
  306. this.client.ol_context.user_id = this.user_id
  307. this.WebsocketController.FLUSH_IF_EMPTY_DELAY = 0
  308. return tk.reset()
  309. }) // Allow setTimeout to work.
  310. describe('when the client did not joined a project yet', function () {
  311. beforeEach(function (done) {
  312. this.client.ol_context = {}
  313. return this.WebsocketController.leaveProject(this.io, this.client, done)
  314. })
  315. it('should bail out when calling leaveProject', function () {
  316. this.WebsocketLoadBalancer.emitToRoom.called.should.equal(false)
  317. this.RoomManager.leaveProjectAndDocs.called.should.equal(false)
  318. return this.ConnectedUsersManager.markUserAsDisconnected.called.should.equal(
  319. false
  320. )
  321. })
  322. return it('should not inc any metric', function () {
  323. return this.metrics.inc.called.should.equal(false)
  324. })
  325. })
  326. describe('when the project is empty', function () {
  327. beforeEach(function (done) {
  328. this.clientsInRoom = []
  329. return this.WebsocketController.leaveProject(this.io, this.client, done)
  330. })
  331. it('should end clientTracking.clientDisconnected to the project room', function () {
  332. return this.WebsocketLoadBalancer.emitToRoom
  333. .calledWith(
  334. this.project_id,
  335. 'clientTracking.clientDisconnected',
  336. this.client.publicId
  337. )
  338. .should.equal(true)
  339. })
  340. it('should mark the user as disconnected', function () {
  341. return this.ConnectedUsersManager.markUserAsDisconnected
  342. .calledWith(this.project_id, this.client.publicId)
  343. .should.equal(true)
  344. })
  345. it('should flush the project in the document updater', function () {
  346. return this.DocumentUpdaterManager.flushProjectToMongoAndDelete
  347. .calledWith(this.project_id)
  348. .should.equal(true)
  349. })
  350. it('should increment the leave-project metric', function () {
  351. return this.metrics.inc
  352. .calledWith('editor.leave-project')
  353. .should.equal(true)
  354. })
  355. return it('should track the disconnection in RoomManager', function () {
  356. return this.RoomManager.leaveProjectAndDocs
  357. .calledWith(this.client)
  358. .should.equal(true)
  359. })
  360. })
  361. describe('when the project is not empty', function () {
  362. beforeEach(function (done) {
  363. this.clientsInRoom = ['mock-remaining-client']
  364. this.io = {
  365. sockets: {
  366. clients: roomId => {
  367. if (roomId !== this.project_id) {
  368. throw 'expected room_id to be project_id'
  369. }
  370. return this.clientsInRoom
  371. },
  372. },
  373. }
  374. return this.WebsocketController.leaveProject(this.io, this.client, done)
  375. })
  376. return it('should not flush the project in the document updater', function () {
  377. return this.DocumentUpdaterManager.flushProjectToMongoAndDelete.called.should.equal(
  378. false
  379. )
  380. })
  381. })
  382. describe('when client has not authenticated', function () {
  383. beforeEach(function (done) {
  384. this.client.ol_context.user_id = null
  385. this.client.ol_context.project_id = null
  386. return this.WebsocketController.leaveProject(this.io, this.client, done)
  387. })
  388. it('should not end clientTracking.clientDisconnected to the project room', function () {
  389. return this.WebsocketLoadBalancer.emitToRoom
  390. .calledWith(
  391. this.project_id,
  392. 'clientTracking.clientDisconnected',
  393. this.client.publicId
  394. )
  395. .should.equal(false)
  396. })
  397. it('should not mark the user as disconnected', function () {
  398. return this.ConnectedUsersManager.markUserAsDisconnected
  399. .calledWith(this.project_id, this.client.publicId)
  400. .should.equal(false)
  401. })
  402. it('should not flush the project in the document updater', function () {
  403. return this.DocumentUpdaterManager.flushProjectToMongoAndDelete
  404. .calledWith(this.project_id)
  405. .should.equal(false)
  406. })
  407. return it('should not increment the leave-project metric', function () {
  408. return this.metrics.inc
  409. .calledWith('editor.leave-project')
  410. .should.equal(false)
  411. })
  412. })
  413. return describe('when client has not joined a project', function () {
  414. beforeEach(function (done) {
  415. this.client.ol_context.user_id = this.user_id
  416. this.client.ol_context.project_id = null
  417. return this.WebsocketController.leaveProject(this.io, this.client, done)
  418. })
  419. it('should not end clientTracking.clientDisconnected to the project room', function () {
  420. return this.WebsocketLoadBalancer.emitToRoom
  421. .calledWith(
  422. this.project_id,
  423. 'clientTracking.clientDisconnected',
  424. this.client.publicId
  425. )
  426. .should.equal(false)
  427. })
  428. it('should not mark the user as disconnected', function () {
  429. return this.ConnectedUsersManager.markUserAsDisconnected
  430. .calledWith(this.project_id, this.client.publicId)
  431. .should.equal(false)
  432. })
  433. it('should not flush the project in the document updater', function () {
  434. return this.DocumentUpdaterManager.flushProjectToMongoAndDelete
  435. .calledWith(this.project_id)
  436. .should.equal(false)
  437. })
  438. return it('should not increment the leave-project metric', function () {
  439. return this.metrics.inc
  440. .calledWith('editor.leave-project')
  441. .should.equal(false)
  442. })
  443. })
  444. })
  445. describe('joinDoc', function () {
  446. beforeEach(function () {
  447. this.doc_id = 'doc-id-123'
  448. this.doc_lines = ['doc', 'lines']
  449. this.version = 42
  450. this.ops = ['mock', 'ops']
  451. this.ranges = { mock: 'ranges' }
  452. this.options = {}
  453. this.client.ol_context.project_id = this.project_id
  454. this.client.ol_context.is_restricted_user = false
  455. this.AuthorizationManager.addAccessToDoc = sinon.stub().yields()
  456. this.AuthorizationManager.assertClientCanViewProject = sinon
  457. .stub()
  458. .callsArgWith(1, null)
  459. this.AuthorizationManager.assertClientCanViewProjectAndDoc = sinon
  460. .stub()
  461. .callsArgWith(2, null)
  462. this.DocumentUpdaterManager.getDocument = sinon
  463. .stub()
  464. .callsArgWith(
  465. 3,
  466. null,
  467. this.doc_lines,
  468. this.version,
  469. this.ranges,
  470. this.ops
  471. )
  472. return (this.RoomManager.joinDoc = sinon.stub().callsArg(2))
  473. })
  474. describe('works', function () {
  475. beforeEach(function () {
  476. return this.WebsocketController.joinDoc(
  477. this.client,
  478. this.doc_id,
  479. -1,
  480. this.options,
  481. this.callback
  482. )
  483. })
  484. it('should inc the joinLeaveEpoch', function () {
  485. expect(this.client.joinLeaveEpoch).to.equal(1)
  486. })
  487. it('should check that the client is authorized to view the project', function () {
  488. return this.AuthorizationManager.assertClientCanViewProject
  489. .calledWith(this.client)
  490. .should.equal(true)
  491. })
  492. it('should get the document from the DocumentUpdaterManager with fromVersion', function () {
  493. return this.DocumentUpdaterManager.getDocument
  494. .calledWith(this.project_id, this.doc_id, -1)
  495. .should.equal(true)
  496. })
  497. it('should add permissions for the client to access the doc', function () {
  498. return this.AuthorizationManager.addAccessToDoc
  499. .calledWith(this.client, this.doc_id)
  500. .should.equal(true)
  501. })
  502. it('should join the client to room for the doc_id', function () {
  503. return this.RoomManager.joinDoc
  504. .calledWith(this.client, this.doc_id)
  505. .should.equal(true)
  506. })
  507. it('should call the callback with the lines, version, ranges and ops', function () {
  508. return this.callback
  509. .calledWith(null, this.doc_lines, this.version, this.ops, this.ranges)
  510. .should.equal(true)
  511. })
  512. return it('should increment the join-doc metric', function () {
  513. return this.metrics.inc.calledWith('editor.join-doc').should.equal(true)
  514. })
  515. })
  516. describe('with a fromVersion', function () {
  517. beforeEach(function () {
  518. this.fromVersion = 40
  519. return this.WebsocketController.joinDoc(
  520. this.client,
  521. this.doc_id,
  522. this.fromVersion,
  523. this.options,
  524. this.callback
  525. )
  526. })
  527. return it('should get the document from the DocumentUpdaterManager with fromVersion', function () {
  528. return this.DocumentUpdaterManager.getDocument
  529. .calledWith(this.project_id, this.doc_id, this.fromVersion)
  530. .should.equal(true)
  531. })
  532. })
  533. describe('with doclines that need escaping', function () {
  534. beforeEach(function () {
  535. this.doc_lines.push(['räksmörgås'])
  536. return this.WebsocketController.joinDoc(
  537. this.client,
  538. this.doc_id,
  539. -1,
  540. this.options,
  541. this.callback
  542. )
  543. })
  544. return it('should call the callback with the escaped lines', function () {
  545. const escapedLines = this.callback.args[0][1]
  546. const escapedWord = escapedLines.pop()
  547. escapedWord.should.equal('räksmörgås')
  548. // Check that unescaping works
  549. return decodeURIComponent(escape(escapedWord)).should.equal(
  550. 'räksmörgås'
  551. )
  552. })
  553. })
  554. describe('with comments that need encoding', function () {
  555. beforeEach(function () {
  556. this.ranges.comments = [{ op: { c: 'räksmörgås' } }]
  557. return this.WebsocketController.joinDoc(
  558. this.client,
  559. this.doc_id,
  560. -1,
  561. { encodeRanges: true },
  562. this.callback
  563. )
  564. })
  565. return it('should call the callback with the encoded comment', function () {
  566. const encodedComments = this.callback.args[0][4]
  567. const encodedComment = encodedComments.comments.pop()
  568. const encodedCommentText = encodedComment.op.c
  569. return encodedCommentText.should.equal('räksmörgås')
  570. })
  571. })
  572. describe('with changes that need encoding', function () {
  573. it('should call the callback with the encoded insert change', function () {
  574. this.ranges.changes = [{ op: { i: 'räksmörgås' } }]
  575. this.WebsocketController.joinDoc(
  576. this.client,
  577. this.doc_id,
  578. -1,
  579. { encodeRanges: true },
  580. this.callback
  581. )
  582. const encodedChanges = this.callback.args[0][4]
  583. const encodedChange = encodedChanges.changes.pop()
  584. const encodedChangeText = encodedChange.op.i
  585. return encodedChangeText.should.equal('räksmörgås')
  586. })
  587. return it('should call the callback with the encoded delete change', function () {
  588. this.ranges.changes = [{ op: { d: 'räksmörgås' } }]
  589. this.WebsocketController.joinDoc(
  590. this.client,
  591. this.doc_id,
  592. -1,
  593. { encodeRanges: true },
  594. this.callback
  595. )
  596. const encodedChanges = this.callback.args[0][4]
  597. const encodedChange = encodedChanges.changes.pop()
  598. const encodedChangeText = encodedChange.op.d
  599. return encodedChangeText.should.equal('räksmörgås')
  600. })
  601. })
  602. describe('when not authorized', function () {
  603. beforeEach(function () {
  604. this.AuthorizationManager.assertClientCanViewProject = sinon
  605. .stub()
  606. .callsArgWith(1, (this.err = new Error('not authorized')))
  607. return this.WebsocketController.joinDoc(
  608. this.client,
  609. this.doc_id,
  610. -1,
  611. this.options,
  612. this.callback
  613. )
  614. })
  615. it('should call the callback with an error', function () {
  616. return this.callback
  617. .calledWith(sinon.match({ message: 'not authorized' }))
  618. .should.equal(true)
  619. })
  620. return it('should not call the DocumentUpdaterManager', function () {
  621. return this.DocumentUpdaterManager.getDocument.called.should.equal(
  622. false
  623. )
  624. })
  625. })
  626. describe('with a restricted client', function () {
  627. beforeEach(function () {
  628. this.ranges.comments = [{ op: { a: 1 } }, { op: { a: 2 } }]
  629. this.client.ol_context.is_restricted_user = true
  630. return this.WebsocketController.joinDoc(
  631. this.client,
  632. this.doc_id,
  633. -1,
  634. this.options,
  635. this.callback
  636. )
  637. })
  638. return it('should overwrite ranges.comments with an empty list', function () {
  639. const ranges = this.callback.args[0][4]
  640. return expect(ranges.comments).to.deep.equal([])
  641. })
  642. })
  643. describe('when the client has disconnected', function () {
  644. beforeEach(function () {
  645. this.client.disconnected = true
  646. return this.WebsocketController.joinDoc(
  647. this.client,
  648. this.doc_id,
  649. -1,
  650. this.options,
  651. this.callback
  652. )
  653. })
  654. it('should call the callback with no details', function () {
  655. return expect(this.callback.args[0]).to.deep.equal([])
  656. })
  657. it('should increment the editor.join-doc.disconnected metric with a status', function () {
  658. return expect(
  659. this.metrics.inc.calledWith('editor.join-doc.disconnected', 1, {
  660. status: 'immediately',
  661. })
  662. ).to.equal(true)
  663. })
  664. return it('should not get the document', function () {
  665. return expect(this.DocumentUpdaterManager.getDocument.called).to.equal(
  666. false
  667. )
  668. })
  669. })
  670. describe('when the client disconnects while auth checks are running', function () {
  671. beforeEach(function (done) {
  672. this.AuthorizationManager.assertClientCanViewProjectAndDoc.yields(
  673. new Error()
  674. )
  675. this.DocumentUpdaterManager.checkDocument = (projectId, docId, cb) => {
  676. this.client.disconnected = true
  677. cb()
  678. }
  679. this.WebsocketController.joinDoc(
  680. this.client,
  681. this.doc_id,
  682. -1,
  683. this.options,
  684. (...args) => {
  685. this.callback(...args)
  686. done()
  687. }
  688. )
  689. })
  690. it('should call the callback with no details', function () {
  691. expect(this.callback.called).to.equal(true)
  692. expect(this.callback.args[0]).to.deep.equal([])
  693. })
  694. it('should increment the editor.join-doc.disconnected metric with a status', function () {
  695. expect(
  696. this.metrics.inc.calledWith('editor.join-doc.disconnected', 1, {
  697. status: 'after-client-auth-check',
  698. })
  699. ).to.equal(true)
  700. })
  701. it('should not get the document', function () {
  702. expect(this.DocumentUpdaterManager.getDocument.called).to.equal(false)
  703. })
  704. })
  705. describe('when the client starts a parallel joinDoc request', function () {
  706. beforeEach(function (done) {
  707. this.AuthorizationManager.assertClientCanViewProjectAndDoc.yields(
  708. new Error()
  709. )
  710. this.DocumentUpdaterManager.checkDocument = (projectId, docId, cb) => {
  711. this.DocumentUpdaterManager.checkDocument = sinon.stub().yields()
  712. this.WebsocketController.joinDoc(
  713. this.client,
  714. this.doc_id,
  715. -1,
  716. {},
  717. () => {}
  718. )
  719. cb()
  720. }
  721. this.WebsocketController.joinDoc(
  722. this.client,
  723. this.doc_id,
  724. -1,
  725. this.options,
  726. (...args) => {
  727. this.callback(...args)
  728. // make sure the other joinDoc request completed
  729. setTimeout(done, 5)
  730. }
  731. )
  732. })
  733. it('should call the callback with an error', function () {
  734. expect(this.callback.called).to.equal(true)
  735. expect(this.callback.args[0][0].message).to.equal(
  736. 'joinLeaveEpoch mismatch'
  737. )
  738. })
  739. it('should get the document once (the parallel request wins)', function () {
  740. expect(this.DocumentUpdaterManager.getDocument.callCount).to.equal(1)
  741. })
  742. })
  743. describe('when the client starts a parallel leaveDoc request', function () {
  744. beforeEach(function (done) {
  745. this.RoomManager.leaveDoc = sinon.stub()
  746. this.AuthorizationManager.assertClientCanViewProjectAndDoc.yields(
  747. new Error()
  748. )
  749. this.DocumentUpdaterManager.checkDocument = (projectId, docId, cb) => {
  750. this.WebsocketController.leaveDoc(this.client, this.doc_id, () => {})
  751. cb()
  752. }
  753. this.WebsocketController.joinDoc(
  754. this.client,
  755. this.doc_id,
  756. -1,
  757. this.options,
  758. (...args) => {
  759. this.callback(...args)
  760. done()
  761. }
  762. )
  763. })
  764. it('should call the callback with an error', function () {
  765. expect(this.callback.called).to.equal(true)
  766. expect(this.callback.args[0][0].message).to.equal(
  767. 'joinLeaveEpoch mismatch'
  768. )
  769. })
  770. it('should not get the document', function () {
  771. expect(this.DocumentUpdaterManager.getDocument.called).to.equal(false)
  772. })
  773. })
  774. describe('when the client disconnects while RoomManager.joinDoc is running', function () {
  775. beforeEach(function () {
  776. this.RoomManager.joinDoc = (client, docId, cb) => {
  777. this.client.disconnected = true
  778. return cb()
  779. }
  780. return this.WebsocketController.joinDoc(
  781. this.client,
  782. this.doc_id,
  783. -1,
  784. this.options,
  785. this.callback
  786. )
  787. })
  788. it('should call the callback with no details', function () {
  789. return expect(this.callback.args[0]).to.deep.equal([])
  790. })
  791. it('should increment the editor.join-doc.disconnected metric with a status', function () {
  792. return expect(
  793. this.metrics.inc.calledWith('editor.join-doc.disconnected', 1, {
  794. status: 'after-joining-room',
  795. })
  796. ).to.equal(true)
  797. })
  798. return it('should not get the document', function () {
  799. return expect(this.DocumentUpdaterManager.getDocument.called).to.equal(
  800. false
  801. )
  802. })
  803. })
  804. return describe('when the client disconnects while DocumentUpdaterManager.getDocument is running', function () {
  805. beforeEach(function () {
  806. this.DocumentUpdaterManager.getDocument = (
  807. projectId,
  808. docId,
  809. fromVersion,
  810. callback
  811. ) => {
  812. this.client.disconnected = true
  813. return callback(
  814. null,
  815. this.doc_lines,
  816. this.version,
  817. this.ranges,
  818. this.ops
  819. )
  820. }
  821. return this.WebsocketController.joinDoc(
  822. this.client,
  823. this.doc_id,
  824. -1,
  825. this.options,
  826. this.callback
  827. )
  828. })
  829. it('should call the callback with no details', function () {
  830. return expect(this.callback.args[0]).to.deep.equal([])
  831. })
  832. return it('should increment the editor.join-doc.disconnected metric with a status', function () {
  833. return expect(
  834. this.metrics.inc.calledWith('editor.join-doc.disconnected', 1, {
  835. status: 'after-doc-updater-call',
  836. })
  837. ).to.equal(true)
  838. })
  839. })
  840. })
  841. describe('leaveDoc', function () {
  842. beforeEach(function () {
  843. this.doc_id = 'doc-id-123'
  844. this.client.ol_context.project_id = this.project_id
  845. this.RoomManager.leaveDoc = sinon.stub()
  846. return this.WebsocketController.leaveDoc(
  847. this.client,
  848. this.doc_id,
  849. this.callback
  850. )
  851. })
  852. it('should inc the joinLeaveEpoch', function () {
  853. expect(this.client.joinLeaveEpoch).to.equal(1)
  854. })
  855. it('should remove the client from the doc_id room', function () {
  856. return this.RoomManager.leaveDoc
  857. .calledWith(this.client, this.doc_id)
  858. .should.equal(true)
  859. })
  860. it('should call the callback', function () {
  861. return this.callback.called.should.equal(true)
  862. })
  863. return it('should increment the leave-doc metric', function () {
  864. return this.metrics.inc.calledWith('editor.leave-doc').should.equal(true)
  865. })
  866. })
  867. describe('getConnectedUsers', function () {
  868. beforeEach(function () {
  869. this.client.ol_context.project_id = this.project_id
  870. this.users = ['mock', 'users']
  871. this.WebsocketLoadBalancer.emitToRoom = sinon.stub()
  872. return (this.ConnectedUsersManager.getConnectedUsers = sinon
  873. .stub()
  874. .callsArgWith(1, null, this.users))
  875. })
  876. describe('when authorized', function () {
  877. beforeEach(function (done) {
  878. this.AuthorizationManager.assertClientCanViewProject = sinon
  879. .stub()
  880. .callsArgWith(1, null)
  881. return this.WebsocketController.getConnectedUsers(
  882. this.client,
  883. (...args) => {
  884. this.callback(...Array.from(args || []))
  885. return done()
  886. }
  887. )
  888. })
  889. it('should check that the client is authorized to view the project', function () {
  890. return this.AuthorizationManager.assertClientCanViewProject
  891. .calledWith(this.client)
  892. .should.equal(true)
  893. })
  894. it('should broadcast a request to update the client list', function () {
  895. return this.WebsocketLoadBalancer.emitToRoom
  896. .calledWith(this.project_id, 'clientTracking.refresh')
  897. .should.equal(true)
  898. })
  899. it('should get the connected users for the project', function () {
  900. return this.ConnectedUsersManager.getConnectedUsers
  901. .calledWith(this.project_id)
  902. .should.equal(true)
  903. })
  904. it('should return the users', function () {
  905. return this.callback.calledWith(null, this.users).should.equal(true)
  906. })
  907. return it('should increment the get-connected-users metric', function () {
  908. return this.metrics.inc
  909. .calledWith('editor.get-connected-users')
  910. .should.equal(true)
  911. })
  912. })
  913. describe('when not authorized', function () {
  914. beforeEach(function () {
  915. this.AuthorizationManager.assertClientCanViewProject = sinon
  916. .stub()
  917. .callsArgWith(1, (this.err = new Error('not authorized')))
  918. return this.WebsocketController.getConnectedUsers(
  919. this.client,
  920. this.callback
  921. )
  922. })
  923. it('should not get the connected users for the project', function () {
  924. return this.ConnectedUsersManager.getConnectedUsers.called.should.equal(
  925. false
  926. )
  927. })
  928. return it('should return an error', function () {
  929. return this.callback.calledWith(this.err).should.equal(true)
  930. })
  931. })
  932. describe('when restricted user', function () {
  933. beforeEach(function () {
  934. this.client.ol_context.is_restricted_user = true
  935. this.AuthorizationManager.assertClientCanViewProject = sinon
  936. .stub()
  937. .callsArgWith(1, null)
  938. return this.WebsocketController.getConnectedUsers(
  939. this.client,
  940. this.callback
  941. )
  942. })
  943. it('should return an empty array of users', function () {
  944. return this.callback.calledWith(null, []).should.equal(true)
  945. })
  946. return it('should not get the connected users for the project', function () {
  947. return this.ConnectedUsersManager.getConnectedUsers.called.should.equal(
  948. false
  949. )
  950. })
  951. })
  952. return describe('when the client has disconnected', function () {
  953. beforeEach(function () {
  954. this.client.disconnected = true
  955. this.AuthorizationManager.assertClientCanViewProject = sinon.stub()
  956. return this.WebsocketController.getConnectedUsers(
  957. this.client,
  958. this.callback
  959. )
  960. })
  961. it('should call the callback with no details', function () {
  962. return expect(this.callback.args[0]).to.deep.equal([])
  963. })
  964. return it('should not check permissions', function () {
  965. return expect(
  966. this.AuthorizationManager.assertClientCanViewProject.called
  967. ).to.equal(false)
  968. })
  969. })
  970. })
  971. describe('updateClientPosition', function () {
  972. beforeEach(function () {
  973. this.WebsocketLoadBalancer.emitToRoom = sinon.stub()
  974. this.ConnectedUsersManager.updateUserPosition = sinon
  975. .stub()
  976. .callsArgAsync(4)
  977. this.AuthorizationManager.assertClientCanViewProjectAndDoc = sinon
  978. .stub()
  979. .callsArgWith(2, null)
  980. return (this.update = {
  981. doc_id: (this.doc_id = 'doc-id-123'),
  982. row: (this.row = 42),
  983. column: (this.column = 37),
  984. })
  985. })
  986. describe('with a logged in user', function () {
  987. beforeEach(function (done) {
  988. this.client.ol_context = {
  989. project_id: this.project_id,
  990. first_name: (this.first_name = 'Douglas'),
  991. last_name: (this.last_name = 'Adams'),
  992. email: (this.email = 'joe@example.com'),
  993. user_id: (this.user_id = 'user-id-123'),
  994. }
  995. this.populatedCursorData = {
  996. doc_id: this.doc_id,
  997. id: this.client.publicId,
  998. name: `${this.first_name} ${this.last_name}`,
  999. row: this.row,
  1000. column: this.column,
  1001. email: this.email,
  1002. user_id: this.user_id,
  1003. }
  1004. this.WebsocketController.updateClientPosition(
  1005. this.client,
  1006. this.update,
  1007. done
  1008. )
  1009. })
  1010. it("should send the update to the project room with the user's name", function () {
  1011. return this.WebsocketLoadBalancer.emitToRoom
  1012. .calledWith(
  1013. this.project_id,
  1014. 'clientTracking.clientUpdated',
  1015. this.populatedCursorData
  1016. )
  1017. .should.equal(true)
  1018. })
  1019. it('should send the cursor data to the connected user manager', function (done) {
  1020. this.ConnectedUsersManager.updateUserPosition
  1021. .calledWith(
  1022. this.project_id,
  1023. this.client.publicId,
  1024. {
  1025. _id: this.user_id,
  1026. email: this.email,
  1027. first_name: this.first_name,
  1028. last_name: this.last_name,
  1029. },
  1030. {
  1031. row: this.row,
  1032. column: this.column,
  1033. doc_id: this.doc_id,
  1034. }
  1035. )
  1036. .should.equal(true)
  1037. return done()
  1038. })
  1039. return it('should increment the update-client-position metric at 0.1 frequency', function () {
  1040. return this.metrics.inc
  1041. .calledWith('editor.update-client-position', 0.1)
  1042. .should.equal(true)
  1043. })
  1044. })
  1045. describe('with a logged in user who has no last_name set', function () {
  1046. beforeEach(function (done) {
  1047. this.client.ol_context = {
  1048. project_id: this.project_id,
  1049. first_name: (this.first_name = 'Douglas'),
  1050. last_name: undefined,
  1051. email: (this.email = 'joe@example.com'),
  1052. user_id: (this.user_id = 'user-id-123'),
  1053. }
  1054. this.populatedCursorData = {
  1055. doc_id: this.doc_id,
  1056. id: this.client.publicId,
  1057. name: `${this.first_name}`,
  1058. row: this.row,
  1059. column: this.column,
  1060. email: this.email,
  1061. user_id: this.user_id,
  1062. }
  1063. this.WebsocketController.updateClientPosition(
  1064. this.client,
  1065. this.update,
  1066. done
  1067. )
  1068. })
  1069. it("should send the update to the project room with the user's name", function () {
  1070. return this.WebsocketLoadBalancer.emitToRoom
  1071. .calledWith(
  1072. this.project_id,
  1073. 'clientTracking.clientUpdated',
  1074. this.populatedCursorData
  1075. )
  1076. .should.equal(true)
  1077. })
  1078. it('should send the cursor data to the connected user manager', function (done) {
  1079. this.ConnectedUsersManager.updateUserPosition
  1080. .calledWith(
  1081. this.project_id,
  1082. this.client.publicId,
  1083. {
  1084. _id: this.user_id,
  1085. email: this.email,
  1086. first_name: this.first_name,
  1087. last_name: undefined,
  1088. },
  1089. {
  1090. row: this.row,
  1091. column: this.column,
  1092. doc_id: this.doc_id,
  1093. }
  1094. )
  1095. .should.equal(true)
  1096. return done()
  1097. })
  1098. return it('should increment the update-client-position metric at 0.1 frequency', function () {
  1099. return this.metrics.inc
  1100. .calledWith('editor.update-client-position', 0.1)
  1101. .should.equal(true)
  1102. })
  1103. })
  1104. describe('with a logged in user who has no first_name set', function () {
  1105. beforeEach(function (done) {
  1106. this.client.ol_context = {
  1107. project_id: this.project_id,
  1108. first_name: undefined,
  1109. last_name: (this.last_name = 'Adams'),
  1110. email: (this.email = 'joe@example.com'),
  1111. user_id: (this.user_id = 'user-id-123'),
  1112. }
  1113. this.populatedCursorData = {
  1114. doc_id: this.doc_id,
  1115. id: this.client.publicId,
  1116. name: `${this.last_name}`,
  1117. row: this.row,
  1118. column: this.column,
  1119. email: this.email,
  1120. user_id: this.user_id,
  1121. }
  1122. this.WebsocketController.updateClientPosition(
  1123. this.client,
  1124. this.update,
  1125. done
  1126. )
  1127. })
  1128. it("should send the update to the project room with the user's name", function () {
  1129. return this.WebsocketLoadBalancer.emitToRoom
  1130. .calledWith(
  1131. this.project_id,
  1132. 'clientTracking.clientUpdated',
  1133. this.populatedCursorData
  1134. )
  1135. .should.equal(true)
  1136. })
  1137. it('should send the cursor data to the connected user manager', function (done) {
  1138. this.ConnectedUsersManager.updateUserPosition
  1139. .calledWith(
  1140. this.project_id,
  1141. this.client.publicId,
  1142. {
  1143. _id: this.user_id,
  1144. email: this.email,
  1145. first_name: undefined,
  1146. last_name: this.last_name,
  1147. },
  1148. {
  1149. row: this.row,
  1150. column: this.column,
  1151. doc_id: this.doc_id,
  1152. }
  1153. )
  1154. .should.equal(true)
  1155. return done()
  1156. })
  1157. return it('should increment the update-client-position metric at 0.1 frequency', function () {
  1158. return this.metrics.inc
  1159. .calledWith('editor.update-client-position', 0.1)
  1160. .should.equal(true)
  1161. })
  1162. })
  1163. describe('with a logged in user who has no names set', function () {
  1164. beforeEach(function (done) {
  1165. this.client.ol_context = {
  1166. project_id: this.project_id,
  1167. first_name: undefined,
  1168. last_name: undefined,
  1169. email: (this.email = 'joe@example.com'),
  1170. user_id: (this.user_id = 'user-id-123'),
  1171. }
  1172. return this.WebsocketController.updateClientPosition(
  1173. this.client,
  1174. this.update,
  1175. done
  1176. )
  1177. })
  1178. return it('should send the update to the project name with no name', function () {
  1179. return this.WebsocketLoadBalancer.emitToRoom
  1180. .calledWith(this.project_id, 'clientTracking.clientUpdated', {
  1181. doc_id: this.doc_id,
  1182. id: this.client.publicId,
  1183. user_id: this.user_id,
  1184. name: '',
  1185. row: this.row,
  1186. column: this.column,
  1187. email: this.email,
  1188. })
  1189. .should.equal(true)
  1190. })
  1191. })
  1192. describe('with an anonymous user', function () {
  1193. beforeEach(function (done) {
  1194. this.client.ol_context = {
  1195. project_id: this.project_id,
  1196. }
  1197. return this.WebsocketController.updateClientPosition(
  1198. this.client,
  1199. this.update,
  1200. done
  1201. )
  1202. })
  1203. it('should send the update to the project room with no name', function () {
  1204. return this.WebsocketLoadBalancer.emitToRoom
  1205. .calledWith(this.project_id, 'clientTracking.clientUpdated', {
  1206. doc_id: this.doc_id,
  1207. id: this.client.publicId,
  1208. name: '',
  1209. row: this.row,
  1210. column: this.column,
  1211. })
  1212. .should.equal(true)
  1213. })
  1214. return it('should not send cursor data to the connected user manager', function (done) {
  1215. this.ConnectedUsersManager.updateUserPosition.called.should.equal(false)
  1216. return done()
  1217. })
  1218. })
  1219. return describe('when the client has disconnected', function () {
  1220. beforeEach(function (done) {
  1221. this.client.disconnected = true
  1222. this.AuthorizationManager.assertClientCanViewProjectAndDoc =
  1223. sinon.stub()
  1224. return this.WebsocketController.updateClientPosition(
  1225. this.client,
  1226. this.update,
  1227. (...args) => {
  1228. this.callback(...args)
  1229. done(args[0])
  1230. }
  1231. )
  1232. })
  1233. it('should call the callback with no details', function () {
  1234. return expect(this.callback.args[0]).to.deep.equal([])
  1235. })
  1236. return it('should not check permissions', function () {
  1237. return expect(
  1238. this.AuthorizationManager.assertClientCanViewProjectAndDoc.called
  1239. ).to.equal(false)
  1240. })
  1241. })
  1242. })
  1243. describe('applyOtUpdate', function () {
  1244. beforeEach(function () {
  1245. this.update = { op: { p: 12, t: 'foo' } }
  1246. this.client.ol_context.user_id = this.user_id
  1247. this.client.ol_context.project_id = this.project_id
  1248. this.WebsocketController._assertClientCanApplyUpdate = sinon
  1249. .stub()
  1250. .yields()
  1251. return (this.DocumentUpdaterManager.queueChange = sinon
  1252. .stub()
  1253. .callsArg(3))
  1254. })
  1255. describe('succesfully', function () {
  1256. beforeEach(function () {
  1257. return this.WebsocketController.applyOtUpdate(
  1258. this.client,
  1259. this.doc_id,
  1260. this.update,
  1261. this.callback
  1262. )
  1263. })
  1264. it('should set the source of the update to the client id', function () {
  1265. return this.update.meta.source.should.equal(this.client.publicId)
  1266. })
  1267. it('should set the user_id of the update to the user id', function () {
  1268. return this.update.meta.user_id.should.equal(this.user_id)
  1269. })
  1270. it('should queue the update', function () {
  1271. return this.DocumentUpdaterManager.queueChange
  1272. .calledWith(this.project_id, this.doc_id, this.update)
  1273. .should.equal(true)
  1274. })
  1275. it('should call the callback', function () {
  1276. return this.callback.called.should.equal(true)
  1277. })
  1278. return it('should increment the doc updates', function () {
  1279. return this.metrics.inc
  1280. .calledWith('editor.doc-update')
  1281. .should.equal(true)
  1282. })
  1283. })
  1284. describe('unsuccessfully', function () {
  1285. beforeEach(function () {
  1286. this.client.disconnect = sinon.stub()
  1287. this.DocumentUpdaterManager.queueChange = sinon
  1288. .stub()
  1289. .callsArgWith(3, (this.error = new Error('Something went wrong')))
  1290. return this.WebsocketController.applyOtUpdate(
  1291. this.client,
  1292. this.doc_id,
  1293. this.update,
  1294. this.callback
  1295. )
  1296. })
  1297. it('should disconnect the client', function () {
  1298. return this.client.disconnect.called.should.equal(true)
  1299. })
  1300. it('should not log an error', function () {
  1301. return this.logger.error.called.should.equal(false)
  1302. })
  1303. return it('should call the callback with the error', function () {
  1304. return this.callback.calledWith(this.error).should.equal(true)
  1305. })
  1306. })
  1307. describe('when not authorized', function () {
  1308. beforeEach(function () {
  1309. this.client.disconnect = sinon.stub()
  1310. this.WebsocketController._assertClientCanApplyUpdate = sinon
  1311. .stub()
  1312. .yields((this.error = new Error('not authorized')))
  1313. return this.WebsocketController.applyOtUpdate(
  1314. this.client,
  1315. this.doc_id,
  1316. this.update,
  1317. this.callback
  1318. )
  1319. })
  1320. // This happens in a setTimeout to allow the client a chance to receive the error first.
  1321. // I'm not sure how to unit test, but it is acceptance tested.
  1322. // it "should disconnect the client", ->
  1323. // @client.disconnect.called.should.equal true
  1324. it('should not log a warning', function () {
  1325. return this.logger.warn.called.should.equal(false)
  1326. })
  1327. return it('should call the callback with the error', function () {
  1328. return this.callback.calledWith(this.error).should.equal(true)
  1329. })
  1330. })
  1331. return describe('update_too_large', function () {
  1332. beforeEach(function (done) {
  1333. this.client.disconnect = sinon.stub()
  1334. this.client.emit = sinon.stub()
  1335. this.client.ol_context.user_id = this.user_id
  1336. this.client.ol_context.project_id = this.project_id
  1337. const error = new UpdateTooLargeError(7372835)
  1338. this.DocumentUpdaterManager.queueChange = sinon
  1339. .stub()
  1340. .callsArgWith(3, error)
  1341. this.WebsocketController.applyOtUpdate(
  1342. this.client,
  1343. this.doc_id,
  1344. this.update,
  1345. this.callback
  1346. )
  1347. return setTimeout(() => done(), 1)
  1348. })
  1349. it('should call the callback with no error', function () {
  1350. this.callback.called.should.equal(true)
  1351. return this.callback.args[0].should.deep.equal([])
  1352. })
  1353. it('should log a warning with the size and context', function () {
  1354. this.logger.warn.called.should.equal(true)
  1355. return this.logger.warn.args[0].should.deep.equal([
  1356. {
  1357. userId: this.user_id,
  1358. projectId: this.project_id,
  1359. docId: this.doc_id,
  1360. updateSize: 7372835,
  1361. },
  1362. 'update is too large',
  1363. ])
  1364. })
  1365. describe('after 100ms', function () {
  1366. beforeEach(function (done) {
  1367. return setTimeout(done, 100)
  1368. })
  1369. it('should send an otUpdateError the client', function () {
  1370. return this.client.emit.calledWith('otUpdateError').should.equal(true)
  1371. })
  1372. return it('should disconnect the client', function () {
  1373. return this.client.disconnect.called.should.equal(true)
  1374. })
  1375. })
  1376. return describe('when the client disconnects during the next 100ms', function () {
  1377. beforeEach(function (done) {
  1378. this.client.disconnected = true
  1379. return setTimeout(done, 100)
  1380. })
  1381. it('should not send an otUpdateError the client', function () {
  1382. return this.client.emit
  1383. .calledWith('otUpdateError')
  1384. .should.equal(false)
  1385. })
  1386. it('should not disconnect the client', function () {
  1387. return this.client.disconnect.called.should.equal(false)
  1388. })
  1389. return it('should increment the editor.doc-update.disconnected metric with a status', function () {
  1390. return expect(
  1391. this.metrics.inc.calledWith('editor.doc-update.disconnected', 1, {
  1392. status: 'at-otUpdateError',
  1393. })
  1394. ).to.equal(true)
  1395. })
  1396. })
  1397. })
  1398. })
  1399. return describe('_assertClientCanApplyUpdate', function () {
  1400. beforeEach(function () {
  1401. this.edit_update = {
  1402. op: [
  1403. { i: 'foo', p: 42 },
  1404. { c: 'bar', p: 132 },
  1405. ],
  1406. } // comments may still be in an edit op
  1407. this.comment_update = { op: [{ c: 'bar', p: 132 }] }
  1408. this.AuthorizationManager.assertClientCanEditProjectAndDoc = sinon.stub()
  1409. return (this.AuthorizationManager.assertClientCanViewProjectAndDoc =
  1410. sinon.stub())
  1411. })
  1412. describe('with a read-write client', function () {
  1413. return it('should return successfully', function (done) {
  1414. this.AuthorizationManager.assertClientCanEditProjectAndDoc.yields(null)
  1415. return this.WebsocketController._assertClientCanApplyUpdate(
  1416. this.client,
  1417. this.doc_id,
  1418. this.edit_update,
  1419. error => {
  1420. expect(error).to.be.null
  1421. return done()
  1422. }
  1423. )
  1424. })
  1425. })
  1426. describe('with a read-only client and an edit op', function () {
  1427. return it('should return an error', function (done) {
  1428. this.AuthorizationManager.assertClientCanEditProjectAndDoc.yields(
  1429. new Error('not authorized')
  1430. )
  1431. this.AuthorizationManager.assertClientCanViewProjectAndDoc.yields(null)
  1432. return this.WebsocketController._assertClientCanApplyUpdate(
  1433. this.client,
  1434. this.doc_id,
  1435. this.edit_update,
  1436. error => {
  1437. expect(error.message).to.equal('not authorized')
  1438. return done()
  1439. }
  1440. )
  1441. })
  1442. })
  1443. describe('with a read-only client and a comment op', function () {
  1444. return it('should return successfully', function (done) {
  1445. this.AuthorizationManager.assertClientCanEditProjectAndDoc.yields(
  1446. new Error('not authorized')
  1447. )
  1448. this.AuthorizationManager.assertClientCanViewProjectAndDoc.yields(null)
  1449. return this.WebsocketController._assertClientCanApplyUpdate(
  1450. this.client,
  1451. this.doc_id,
  1452. this.comment_update,
  1453. error => {
  1454. expect(error).to.be.null
  1455. return done()
  1456. }
  1457. )
  1458. })
  1459. })
  1460. return describe('with a totally unauthorized client', function () {
  1461. return it('should return an error', function (done) {
  1462. this.AuthorizationManager.assertClientCanEditProjectAndDoc.yields(
  1463. new Error('not authorized')
  1464. )
  1465. this.AuthorizationManager.assertClientCanViewProjectAndDoc.yields(
  1466. new Error('not authorized')
  1467. )
  1468. return this.WebsocketController._assertClientCanApplyUpdate(
  1469. this.client,
  1470. this.doc_id,
  1471. this.comment_update,
  1472. error => {
  1473. expect(error.message).to.equal('not authorized')
  1474. return done()
  1475. }
  1476. )
  1477. })
  1478. })
  1479. })
  1480. })