JoinDocTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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('with a fromVersion', function () {
  267. before(function (done) {
  268. this.fromVersion = 36
  269. return async.series(
  270. [
  271. (cb) => {
  272. return FixturesManager.setUpProject(
  273. {
  274. privilegeLevel: 'readAndWrite'
  275. },
  276. (e, { project_id, user_id }) => {
  277. this.project_id = project_id
  278. this.user_id = user_id
  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 }) => {
  293. this.doc_id = doc_id
  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. this.doc_id,
  313. this.fromVersion,
  314. (error, ...rest) => {
  315. ;[...this.returnedArgs] = Array.from(rest)
  316. return cb(error)
  317. }
  318. )
  319. }
  320. ],
  321. done
  322. )
  323. })
  324. it('should get the doc from the doc updater with the fromVersion', function () {
  325. return MockDocUpdaterServer.getDocument
  326. .calledWith(this.project_id, this.doc_id, this.fromVersion)
  327. .should.equal(true)
  328. })
  329. it('should return the doc lines, version, ranges and ops', function () {
  330. return this.returnedArgs.should.deep.equal([
  331. this.lines,
  332. this.version,
  333. this.ops,
  334. this.ranges
  335. ])
  336. })
  337. return it('should have joined the doc room', function (done) {
  338. return RealTimeClient.getConnectedClient(
  339. this.client.socket.sessionid,
  340. (error, client) => {
  341. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  342. return done()
  343. }
  344. )
  345. })
  346. })
  347. describe('with options', function () {
  348. before(function (done) {
  349. this.options = { encodeRanges: true }
  350. return async.series(
  351. [
  352. (cb) => {
  353. return FixturesManager.setUpProject(
  354. {
  355. privilegeLevel: 'readAndWrite'
  356. },
  357. (e, { project_id, user_id }) => {
  358. this.project_id = project_id
  359. this.user_id = user_id
  360. return cb(e)
  361. }
  362. )
  363. },
  364. (cb) => {
  365. return FixturesManager.setUpDoc(
  366. this.project_id,
  367. {
  368. lines: this.lines,
  369. version: this.version,
  370. ops: this.ops,
  371. ranges: this.ranges
  372. },
  373. (e, { doc_id }) => {
  374. this.doc_id = doc_id
  375. return cb(e)
  376. }
  377. )
  378. },
  379. (cb) => {
  380. this.client = RealTimeClient.connect()
  381. return this.client.on('connectionAccepted', cb)
  382. },
  383. (cb) => {
  384. return this.client.emit(
  385. 'joinProject',
  386. { project_id: this.project_id },
  387. cb
  388. )
  389. },
  390. (cb) => {
  391. return this.client.emit(
  392. 'joinDoc',
  393. this.doc_id,
  394. this.options,
  395. (error, ...rest) => {
  396. ;[...this.returnedArgs] = Array.from(rest)
  397. return cb(error)
  398. }
  399. )
  400. }
  401. ],
  402. done
  403. )
  404. })
  405. it('should get the doc from the doc updater with the default fromVersion', function () {
  406. return MockDocUpdaterServer.getDocument
  407. .calledWith(this.project_id, this.doc_id, -1)
  408. .should.equal(true)
  409. })
  410. it('should return the doc lines, version, ranges and ops', function () {
  411. return this.returnedArgs.should.deep.equal([
  412. this.lines,
  413. this.version,
  414. this.ops,
  415. this.ranges
  416. ])
  417. })
  418. return it('should have joined the doc room', function (done) {
  419. return RealTimeClient.getConnectedClient(
  420. this.client.socket.sessionid,
  421. (error, client) => {
  422. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  423. return done()
  424. }
  425. )
  426. })
  427. })
  428. return describe('with fromVersion and options', function () {
  429. before(function (done) {
  430. this.fromVersion = 36
  431. this.options = { encodeRanges: true }
  432. return async.series(
  433. [
  434. (cb) => {
  435. return FixturesManager.setUpProject(
  436. {
  437. privilegeLevel: 'readAndWrite'
  438. },
  439. (e, { project_id, user_id }) => {
  440. this.project_id = project_id
  441. this.user_id = user_id
  442. return cb(e)
  443. }
  444. )
  445. },
  446. (cb) => {
  447. return FixturesManager.setUpDoc(
  448. this.project_id,
  449. {
  450. lines: this.lines,
  451. version: this.version,
  452. ops: this.ops,
  453. ranges: this.ranges
  454. },
  455. (e, { doc_id }) => {
  456. this.doc_id = doc_id
  457. return cb(e)
  458. }
  459. )
  460. },
  461. (cb) => {
  462. this.client = RealTimeClient.connect()
  463. return this.client.on('connectionAccepted', cb)
  464. },
  465. (cb) => {
  466. return this.client.emit(
  467. 'joinProject',
  468. { project_id: this.project_id },
  469. cb
  470. )
  471. },
  472. (cb) => {
  473. return this.client.emit(
  474. 'joinDoc',
  475. this.doc_id,
  476. this.fromVersion,
  477. this.options,
  478. (error, ...rest) => {
  479. ;[...this.returnedArgs] = Array.from(rest)
  480. return cb(error)
  481. }
  482. )
  483. }
  484. ],
  485. done
  486. )
  487. })
  488. it('should get the doc from the doc updater with the fromVersion', function () {
  489. return MockDocUpdaterServer.getDocument
  490. .calledWith(this.project_id, this.doc_id, this.fromVersion)
  491. .should.equal(true)
  492. })
  493. it('should return the doc lines, version, ranges and ops', function () {
  494. return this.returnedArgs.should.deep.equal([
  495. this.lines,
  496. this.version,
  497. this.ops,
  498. this.ranges
  499. ])
  500. })
  501. return it('should have joined the doc room', function (done) {
  502. return RealTimeClient.getConnectedClient(
  503. this.client.socket.sessionid,
  504. (error, client) => {
  505. expect(Array.from(client.rooms).includes(this.doc_id)).to.equal(true)
  506. return done()
  507. }
  508. )
  509. })
  510. })
  511. })