JoinDocTests.js 15 KB

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