WebsocketControllerTests.js 52 KB

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