WebsocketControllerTests.js 48 KB

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