JoinDocTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-return-assign,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const { expect } = require('chai')
  15. const RealTimeClient = require('./helpers/RealTimeClient')
  16. const MockDocUpdaterServer = require('./helpers/MockDocUpdaterServer')
  17. const FixturesManager = require('./helpers/FixturesManager')
  18. const async = require('async')
  19. describe('joinDoc', function () {
  20. before(function () {
  21. this.lines = ['test', 'doc', 'lines']
  22. this.version = 42
  23. this.ops = ['mock', 'doc', 'ops']
  24. return (this.ranges = { mock: 'ranges' })
  25. })
  26. describe('when authorised readAndWrite', function () {
  27. before(function (done) {
  28. return async.series(
  29. [
  30. cb => {
  31. return FixturesManager.setUpProject(
  32. {
  33. privilegeLevel: 'readAndWrite',
  34. },
  35. (e, { project_id, user_id }) => {
  36. this.project_id = project_id
  37. this.user_id = user_id
  38. return cb(e)
  39. }
  40. )
  41. },
  42. cb => {
  43. return FixturesManager.setUpDoc(
  44. this.project_id,
  45. {
  46. lines: this.lines,
  47. version: this.version,
  48. ops: this.ops,
  49. ranges: this.ranges,
  50. },
  51. (e, { doc_id }) => {
  52. this.doc_id = doc_id
  53. return cb(e)
  54. }
  55. )
  56. },
  57. cb => {
  58. this.client = RealTimeClient.connect()
  59. return this.client.on('connectionAccepted', cb)
  60. },
  61. cb => {
  62. return this.client.emit(
  63. 'joinProject',
  64. { project_id: this.project_id },
  65. cb
  66. )
  67. },
  68. cb => {
  69. return this.client.emit(
  70. 'joinDoc',
  71. this.doc_id,
  72. (error, ...rest) => {
  73. ;[...this.returnedArgs] = Array.from(rest)
  74. return cb(error)
  75. }
  76. )
  77. },
  78. ],
  79. done
  80. )
  81. })
  82. it('should get the doc from the doc updater', function () {
  83. return MockDocUpdaterServer.getDocument
  84. .calledWith(this.project_id, this.doc_id, -1)
  85. .should.equal(true)
  86. })
  87. it('should return the doc lines, version, ranges and ops', function () {
  88. return this.returnedArgs.should.deep.equal([
  89. this.lines,
  90. this.version,
  91. this.ops,
  92. this.ranges,
  93. ])
  94. })
  95. return it('should have joined the doc room', function (done) {
  96. return RealTimeClient.getConnectedClient(
  97. this.client.socket.sessionid,
  98. (error, client) => {
  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. 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, user_id }) => {
  194. this.project_id = project_id
  195. this.user_id = user_id
  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 }) => {
  210. this.doc_id = doc_id
  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. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  258. return done()
  259. }
  260. )
  261. })
  262. })
  263. // It is impossible to write an acceptance test to test joining an unauthorized
  264. // project, since joinProject already catches that. If you can join a project,
  265. // then you can join a doc in that project.
  266. describe('for an invalid doc', function () {
  267. before(function (done) {
  268. return async.series(
  269. [
  270. cb => {
  271. return FixturesManager.setUpProject(
  272. {
  273. privilegeLevel: 'owner',
  274. },
  275. (e, { project_id, user_id }) => {
  276. this.project_id = project_id
  277. this.user_id = user_id
  278. return cb(e)
  279. }
  280. )
  281. },
  282. cb => {
  283. return FixturesManager.setUpDoc(
  284. this.project_id,
  285. {
  286. lines: this.lines,
  287. version: this.version,
  288. ops: this.ops,
  289. ranges: this.ranges,
  290. },
  291. (e, { doc_id }) => {
  292. this.doc_id = doc_id
  293. return cb(e)
  294. }
  295. )
  296. },
  297. cb => {
  298. this.client = RealTimeClient.connect()
  299. return this.client.on('connectionAccepted', cb)
  300. },
  301. cb => {
  302. return this.client.emit(
  303. 'joinProject',
  304. { project_id: this.project_id },
  305. cb
  306. )
  307. },
  308. cb => {
  309. return this.client.emit(
  310. 'joinDoc',
  311. 'invalid-doc-id',
  312. (error, ...rest) => {
  313. this.error = error
  314. return cb()
  315. }
  316. )
  317. },
  318. ],
  319. done
  320. )
  321. })
  322. it('should not get the doc from the doc updater', function () {
  323. return MockDocUpdaterServer.getDocument
  324. .calledWith(this.project_id, 'invalid-doc-id')
  325. .should.equal(false)
  326. })
  327. it('should return an invalid id error', function () {
  328. this.error.message.should.equal('invalid id')
  329. })
  330. return it('should not have joined the doc room', function (done) {
  331. return RealTimeClient.getConnectedClient(
  332. this.client.socket.sessionid,
  333. (error, client) => {
  334. expect(Array.from(client.rooms).includes('invalid-doc-id')).to.equal(
  335. false
  336. )
  337. return done()
  338. }
  339. )
  340. })
  341. })
  342. describe('with a fromVersion', function () {
  343. before(function (done) {
  344. this.fromVersion = 36
  345. return async.series(
  346. [
  347. cb => {
  348. return FixturesManager.setUpProject(
  349. {
  350. privilegeLevel: 'readAndWrite',
  351. },
  352. (e, { project_id, user_id }) => {
  353. this.project_id = project_id
  354. this.user_id = user_id
  355. return cb(e)
  356. }
  357. )
  358. },
  359. cb => {
  360. return FixturesManager.setUpDoc(
  361. this.project_id,
  362. {
  363. lines: this.lines,
  364. version: this.version,
  365. ops: this.ops,
  366. ranges: this.ranges,
  367. },
  368. (e, { doc_id }) => {
  369. this.doc_id = doc_id
  370. return cb(e)
  371. }
  372. )
  373. },
  374. cb => {
  375. this.client = RealTimeClient.connect()
  376. return this.client.on('connectionAccepted', cb)
  377. },
  378. cb => {
  379. return this.client.emit(
  380. 'joinProject',
  381. { project_id: this.project_id },
  382. cb
  383. )
  384. },
  385. cb => {
  386. return this.client.emit(
  387. 'joinDoc',
  388. this.doc_id,
  389. this.fromVersion,
  390. (error, ...rest) => {
  391. ;[...this.returnedArgs] = Array.from(rest)
  392. return cb(error)
  393. }
  394. )
  395. },
  396. ],
  397. done
  398. )
  399. })
  400. it('should get the doc from the doc updater with the fromVersion', function () {
  401. return MockDocUpdaterServer.getDocument
  402. .calledWith(this.project_id, this.doc_id, this.fromVersion)
  403. .should.equal(true)
  404. })
  405. it('should return the doc lines, version, ranges and ops', function () {
  406. return this.returnedArgs.should.deep.equal([
  407. this.lines,
  408. this.version,
  409. this.ops,
  410. this.ranges,
  411. ])
  412. })
  413. return it('should have joined the doc room', function (done) {
  414. return RealTimeClient.getConnectedClient(
  415. this.client.socket.sessionid,
  416. (error, client) => {
  417. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  418. return done()
  419. }
  420. )
  421. })
  422. })
  423. describe('with options', function () {
  424. before(function (done) {
  425. this.options = { encodeRanges: true }
  426. return async.series(
  427. [
  428. cb => {
  429. return FixturesManager.setUpProject(
  430. {
  431. privilegeLevel: 'readAndWrite',
  432. },
  433. (e, { project_id, user_id }) => {
  434. this.project_id = project_id
  435. this.user_id = user_id
  436. return cb(e)
  437. }
  438. )
  439. },
  440. cb => {
  441. return FixturesManager.setUpDoc(
  442. this.project_id,
  443. {
  444. lines: this.lines,
  445. version: this.version,
  446. ops: this.ops,
  447. ranges: this.ranges,
  448. },
  449. (e, { doc_id }) => {
  450. this.doc_id = doc_id
  451. return cb(e)
  452. }
  453. )
  454. },
  455. cb => {
  456. this.client = RealTimeClient.connect()
  457. return this.client.on('connectionAccepted', cb)
  458. },
  459. cb => {
  460. return this.client.emit(
  461. 'joinProject',
  462. { project_id: this.project_id },
  463. cb
  464. )
  465. },
  466. cb => {
  467. return this.client.emit(
  468. 'joinDoc',
  469. this.doc_id,
  470. this.options,
  471. (error, ...rest) => {
  472. ;[...this.returnedArgs] = Array.from(rest)
  473. return cb(error)
  474. }
  475. )
  476. },
  477. ],
  478. done
  479. )
  480. })
  481. it('should get the doc from the doc updater with the default fromVersion', function () {
  482. return MockDocUpdaterServer.getDocument
  483. .calledWith(this.project_id, this.doc_id, -1)
  484. .should.equal(true)
  485. })
  486. it('should return the doc lines, version, ranges and ops', function () {
  487. return this.returnedArgs.should.deep.equal([
  488. this.lines,
  489. this.version,
  490. this.ops,
  491. this.ranges,
  492. ])
  493. })
  494. return it('should have joined the doc room', function (done) {
  495. return RealTimeClient.getConnectedClient(
  496. this.client.socket.sessionid,
  497. (error, client) => {
  498. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  499. return done()
  500. }
  501. )
  502. })
  503. })
  504. return describe('with fromVersion and options', function () {
  505. before(function (done) {
  506. this.fromVersion = 36
  507. this.options = { encodeRanges: true }
  508. return async.series(
  509. [
  510. cb => {
  511. return FixturesManager.setUpProject(
  512. {
  513. privilegeLevel: 'readAndWrite',
  514. },
  515. (e, { project_id, user_id }) => {
  516. this.project_id = project_id
  517. this.user_id = user_id
  518. return cb(e)
  519. }
  520. )
  521. },
  522. cb => {
  523. return FixturesManager.setUpDoc(
  524. this.project_id,
  525. {
  526. lines: this.lines,
  527. version: this.version,
  528. ops: this.ops,
  529. ranges: this.ranges,
  530. },
  531. (e, { doc_id }) => {
  532. this.doc_id = doc_id
  533. return cb(e)
  534. }
  535. )
  536. },
  537. cb => {
  538. this.client = RealTimeClient.connect()
  539. return this.client.on('connectionAccepted', cb)
  540. },
  541. cb => {
  542. return this.client.emit(
  543. 'joinProject',
  544. { project_id: this.project_id },
  545. cb
  546. )
  547. },
  548. cb => {
  549. return this.client.emit(
  550. 'joinDoc',
  551. this.doc_id,
  552. this.fromVersion,
  553. this.options,
  554. (error, ...rest) => {
  555. ;[...this.returnedArgs] = Array.from(rest)
  556. return cb(error)
  557. }
  558. )
  559. },
  560. ],
  561. done
  562. )
  563. })
  564. it('should get the doc from the doc updater with the fromVersion', function () {
  565. return MockDocUpdaterServer.getDocument
  566. .calledWith(this.project_id, this.doc_id, this.fromVersion)
  567. .should.equal(true)
  568. })
  569. it('should return the doc lines, version, ranges and ops', function () {
  570. return this.returnedArgs.should.deep.equal([
  571. this.lines,
  572. this.version,
  573. this.ops,
  574. this.ranges,
  575. ])
  576. })
  577. return it('should have joined the doc room', function (done) {
  578. return RealTimeClient.getConnectedClient(
  579. this.client.socket.sessionid,
  580. (error, client) => {
  581. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  582. return done()
  583. }
  584. )
  585. })
  586. })
  587. })