JoinDocTests.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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(this.project_id, cb)
  57. },
  58. cb => {
  59. return this.client.emit(
  60. 'joinDoc',
  61. this.doc_id,
  62. (error, ...rest) => {
  63. ;[...this.returnedArgs] = Array.from(rest)
  64. return cb(error)
  65. }
  66. )
  67. },
  68. ],
  69. done
  70. )
  71. })
  72. it('should get the doc from the doc updater', function () {
  73. return MockDocUpdaterServer.getDocument
  74. .calledWith(this.project_id, this.doc_id, -1)
  75. .should.equal(true)
  76. })
  77. it('should return the doc lines, version, ranges and ops', function () {
  78. return this.returnedArgs.should.deep.equal([
  79. this.lines,
  80. this.version,
  81. this.ops,
  82. this.ranges,
  83. ])
  84. })
  85. return it('should have joined the doc room', function (done) {
  86. return RealTimeClient.getConnectedClient(
  87. this.client.socket.sessionid,
  88. (error, client) => {
  89. if (error) return done(error)
  90. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  91. return done()
  92. }
  93. )
  94. })
  95. })
  96. describe('when authorised readOnly', function () {
  97. before(function (done) {
  98. return async.series(
  99. [
  100. cb => {
  101. return FixturesManager.setUpProject(
  102. {
  103. privilegeLevel: 'readOnly',
  104. },
  105. (e, { project_id: projectId, user_id: userId }) => {
  106. this.project_id = projectId
  107. this.user_id = userId
  108. return cb(e)
  109. }
  110. )
  111. },
  112. cb => {
  113. return FixturesManager.setUpDoc(
  114. this.project_id,
  115. {
  116. lines: this.lines,
  117. version: this.version,
  118. ops: this.ops,
  119. ranges: this.ranges,
  120. },
  121. (e, { doc_id: docId }) => {
  122. this.doc_id = docId
  123. return cb(e)
  124. }
  125. )
  126. },
  127. cb => {
  128. this.client = RealTimeClient.connect(this.project_id, cb)
  129. },
  130. cb => {
  131. return this.client.emit(
  132. 'joinDoc',
  133. this.doc_id,
  134. (error, ...rest) => {
  135. ;[...this.returnedArgs] = Array.from(rest)
  136. return cb(error)
  137. }
  138. )
  139. },
  140. ],
  141. done
  142. )
  143. })
  144. it('should get the doc from the doc updater', function () {
  145. return MockDocUpdaterServer.getDocument
  146. .calledWith(this.project_id, this.doc_id, -1)
  147. .should.equal(true)
  148. })
  149. it('should return the doc lines, version, ranges and ops', function () {
  150. return this.returnedArgs.should.deep.equal([
  151. this.lines,
  152. this.version,
  153. this.ops,
  154. this.ranges,
  155. ])
  156. })
  157. return it('should have joined the doc room', function (done) {
  158. return RealTimeClient.getConnectedClient(
  159. this.client.socket.sessionid,
  160. (error, client) => {
  161. if (error) return done(error)
  162. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  163. return done()
  164. }
  165. )
  166. })
  167. })
  168. describe('when authorised as owner', function () {
  169. before(function (done) {
  170. return async.series(
  171. [
  172. cb => {
  173. return FixturesManager.setUpProject(
  174. {
  175. privilegeLevel: 'owner',
  176. },
  177. (e, { project_id: projectId, user_id: userId }) => {
  178. this.project_id = projectId
  179. this.user_id = userId
  180. return cb(e)
  181. }
  182. )
  183. },
  184. cb => {
  185. return FixturesManager.setUpDoc(
  186. this.project_id,
  187. {
  188. lines: this.lines,
  189. version: this.version,
  190. ops: this.ops,
  191. ranges: this.ranges,
  192. },
  193. (e, { doc_id: docId }) => {
  194. this.doc_id = docId
  195. return cb(e)
  196. }
  197. )
  198. },
  199. cb => {
  200. this.client = RealTimeClient.connect(this.project_id, cb)
  201. },
  202. cb => {
  203. return this.client.emit(
  204. 'joinDoc',
  205. this.doc_id,
  206. (error, ...rest) => {
  207. ;[...this.returnedArgs] = Array.from(rest)
  208. return cb(error)
  209. }
  210. )
  211. },
  212. ],
  213. done
  214. )
  215. })
  216. it('should get the doc from the doc updater', function () {
  217. return MockDocUpdaterServer.getDocument
  218. .calledWith(this.project_id, this.doc_id, -1)
  219. .should.equal(true)
  220. })
  221. it('should return the doc lines, version, ranges and ops', function () {
  222. return this.returnedArgs.should.deep.equal([
  223. this.lines,
  224. this.version,
  225. this.ops,
  226. this.ranges,
  227. ])
  228. })
  229. return it('should have joined the doc room', function (done) {
  230. return RealTimeClient.getConnectedClient(
  231. this.client.socket.sessionid,
  232. (error, client) => {
  233. if (error) return done(error)
  234. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  235. return done()
  236. }
  237. )
  238. })
  239. })
  240. // It is impossible to write an acceptance test to test joining an unauthorized
  241. // project, since joinProject already catches that. If you can join a project,
  242. // then you can join a doc in that project.
  243. describe('for an invalid doc', function () {
  244. before(function (done) {
  245. return async.series(
  246. [
  247. cb => {
  248. return FixturesManager.setUpProject(
  249. {
  250. privilegeLevel: 'owner',
  251. },
  252. (e, { project_id: projectId, user_id: userId }) => {
  253. this.project_id = projectId
  254. this.user_id = userId
  255. return cb(e)
  256. }
  257. )
  258. },
  259. cb => {
  260. return FixturesManager.setUpDoc(
  261. this.project_id,
  262. {
  263. lines: this.lines,
  264. version: this.version,
  265. ops: this.ops,
  266. ranges: this.ranges,
  267. },
  268. (e, { doc_id: docId }) => {
  269. this.doc_id = docId
  270. return cb(e)
  271. }
  272. )
  273. },
  274. cb => {
  275. this.client = RealTimeClient.connect(this.project_id, cb)
  276. },
  277. cb => {
  278. return this.client.emit(
  279. 'joinDoc',
  280. 'invalid-doc-id',
  281. (error, ...rest) => {
  282. this.error = error
  283. return cb()
  284. }
  285. )
  286. },
  287. ],
  288. done
  289. )
  290. })
  291. it('should not get the doc from the doc updater', function () {
  292. return MockDocUpdaterServer.getDocument
  293. .calledWith(this.project_id, 'invalid-doc-id')
  294. .should.equal(false)
  295. })
  296. it('should return an invalid id error', function () {
  297. this.error.message.should.equal('invalid id')
  298. })
  299. return it('should not have joined the doc room', function (done) {
  300. return RealTimeClient.getConnectedClient(
  301. this.client.socket.sessionid,
  302. (error, client) => {
  303. if (error) return done(error)
  304. expect(Array.from(client.rooms).includes('invalid-doc-id')).to.equal(
  305. false
  306. )
  307. return done()
  308. }
  309. )
  310. })
  311. })
  312. describe('with a fromVersion', function () {
  313. before(function (done) {
  314. this.fromVersion = 36
  315. return async.series(
  316. [
  317. cb => {
  318. return FixturesManager.setUpProject(
  319. {
  320. privilegeLevel: 'readAndWrite',
  321. },
  322. (e, { project_id: projectId, user_id: userId }) => {
  323. this.project_id = projectId
  324. this.user_id = userId
  325. return cb(e)
  326. }
  327. )
  328. },
  329. cb => {
  330. return FixturesManager.setUpDoc(
  331. this.project_id,
  332. {
  333. lines: this.lines,
  334. version: this.version,
  335. ops: this.ops,
  336. ranges: this.ranges,
  337. },
  338. (e, { doc_id: docId }) => {
  339. this.doc_id = docId
  340. return cb(e)
  341. }
  342. )
  343. },
  344. cb => {
  345. this.client = RealTimeClient.connect(this.project_id, cb)
  346. },
  347. cb => {
  348. return this.client.emit(
  349. 'joinDoc',
  350. this.doc_id,
  351. this.fromVersion,
  352. (error, ...rest) => {
  353. ;[...this.returnedArgs] = Array.from(rest)
  354. return cb(error)
  355. }
  356. )
  357. },
  358. ],
  359. done
  360. )
  361. })
  362. it('should get the doc from the doc updater with the fromVersion', function () {
  363. return MockDocUpdaterServer.getDocument
  364. .calledWith(this.project_id, this.doc_id, this.fromVersion)
  365. .should.equal(true)
  366. })
  367. it('should return the doc lines, version, ranges and ops', function () {
  368. return this.returnedArgs.should.deep.equal([
  369. this.lines,
  370. this.version,
  371. this.ops,
  372. this.ranges,
  373. ])
  374. })
  375. return it('should have joined the doc room', function (done) {
  376. return RealTimeClient.getConnectedClient(
  377. this.client.socket.sessionid,
  378. (error, client) => {
  379. if (error) return done(error)
  380. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  381. return done()
  382. }
  383. )
  384. })
  385. })
  386. describe('with options', function () {
  387. before(function (done) {
  388. this.options = { encodeRanges: true }
  389. return async.series(
  390. [
  391. cb => {
  392. return FixturesManager.setUpProject(
  393. {
  394. privilegeLevel: 'readAndWrite',
  395. },
  396. (e, { project_id: projectId, user_id: userId }) => {
  397. this.project_id = projectId
  398. this.user_id = userId
  399. return cb(e)
  400. }
  401. )
  402. },
  403. cb => {
  404. return FixturesManager.setUpDoc(
  405. this.project_id,
  406. {
  407. lines: this.lines,
  408. version: this.version,
  409. ops: this.ops,
  410. ranges: this.ranges,
  411. },
  412. (e, { doc_id: docId }) => {
  413. this.doc_id = docId
  414. return cb(e)
  415. }
  416. )
  417. },
  418. cb => {
  419. this.client = RealTimeClient.connect(this.project_id, cb)
  420. },
  421. cb => {
  422. return this.client.emit(
  423. 'joinDoc',
  424. this.doc_id,
  425. this.options,
  426. (error, ...rest) => {
  427. ;[...this.returnedArgs] = Array.from(rest)
  428. return cb(error)
  429. }
  430. )
  431. },
  432. ],
  433. done
  434. )
  435. })
  436. it('should get the doc from the doc updater with the default fromVersion', function () {
  437. return MockDocUpdaterServer.getDocument
  438. .calledWith(this.project_id, this.doc_id, -1)
  439. .should.equal(true)
  440. })
  441. it('should return the doc lines, version, ranges and ops', function () {
  442. return this.returnedArgs.should.deep.equal([
  443. this.lines,
  444. this.version,
  445. this.ops,
  446. this.ranges,
  447. ])
  448. })
  449. return it('should have joined the doc room', function (done) {
  450. return RealTimeClient.getConnectedClient(
  451. this.client.socket.sessionid,
  452. (error, client) => {
  453. if (error) return done(error)
  454. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  455. return done()
  456. }
  457. )
  458. })
  459. })
  460. return describe('with fromVersion and options', function () {
  461. before(function (done) {
  462. this.fromVersion = 36
  463. this.options = { encodeRanges: true }
  464. return async.series(
  465. [
  466. cb => {
  467. return FixturesManager.setUpProject(
  468. {
  469. privilegeLevel: 'readAndWrite',
  470. },
  471. (e, { project_id: projectId, user_id: userId }) => {
  472. this.project_id = projectId
  473. this.user_id = userId
  474. return cb(e)
  475. }
  476. )
  477. },
  478. cb => {
  479. return FixturesManager.setUpDoc(
  480. this.project_id,
  481. {
  482. lines: this.lines,
  483. version: this.version,
  484. ops: this.ops,
  485. ranges: this.ranges,
  486. },
  487. (e, { doc_id: docId }) => {
  488. this.doc_id = docId
  489. return cb(e)
  490. }
  491. )
  492. },
  493. cb => {
  494. this.client = RealTimeClient.connect(this.project_id, cb)
  495. },
  496. cb => {
  497. return this.client.emit(
  498. 'joinDoc',
  499. this.doc_id,
  500. this.fromVersion,
  501. this.options,
  502. (error, ...rest) => {
  503. ;[...this.returnedArgs] = Array.from(rest)
  504. return cb(error)
  505. }
  506. )
  507. },
  508. ],
  509. done
  510. )
  511. })
  512. it('should get the doc from the doc updater with the fromVersion', function () {
  513. return MockDocUpdaterServer.getDocument
  514. .calledWith(this.project_id, this.doc_id, this.fromVersion)
  515. .should.equal(true)
  516. })
  517. it('should return the doc lines, version, ranges and ops', function () {
  518. return this.returnedArgs.should.deep.equal([
  519. this.lines,
  520. this.version,
  521. this.ops,
  522. this.ranges,
  523. ])
  524. })
  525. return it('should have joined the doc room', function (done) {
  526. return RealTimeClient.getConnectedClient(
  527. this.client.socket.sessionid,
  528. (error, client) => {
  529. if (error) return done(error)
  530. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  531. return done()
  532. }
  533. )
  534. })
  535. })
  536. })