JoinProjectTests.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Fix any style issues and re-enable lint.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS101: Remove unnecessary use of Array.from
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  8. */
  9. import { expect } from 'chai'
  10. import RealTimeClient from './helpers/RealTimeClient.js'
  11. import MockWebServer from './helpers/MockWebServer.js'
  12. import FixturesManager from './helpers/FixturesManager.js'
  13. import async from 'async'
  14. describe('joinProject', function () {
  15. describe('when authorized', function () {
  16. before(function (done) {
  17. return async.series(
  18. [
  19. cb => {
  20. return FixturesManager.setUpProject(
  21. {
  22. privilegeLevel: 'owner',
  23. project: {
  24. name: 'Test Project',
  25. },
  26. },
  27. (e, { project_id: projectId, user_id: userId }) => {
  28. this.project_id = projectId
  29. this.user_id = userId
  30. return cb(e)
  31. }
  32. )
  33. },
  34. cb => {
  35. this.client = RealTimeClient.connect(
  36. this.project_id,
  37. (error, project, privilegeLevel, protocolVersion) => {
  38. this.project = project
  39. this.privilegeLevel = privilegeLevel
  40. this.protocolVersion = protocolVersion
  41. return cb(error)
  42. }
  43. )
  44. },
  45. ],
  46. done
  47. )
  48. })
  49. it('should get the project from web', function () {
  50. return MockWebServer.joinProject
  51. .calledWith(this.project_id, this.user_id)
  52. .should.equal(true)
  53. })
  54. it('should return the project', function () {
  55. return this.project.should.deep.equal({
  56. name: 'Test Project',
  57. owner: { _id: this.user_id },
  58. })
  59. })
  60. it('should return the privilege level', function () {
  61. return this.privilegeLevel.should.equal('owner')
  62. })
  63. it('should return the protocolVersion', function () {
  64. return this.protocolVersion.should.equal(2)
  65. })
  66. it('should have joined the project room', function (done) {
  67. return RealTimeClient.getConnectedClient(
  68. this.client.socket.sessionid,
  69. (error, client) => {
  70. if (error) return done(error)
  71. expect(Array.from(client.rooms).includes(this.project_id)).to.equal(
  72. true
  73. )
  74. return done()
  75. }
  76. )
  77. })
  78. return it('should have marked the user as connected', function (done) {
  79. return this.client.emit(
  80. 'clientTracking.getConnectedUsers',
  81. (error, users) => {
  82. if (error) return done(error)
  83. let connected = false
  84. for (const user of Array.from(users)) {
  85. if (
  86. user.client_id === this.client.publicId &&
  87. user.user_id === this.user_id
  88. ) {
  89. connected = true
  90. break
  91. }
  92. }
  93. expect(connected).to.equal(true)
  94. return done()
  95. }
  96. )
  97. })
  98. })
  99. describe('when authorized with token', function () {
  100. before(function (done) {
  101. async.series(
  102. [
  103. cb => {
  104. FixturesManager.setUpProject(
  105. {
  106. privilegeLevel: 'owner',
  107. publicAccess: 'readOnly',
  108. project: {
  109. name: 'Test Project',
  110. },
  111. },
  112. (
  113. e,
  114. {
  115. user_id: ownerId,
  116. project_id: projectId,
  117. anonymousAccessToken,
  118. }
  119. ) => {
  120. this.ownerId = ownerId
  121. this.project_id = projectId
  122. this.anonymousAccessToken = anonymousAccessToken
  123. cb(e)
  124. }
  125. )
  126. },
  127. cb => {
  128. RealTimeClient.setAnonSession(
  129. this.project_id,
  130. this.anonymousAccessToken,
  131. cb
  132. )
  133. },
  134. cb => {
  135. this.client = RealTimeClient.connect(
  136. this.project_id,
  137. (error, project, privilegeLevel, protocolVersion) => {
  138. this.project = project
  139. this.privilegeLevel = privilegeLevel
  140. this.protocolVersion = protocolVersion
  141. cb(error)
  142. }
  143. )
  144. },
  145. ],
  146. done
  147. )
  148. })
  149. it('should get the project from web', function () {
  150. MockWebServer.joinProject
  151. .calledWith(
  152. this.project_id,
  153. 'anonymous-user',
  154. this.anonymousAccessToken
  155. )
  156. .should.equal(true)
  157. })
  158. it('should return the project', function () {
  159. this.project.should.deep.equal({
  160. name: 'Test Project',
  161. owner: { _id: this.ownerId },
  162. })
  163. })
  164. it('should return the privilege level', function () {
  165. this.privilegeLevel.should.equal('readOnly')
  166. })
  167. it('should return the protocolVersion', function () {
  168. this.protocolVersion.should.equal(2)
  169. })
  170. it('should have joined the project room', function (done) {
  171. RealTimeClient.getConnectedClient(
  172. this.client.socket.sessionid,
  173. (error, client) => {
  174. if (error) return done(error)
  175. expect(Array.from(client.rooms).includes(this.project_id)).to.equal(
  176. true
  177. )
  178. done()
  179. }
  180. )
  181. })
  182. it('should have marked the user as connected', function (done) {
  183. this.client.emit('clientTracking.getConnectedUsers', (error, users) => {
  184. if (error) return done(error)
  185. let connected = false
  186. for (const user of Array.from(users)) {
  187. if (user.client_id === this.client.publicId) {
  188. connected = true
  189. break
  190. }
  191. }
  192. expect(connected).to.equal(true)
  193. done()
  194. })
  195. })
  196. })
  197. describe('when not authorized', function () {
  198. before(function (done) {
  199. return async.series(
  200. [
  201. cb => {
  202. return FixturesManager.setUpProject(
  203. {
  204. privilegeLevel: null,
  205. project: {
  206. name: 'Test Project',
  207. },
  208. },
  209. (e, { project_id: projectId, user_id: userId }) => {
  210. this.project_id = projectId
  211. this.user_id = userId
  212. return cb(e)
  213. }
  214. )
  215. },
  216. cb => {
  217. this.client = RealTimeClient.connect(
  218. this.project_id,
  219. (error, project, privilegeLevel, protocolVersion) => {
  220. this.error = error
  221. this.project = project
  222. this.privilegeLevel = privilegeLevel
  223. this.protocolVersion = protocolVersion
  224. return cb()
  225. }
  226. )
  227. },
  228. ],
  229. done
  230. )
  231. })
  232. it('should return an error', function () {
  233. return this.error.message.should.equal('not authorized')
  234. })
  235. return it('should not have joined the project room', function (done) {
  236. return RealTimeClient.getConnectedClient(
  237. this.client.socket.sessionid,
  238. error => {
  239. expect(error.message).to.equal('not found')
  240. return done()
  241. }
  242. )
  243. })
  244. })
  245. describe('when not authorized and web replies with a 403', function () {
  246. before(function (done) {
  247. return async.series(
  248. [
  249. cb => {
  250. return FixturesManager.setUpProject(
  251. {
  252. project_id: '403403403403403403403403', // forbidden
  253. privilegeLevel: 'owner',
  254. project: {
  255. name: 'Test Project',
  256. },
  257. },
  258. (e, { project_id: projectId, user_id: userId }) => {
  259. this.project_id = projectId
  260. this.user_id = userId
  261. cb(e)
  262. }
  263. )
  264. },
  265. cb => {
  266. this.client = RealTimeClient.connect(this.project_id, error => {
  267. this.error = error
  268. cb()
  269. })
  270. },
  271. ],
  272. done
  273. )
  274. })
  275. it('should return an error', function () {
  276. this.error.message.should.equal('not authorized')
  277. })
  278. it('should not have joined the project room', function (done) {
  279. RealTimeClient.getConnectedClient(this.client.socket.sessionid, error => {
  280. expect(error.message).to.equal('not found')
  281. done()
  282. })
  283. })
  284. })
  285. describe('when deleted and web replies with a 404', function () {
  286. before(function (done) {
  287. return async.series(
  288. [
  289. cb => {
  290. return FixturesManager.setUpProject(
  291. {
  292. project_id: '404404404404404404404404', // not-found
  293. privilegeLevel: 'owner',
  294. project: {
  295. name: 'Test Project',
  296. },
  297. },
  298. (e, { project_id: projectId, user_id: userId }) => {
  299. this.project_id = projectId
  300. this.user_id = userId
  301. cb(e)
  302. }
  303. )
  304. },
  305. cb => {
  306. this.client = RealTimeClient.connect(this.project_id, error => {
  307. this.error = error
  308. cb()
  309. })
  310. },
  311. ],
  312. done
  313. )
  314. })
  315. it('should return an error', function () {
  316. this.error.code.should.equal('ProjectNotFound')
  317. })
  318. it('should not have joined the project room', function (done) {
  319. RealTimeClient.getConnectedClient(this.client.socket.sessionid, error => {
  320. expect(error.message).to.equal('not found')
  321. done()
  322. })
  323. })
  324. })
  325. describe('when invalid', function () {
  326. before(function (done) {
  327. MockWebServer.joinProject.resetHistory()
  328. return async.series(
  329. [
  330. cb => {
  331. this.client = RealTimeClient.connect('invalid-id', error => {
  332. this.error = error
  333. return cb()
  334. })
  335. },
  336. ],
  337. done
  338. )
  339. })
  340. it('should return an invalid id error', function () {
  341. this.error.message.should.equal(
  342. 'missing/bad ?projectId=... query flag on handshake'
  343. )
  344. })
  345. it('should not call to web', function () {
  346. MockWebServer.joinProject.called.should.equal(false)
  347. })
  348. })
  349. describe('when over rate limit', function () {
  350. before(function (done) {
  351. return async.series(
  352. [
  353. cb => {
  354. this.client = RealTimeClient.connect(
  355. '429429429429429429429429', // rate-limited
  356. error => {
  357. this.error = error
  358. return cb()
  359. }
  360. )
  361. },
  362. ],
  363. done
  364. )
  365. })
  366. return it('should return a TooManyRequests error code', function () {
  367. this.error.message.should.equal('rate-limit hit when joining project')
  368. return this.error.code.should.equal('TooManyRequests')
  369. })
  370. })
  371. describe('when automatically joining the project', function () {
  372. describe('when authorized', function () {
  373. before(function (done) {
  374. async.series(
  375. [
  376. cb => {
  377. FixturesManager.setUpProject(
  378. {
  379. privilegeLevel: 'owner',
  380. project: {
  381. name: 'Test Project',
  382. },
  383. },
  384. (e, { project_id: projectId, user_id: userId }) => {
  385. this.project_id = projectId
  386. this.user_id = userId
  387. cb(e)
  388. }
  389. )
  390. },
  391. cb => {
  392. this.client = RealTimeClient.connect(
  393. this.project_id,
  394. (err, project, permissionsLevel, protocolVersion) => {
  395. this.project = project
  396. this.permissionsLevel = permissionsLevel
  397. this.protocolVersion = protocolVersion
  398. cb(err)
  399. }
  400. )
  401. },
  402. ],
  403. done
  404. )
  405. })
  406. it('should get the project from web', function () {
  407. MockWebServer.joinProject
  408. .calledWith(this.project_id, this.user_id)
  409. .should.equal(true)
  410. })
  411. it('should return the project', function () {
  412. this.project.should.deep.equal({
  413. name: 'Test Project',
  414. owner: { _id: this.user_id },
  415. })
  416. })
  417. it('should return the privilege level', function () {
  418. this.permissionsLevel.should.equal('owner')
  419. })
  420. it('should return the protocolVersion', function () {
  421. this.protocolVersion.should.equal(2)
  422. })
  423. it('should have joined the project room', function (done) {
  424. RealTimeClient.getConnectedClient(
  425. this.client.socket.sessionid,
  426. (error, client) => {
  427. if (error) return done(error)
  428. expect(Array.from(client.rooms).includes(this.project_id)).to.equal(
  429. true
  430. )
  431. done()
  432. }
  433. )
  434. })
  435. it('should have marked the user as connected', function (done) {
  436. this.client.emit('clientTracking.getConnectedUsers', (error, users) => {
  437. if (error) return done(error)
  438. let connected = false
  439. for (const user of Array.from(users)) {
  440. if (
  441. user.client_id === this.client.publicId &&
  442. user.user_id === this.user_id
  443. ) {
  444. connected = true
  445. break
  446. }
  447. }
  448. expect(connected).to.equal(true)
  449. done()
  450. })
  451. })
  452. })
  453. describe('when authorized with token', function () {
  454. before(function (done) {
  455. async.series(
  456. [
  457. cb => {
  458. FixturesManager.setUpProject(
  459. {
  460. privilegeLevel: 'owner',
  461. publicAccess: 'readOnly',
  462. project: {
  463. name: 'Test Project',
  464. },
  465. },
  466. (
  467. e,
  468. {
  469. user_id: ownerId,
  470. project_id: projectId,
  471. anonymousAccessToken,
  472. }
  473. ) => {
  474. this.ownerId = ownerId
  475. this.project_id = projectId
  476. this.anonymousAccessToken = anonymousAccessToken
  477. cb(e)
  478. }
  479. )
  480. },
  481. cb => {
  482. RealTimeClient.setAnonSession(
  483. this.project_id,
  484. this.anonymousAccessToken,
  485. cb
  486. )
  487. },
  488. cb => {
  489. this.client = RealTimeClient.connect(
  490. this.project_id,
  491. (err, project, permissionsLevel, protocolVersion) => {
  492. this.project = project
  493. this.permissionsLevel = permissionsLevel
  494. this.protocolVersion = protocolVersion
  495. cb(err)
  496. }
  497. )
  498. },
  499. ],
  500. done
  501. )
  502. })
  503. it('should get the project from web', function () {
  504. MockWebServer.joinProject
  505. .calledWith(
  506. this.project_id,
  507. 'anonymous-user',
  508. this.anonymousAccessToken
  509. )
  510. .should.equal(true)
  511. })
  512. it('should return the project', function () {
  513. this.project.should.deep.equal({
  514. name: 'Test Project',
  515. owner: { _id: this.ownerId },
  516. })
  517. })
  518. it('should return the privilege level', function () {
  519. this.permissionsLevel.should.equal('readOnly')
  520. })
  521. it('should return the protocolVersion', function () {
  522. this.protocolVersion.should.equal(2)
  523. })
  524. it('should have joined the project room', function (done) {
  525. RealTimeClient.getConnectedClient(
  526. this.client.socket.sessionid,
  527. (error, client) => {
  528. if (error) return done(error)
  529. expect(Array.from(client.rooms).includes(this.project_id)).to.equal(
  530. true
  531. )
  532. done()
  533. }
  534. )
  535. })
  536. it('should have marked the user as connected', function (done) {
  537. this.client.emit('clientTracking.getConnectedUsers', (error, users) => {
  538. if (error) return done(error)
  539. let connected = false
  540. for (const user of Array.from(users)) {
  541. if (user.client_id === this.client.publicId) {
  542. connected = true
  543. break
  544. }
  545. }
  546. expect(connected).to.equal(true)
  547. done()
  548. })
  549. })
  550. })
  551. describe('when not authorized', function () {
  552. let joinProjectResponseReceived = false
  553. before(function (done) {
  554. async.series(
  555. [
  556. cb => {
  557. FixturesManager.setUpProject(
  558. {
  559. privilegeLevel: null,
  560. project: {
  561. name: 'Test Project',
  562. },
  563. },
  564. (e, { project_id: projectId, user_id: userId }) => {
  565. this.project_id = projectId
  566. this.user_id = userId
  567. cb(e)
  568. }
  569. )
  570. },
  571. cb => {
  572. this.client = RealTimeClient.connect(this.project_id, err => {
  573. this.error = err
  574. cb()
  575. })
  576. this.client.on('joinProjectResponse', () => {
  577. joinProjectResponseReceived = true
  578. cb()
  579. })
  580. },
  581. ],
  582. done
  583. )
  584. })
  585. it('should not emit joinProjectResponse', function () {
  586. expect(joinProjectResponseReceived).to.equal(false)
  587. })
  588. it('should have disconnected the client', function () {
  589. expect(this.client.socket.connected).to.equal(false)
  590. })
  591. it('should return an error', function () {
  592. this.error.message.should.equal('not authorized')
  593. })
  594. it('should not have joined the project room', function (done) {
  595. RealTimeClient.getConnectedClient(
  596. this.client.socket.sessionid,
  597. error => {
  598. expect(error.message).to.equal('not found')
  599. done()
  600. }
  601. )
  602. })
  603. })
  604. describe('when not authorized and web replies with a 403', function () {
  605. let joinProjectResponseReceived = false
  606. before(function (done) {
  607. async.series(
  608. [
  609. cb => {
  610. FixturesManager.setUpProject(
  611. {
  612. project_id: '403403403403403403403403', // forbidden
  613. privilegeLevel: 'owner',
  614. project: {
  615. name: 'Test Project',
  616. },
  617. },
  618. (e, { project_id: projectId, user_id: userId }) => {
  619. this.project_id = projectId
  620. this.user_id = userId
  621. cb(e)
  622. }
  623. )
  624. },
  625. cb => {
  626. this.client = RealTimeClient.connect(this.project_id, err => {
  627. this.error = err
  628. cb()
  629. })
  630. this.client.on('joinProjectResponse', () => {
  631. joinProjectResponseReceived = true
  632. cb()
  633. })
  634. },
  635. ],
  636. done
  637. )
  638. })
  639. it('should not emit joinProjectResponse', function () {
  640. expect(joinProjectResponseReceived).to.equal(false)
  641. })
  642. it('should have disconnected the client', function () {
  643. expect(this.client.socket.connected).to.equal(false)
  644. })
  645. it('should return an error', function () {
  646. this.error.message.should.equal('not authorized')
  647. })
  648. it('should not have joined the project room', function (done) {
  649. RealTimeClient.getConnectedClient(
  650. this.client.socket.sessionid,
  651. error => {
  652. expect(error.message).to.equal('not found')
  653. done()
  654. }
  655. )
  656. })
  657. })
  658. describe('when deleted and web replies with a 404', function () {
  659. let joinProjectResponseReceived = false
  660. before(function (done) {
  661. async.series(
  662. [
  663. cb => {
  664. FixturesManager.setUpProject(
  665. {
  666. project_id: '404404404404404404404404', // not-found
  667. privilegeLevel: 'owner',
  668. project: {
  669. name: 'Test Project',
  670. },
  671. },
  672. (e, { project_id: projectId, user_id: userId }) => {
  673. this.project_id = projectId
  674. this.user_id = userId
  675. cb(e)
  676. }
  677. )
  678. },
  679. cb => {
  680. this.client = RealTimeClient.connect(this.project_id, err => {
  681. this.error = err
  682. cb()
  683. })
  684. this.client.on('joinProjectResponse', () => {
  685. joinProjectResponseReceived = true
  686. cb()
  687. })
  688. },
  689. ],
  690. done
  691. )
  692. })
  693. it('should not emit joinProjectResponse', function () {
  694. expect(joinProjectResponseReceived).to.equal(false)
  695. })
  696. it('should have disconnected the client', function () {
  697. expect(this.client.socket.connected).to.equal(false)
  698. })
  699. it('should return an error', function () {
  700. this.error.code.should.equal('ProjectNotFound')
  701. })
  702. it('should not have joined the project room', function (done) {
  703. RealTimeClient.getConnectedClient(
  704. this.client.socket.sessionid,
  705. error => {
  706. expect(error.message).to.equal('not found')
  707. done()
  708. }
  709. )
  710. })
  711. })
  712. describe('when invalid', function () {
  713. let joinProjectResponseReceived = false
  714. before(function (done) {
  715. MockWebServer.joinProject.resetHistory()
  716. async.series(
  717. [
  718. cb => {
  719. this.client = RealTimeClient.connect('invalid-id', err => {
  720. this.error = err
  721. cb()
  722. })
  723. this.client.on('joinProjectResponse', () => {
  724. joinProjectResponseReceived = true
  725. cb()
  726. })
  727. },
  728. ],
  729. done
  730. )
  731. })
  732. it('should not emit joinProjectResponse', function () {
  733. expect(joinProjectResponseReceived).to.equal(false)
  734. })
  735. it('should have disconnected the client', function () {
  736. expect(this.client.socket.connected).to.equal(false)
  737. })
  738. it('should return an invalid id error', function () {
  739. this.error.message.should.equal(
  740. 'missing/bad ?projectId=... query flag on handshake'
  741. )
  742. })
  743. it('should not call to web', function () {
  744. MockWebServer.joinProject.called.should.equal(false)
  745. })
  746. })
  747. describe('when over rate limit', function () {
  748. let joinProjectResponseReceived = false
  749. before(function (done) {
  750. async.series(
  751. [
  752. cb => {
  753. this.client = RealTimeClient.connect(
  754. '429429429429429429429429',
  755. err => {
  756. this.error = err
  757. cb()
  758. }
  759. )
  760. this.client.on('joinProjectResponse', () => {
  761. joinProjectResponseReceived = true
  762. cb()
  763. })
  764. },
  765. ],
  766. done
  767. )
  768. })
  769. it('should not emit joinProjectResponse', function () {
  770. expect(joinProjectResponseReceived).to.equal(false)
  771. })
  772. it('should have disconnected the client', function () {
  773. expect(this.client.socket.connected).to.equal(false)
  774. })
  775. it('should return a TooManyRequests error code', function () {
  776. this.error.message.should.equal('rate-limit hit when joining project')
  777. this.error.code.should.equal('TooManyRequests')
  778. })
  779. })
  780. })
  781. })