JoinDocTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /* eslint-disable
  2. no-return-assign,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS101: Remove unnecessary use of Array.from
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const { expect } = require('chai')
  13. const RealTimeClient = require('./helpers/RealTimeClient')
  14. const MockDocUpdaterServer = require('./helpers/MockDocUpdaterServer')
  15. const FixturesManager = require('./helpers/FixturesManager')
  16. const async = require('async')
  17. describe('joinDoc', function () {
  18. before(function () {
  19. this.lines = ['test', 'doc', 'lines']
  20. this.version = 42
  21. this.ops = ['mock', 'doc', 'ops']
  22. return (this.ranges = { mock: 'ranges' })
  23. })
  24. describe('when authorised readAndWrite', function () {
  25. before(function (done) {
  26. return async.series(
  27. [
  28. cb => {
  29. return FixturesManager.setUpProject(
  30. {
  31. privilegeLevel: 'readAndWrite',
  32. },
  33. (e, { project_id: projectId, user_id: userId }) => {
  34. this.project_id = projectId
  35. this.user_id = userId
  36. return cb(e)
  37. }
  38. )
  39. },
  40. cb => {
  41. return FixturesManager.setUpDoc(
  42. this.project_id,
  43. {
  44. lines: this.lines,
  45. version: this.version,
  46. ops: this.ops,
  47. ranges: this.ranges,
  48. },
  49. (e, { doc_id: docId }) => {
  50. this.doc_id = docId
  51. return cb(e)
  52. }
  53. )
  54. },
  55. cb => {
  56. this.client = RealTimeClient.connect()
  57. return this.client.on('connectionAccepted', cb)
  58. },
  59. cb => {
  60. return this.client.emit(
  61. 'joinProject',
  62. { project_id: this.project_id },
  63. cb
  64. )
  65. },
  66. cb => {
  67. return this.client.emit(
  68. 'joinDoc',
  69. this.doc_id,
  70. (error, ...rest) => {
  71. ;[...this.returnedArgs] = Array.from(rest)
  72. return cb(error)
  73. }
  74. )
  75. },
  76. ],
  77. done
  78. )
  79. })
  80. it('should get the doc from the doc updater', function () {
  81. return MockDocUpdaterServer.getDocument
  82. .calledWith(this.project_id, this.doc_id, -1)
  83. .should.equal(true)
  84. })
  85. it('should return the doc lines, version, ranges and ops', function () {
  86. return this.returnedArgs.should.deep.equal([
  87. this.lines,
  88. this.version,
  89. this.ops,
  90. this.ranges,
  91. ])
  92. })
  93. return it('should have joined the doc room', function (done) {
  94. return RealTimeClient.getConnectedClient(
  95. this.client.socket.sessionid,
  96. (error, client) => {
  97. if (error) return done(error)
  98. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  99. return done()
  100. }
  101. )
  102. })
  103. })
  104. describe('when authorised readOnly', function () {
  105. before(function (done) {
  106. return async.series(
  107. [
  108. cb => {
  109. return FixturesManager.setUpProject(
  110. {
  111. privilegeLevel: 'readOnly',
  112. },
  113. (e, { project_id: projectId, user_id: userId }) => {
  114. this.project_id = projectId
  115. this.user_id = userId
  116. return cb(e)
  117. }
  118. )
  119. },
  120. cb => {
  121. return FixturesManager.setUpDoc(
  122. this.project_id,
  123. {
  124. lines: this.lines,
  125. version: this.version,
  126. ops: this.ops,
  127. ranges: this.ranges,
  128. },
  129. (e, { doc_id: docId }) => {
  130. this.doc_id = docId
  131. return cb(e)
  132. }
  133. )
  134. },
  135. cb => {
  136. this.client = RealTimeClient.connect()
  137. return this.client.on('connectionAccepted', cb)
  138. },
  139. cb => {
  140. return this.client.emit(
  141. 'joinProject',
  142. { project_id: this.project_id },
  143. cb
  144. )
  145. },
  146. cb => {
  147. return this.client.emit(
  148. 'joinDoc',
  149. this.doc_id,
  150. (error, ...rest) => {
  151. ;[...this.returnedArgs] = Array.from(rest)
  152. return cb(error)
  153. }
  154. )
  155. },
  156. ],
  157. done
  158. )
  159. })
  160. it('should get the doc from the doc updater', function () {
  161. return MockDocUpdaterServer.getDocument
  162. .calledWith(this.project_id, this.doc_id, -1)
  163. .should.equal(true)
  164. })
  165. it('should return the doc lines, version, ranges and ops', function () {
  166. return this.returnedArgs.should.deep.equal([
  167. this.lines,
  168. this.version,
  169. this.ops,
  170. this.ranges,
  171. ])
  172. })
  173. return it('should have joined the doc room', function (done) {
  174. return RealTimeClient.getConnectedClient(
  175. this.client.socket.sessionid,
  176. (error, client) => {
  177. if (error) return done(error)
  178. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  179. return done()
  180. }
  181. )
  182. })
  183. })
  184. describe('when authorised as owner', function () {
  185. before(function (done) {
  186. return async.series(
  187. [
  188. cb => {
  189. return FixturesManager.setUpProject(
  190. {
  191. privilegeLevel: 'owner',
  192. },
  193. (e, { project_id: projectId, user_id: userId }) => {
  194. this.project_id = projectId
  195. this.user_id = userId
  196. return cb(e)
  197. }
  198. )
  199. },
  200. cb => {
  201. return FixturesManager.setUpDoc(
  202. this.project_id,
  203. {
  204. lines: this.lines,
  205. version: this.version,
  206. ops: this.ops,
  207. ranges: this.ranges,
  208. },
  209. (e, { doc_id: docId }) => {
  210. this.doc_id = docId
  211. return cb(e)
  212. }
  213. )
  214. },
  215. cb => {
  216. this.client = RealTimeClient.connect()
  217. return this.client.on('connectionAccepted', cb)
  218. },
  219. cb => {
  220. return this.client.emit(
  221. 'joinProject',
  222. { project_id: this.project_id },
  223. cb
  224. )
  225. },
  226. cb => {
  227. return this.client.emit(
  228. 'joinDoc',
  229. this.doc_id,
  230. (error, ...rest) => {
  231. ;[...this.returnedArgs] = Array.from(rest)
  232. return cb(error)
  233. }
  234. )
  235. },
  236. ],
  237. done
  238. )
  239. })
  240. it('should get the doc from the doc updater', function () {
  241. return MockDocUpdaterServer.getDocument
  242. .calledWith(this.project_id, this.doc_id, -1)
  243. .should.equal(true)
  244. })
  245. it('should return the doc lines, version, ranges and ops', function () {
  246. return this.returnedArgs.should.deep.equal([
  247. this.lines,
  248. this.version,
  249. this.ops,
  250. this.ranges,
  251. ])
  252. })
  253. return it('should have joined the doc room', function (done) {
  254. return RealTimeClient.getConnectedClient(
  255. this.client.socket.sessionid,
  256. (error, client) => {
  257. if (error) return done(error)
  258. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  259. return done()
  260. }
  261. )
  262. })
  263. })
  264. // It is impossible to write an acceptance test to test joining an unauthorized
  265. // project, since joinProject already catches that. If you can join a project,
  266. // then you can join a doc in that project.
  267. describe('for an invalid doc', function () {
  268. before(function (done) {
  269. return async.series(
  270. [
  271. cb => {
  272. return FixturesManager.setUpProject(
  273. {
  274. privilegeLevel: 'owner',
  275. },
  276. (e, { project_id: projectId, user_id: userId }) => {
  277. this.project_id = projectId
  278. this.user_id = userId
  279. return cb(e)
  280. }
  281. )
  282. },
  283. cb => {
  284. return FixturesManager.setUpDoc(
  285. this.project_id,
  286. {
  287. lines: this.lines,
  288. version: this.version,
  289. ops: this.ops,
  290. ranges: this.ranges,
  291. },
  292. (e, { doc_id: docId }) => {
  293. this.doc_id = docId
  294. return cb(e)
  295. }
  296. )
  297. },
  298. cb => {
  299. this.client = RealTimeClient.connect()
  300. return this.client.on('connectionAccepted', cb)
  301. },
  302. cb => {
  303. return this.client.emit(
  304. 'joinProject',
  305. { project_id: this.project_id },
  306. cb
  307. )
  308. },
  309. cb => {
  310. return this.client.emit(
  311. 'joinDoc',
  312. 'invalid-doc-id',
  313. (error, ...rest) => {
  314. this.error = error
  315. return cb()
  316. }
  317. )
  318. },
  319. ],
  320. done
  321. )
  322. })
  323. it('should not get the doc from the doc updater', function () {
  324. return MockDocUpdaterServer.getDocument
  325. .calledWith(this.project_id, 'invalid-doc-id')
  326. .should.equal(false)
  327. })
  328. it('should return an invalid id error', function () {
  329. this.error.message.should.equal('invalid id')
  330. })
  331. return it('should not have joined the doc room', function (done) {
  332. return RealTimeClient.getConnectedClient(
  333. this.client.socket.sessionid,
  334. (error, client) => {
  335. if (error) return done(error)
  336. expect(Array.from(client.rooms).includes('invalid-doc-id')).to.equal(
  337. false
  338. )
  339. return done()
  340. }
  341. )
  342. })
  343. })
  344. describe('with a fromVersion', function () {
  345. before(function (done) {
  346. this.fromVersion = 36
  347. return async.series(
  348. [
  349. cb => {
  350. return FixturesManager.setUpProject(
  351. {
  352. privilegeLevel: 'readAndWrite',
  353. },
  354. (e, { project_id: projectId, user_id: userId }) => {
  355. this.project_id = projectId
  356. this.user_id = userId
  357. return cb(e)
  358. }
  359. )
  360. },
  361. cb => {
  362. return FixturesManager.setUpDoc(
  363. this.project_id,
  364. {
  365. lines: this.lines,
  366. version: this.version,
  367. ops: this.ops,
  368. ranges: this.ranges,
  369. },
  370. (e, { doc_id: docId }) => {
  371. this.doc_id = docId
  372. return cb(e)
  373. }
  374. )
  375. },
  376. cb => {
  377. this.client = RealTimeClient.connect()
  378. return this.client.on('connectionAccepted', cb)
  379. },
  380. cb => {
  381. return this.client.emit(
  382. 'joinProject',
  383. { project_id: this.project_id },
  384. cb
  385. )
  386. },
  387. cb => {
  388. return this.client.emit(
  389. 'joinDoc',
  390. this.doc_id,
  391. this.fromVersion,
  392. (error, ...rest) => {
  393. ;[...this.returnedArgs] = Array.from(rest)
  394. return cb(error)
  395. }
  396. )
  397. },
  398. ],
  399. done
  400. )
  401. })
  402. it('should get the doc from the doc updater with the fromVersion', function () {
  403. return MockDocUpdaterServer.getDocument
  404. .calledWith(this.project_id, this.doc_id, this.fromVersion)
  405. .should.equal(true)
  406. })
  407. it('should return the doc lines, version, ranges and ops', function () {
  408. return this.returnedArgs.should.deep.equal([
  409. this.lines,
  410. this.version,
  411. this.ops,
  412. this.ranges,
  413. ])
  414. })
  415. return it('should have joined the doc room', function (done) {
  416. return RealTimeClient.getConnectedClient(
  417. this.client.socket.sessionid,
  418. (error, client) => {
  419. if (error) return done(error)
  420. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  421. return done()
  422. }
  423. )
  424. })
  425. })
  426. describe('with options', function () {
  427. before(function (done) {
  428. this.options = { encodeRanges: true }
  429. return async.series(
  430. [
  431. cb => {
  432. return FixturesManager.setUpProject(
  433. {
  434. privilegeLevel: 'readAndWrite',
  435. },
  436. (e, { project_id: projectId, user_id: userId }) => {
  437. this.project_id = projectId
  438. this.user_id = userId
  439. return cb(e)
  440. }
  441. )
  442. },
  443. cb => {
  444. return FixturesManager.setUpDoc(
  445. this.project_id,
  446. {
  447. lines: this.lines,
  448. version: this.version,
  449. ops: this.ops,
  450. ranges: this.ranges,
  451. },
  452. (e, { doc_id: docId }) => {
  453. this.doc_id = docId
  454. return cb(e)
  455. }
  456. )
  457. },
  458. cb => {
  459. this.client = RealTimeClient.connect()
  460. return this.client.on('connectionAccepted', cb)
  461. },
  462. cb => {
  463. return this.client.emit(
  464. 'joinProject',
  465. { project_id: this.project_id },
  466. cb
  467. )
  468. },
  469. cb => {
  470. return this.client.emit(
  471. 'joinDoc',
  472. this.doc_id,
  473. this.options,
  474. (error, ...rest) => {
  475. ;[...this.returnedArgs] = Array.from(rest)
  476. return cb(error)
  477. }
  478. )
  479. },
  480. ],
  481. done
  482. )
  483. })
  484. it('should get the doc from the doc updater with the default fromVersion', function () {
  485. return MockDocUpdaterServer.getDocument
  486. .calledWith(this.project_id, this.doc_id, -1)
  487. .should.equal(true)
  488. })
  489. it('should return the doc lines, version, ranges and ops', function () {
  490. return this.returnedArgs.should.deep.equal([
  491. this.lines,
  492. this.version,
  493. this.ops,
  494. this.ranges,
  495. ])
  496. })
  497. return it('should have joined the doc room', function (done) {
  498. return RealTimeClient.getConnectedClient(
  499. this.client.socket.sessionid,
  500. (error, client) => {
  501. if (error) return done(error)
  502. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  503. return done()
  504. }
  505. )
  506. })
  507. })
  508. return describe('with fromVersion and options', function () {
  509. before(function (done) {
  510. this.fromVersion = 36
  511. this.options = { encodeRanges: true }
  512. return async.series(
  513. [
  514. cb => {
  515. return FixturesManager.setUpProject(
  516. {
  517. privilegeLevel: 'readAndWrite',
  518. },
  519. (e, { project_id: projectId, user_id: userId }) => {
  520. this.project_id = projectId
  521. this.user_id = userId
  522. return cb(e)
  523. }
  524. )
  525. },
  526. cb => {
  527. return FixturesManager.setUpDoc(
  528. this.project_id,
  529. {
  530. lines: this.lines,
  531. version: this.version,
  532. ops: this.ops,
  533. ranges: this.ranges,
  534. },
  535. (e, { doc_id: docId }) => {
  536. this.doc_id = docId
  537. return cb(e)
  538. }
  539. )
  540. },
  541. cb => {
  542. this.client = RealTimeClient.connect()
  543. return this.client.on('connectionAccepted', cb)
  544. },
  545. cb => {
  546. return this.client.emit(
  547. 'joinProject',
  548. { project_id: this.project_id },
  549. cb
  550. )
  551. },
  552. cb => {
  553. return this.client.emit(
  554. 'joinDoc',
  555. this.doc_id,
  556. this.fromVersion,
  557. this.options,
  558. (error, ...rest) => {
  559. ;[...this.returnedArgs] = Array.from(rest)
  560. return cb(error)
  561. }
  562. )
  563. },
  564. ],
  565. done
  566. )
  567. })
  568. it('should get the doc from the doc updater with the fromVersion', function () {
  569. return MockDocUpdaterServer.getDocument
  570. .calledWith(this.project_id, this.doc_id, this.fromVersion)
  571. .should.equal(true)
  572. })
  573. it('should return the doc lines, version, ranges and ops', function () {
  574. return this.returnedArgs.should.deep.equal([
  575. this.lines,
  576. this.version,
  577. this.ops,
  578. this.ranges,
  579. ])
  580. })
  581. return it('should have joined the doc room', function (done) {
  582. return RealTimeClient.getConnectedClient(
  583. this.client.socket.sessionid,
  584. (error, client) => {
  585. if (error) return done(error)
  586. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  587. return done()
  588. }
  589. )
  590. })
  591. })
  592. })