WebsocketControllerTests.js 48 KB

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