JoinDocTests.js 17 KB

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