WebsocketControllerTests.js 52 KB

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