WebsocketController.test.js 56 KB

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