JoinProjectTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /* eslint-disable
  2. camelcase,
  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 MockWebServer = require('./helpers/MockWebServer')
  15. const FixturesManager = require('./helpers/FixturesManager')
  16. const async = require('async')
  17. describe('joinProject', function () {
  18. describe('when authorized', function () {
  19. before(function (done) {
  20. return async.series(
  21. [
  22. cb => {
  23. return FixturesManager.setUpProject(
  24. {
  25. privilegeLevel: 'owner',
  26. project: {
  27. name: 'Test Project',
  28. },
  29. },
  30. (e, { project_id, user_id }) => {
  31. this.project_id = project_id
  32. this.user_id = user_id
  33. return cb(e)
  34. }
  35. )
  36. },
  37. cb => {
  38. this.client = RealTimeClient.connect()
  39. return this.client.on('connectionAccepted', cb)
  40. },
  41. cb => {
  42. return this.client.emit(
  43. 'joinProject',
  44. { project_id: this.project_id },
  45. (error, project, privilegeLevel, protocolVersion) => {
  46. this.project = project
  47. this.privilegeLevel = privilegeLevel
  48. this.protocolVersion = protocolVersion
  49. return cb(error)
  50. }
  51. )
  52. },
  53. ],
  54. done
  55. )
  56. })
  57. it('should get the project from web', function () {
  58. return MockWebServer.joinProject
  59. .calledWith(this.project_id, this.user_id)
  60. .should.equal(true)
  61. })
  62. it('should return the project', function () {
  63. return this.project.should.deep.equal({
  64. name: 'Test Project',
  65. })
  66. })
  67. it('should return the privilege level', function () {
  68. return this.privilegeLevel.should.equal('owner')
  69. })
  70. it('should return the protocolVersion', function () {
  71. return this.protocolVersion.should.equal(2)
  72. })
  73. it('should have joined the project room', function (done) {
  74. return RealTimeClient.getConnectedClient(
  75. this.client.socket.sessionid,
  76. (error, client) => {
  77. if (error) return done(error)
  78. expect(Array.from(client.rooms).includes(this.project_id)).to.equal(
  79. true
  80. )
  81. return done()
  82. }
  83. )
  84. })
  85. return it('should have marked the user as connected', function (done) {
  86. return this.client.emit(
  87. 'clientTracking.getConnectedUsers',
  88. (error, users) => {
  89. if (error) return done(error)
  90. let connected = false
  91. for (const user of Array.from(users)) {
  92. if (
  93. user.client_id === this.client.publicId &&
  94. user.user_id === this.user_id
  95. ) {
  96. connected = true
  97. break
  98. }
  99. }
  100. expect(connected).to.equal(true)
  101. return done()
  102. }
  103. )
  104. })
  105. })
  106. describe('when not authorized', function () {
  107. before(function (done) {
  108. return async.series(
  109. [
  110. cb => {
  111. return FixturesManager.setUpProject(
  112. {
  113. privilegeLevel: null,
  114. project: {
  115. name: 'Test Project',
  116. },
  117. },
  118. (e, { project_id, user_id }) => {
  119. this.project_id = project_id
  120. this.user_id = user_id
  121. return cb(e)
  122. }
  123. )
  124. },
  125. cb => {
  126. this.client = RealTimeClient.connect()
  127. return this.client.on('connectionAccepted', cb)
  128. },
  129. cb => {
  130. return this.client.emit(
  131. 'joinProject',
  132. { project_id: this.project_id },
  133. (error, project, privilegeLevel, protocolVersion) => {
  134. this.error = error
  135. this.project = project
  136. this.privilegeLevel = privilegeLevel
  137. this.protocolVersion = protocolVersion
  138. return cb()
  139. }
  140. )
  141. },
  142. ],
  143. done
  144. )
  145. })
  146. it('should return an error', function () {
  147. return this.error.message.should.equal('not authorized')
  148. })
  149. return it('should not have joined the project room', function (done) {
  150. return RealTimeClient.getConnectedClient(
  151. this.client.socket.sessionid,
  152. (error, client) => {
  153. if (error) return done(error)
  154. expect(Array.from(client.rooms).includes(this.project_id)).to.equal(
  155. false
  156. )
  157. return done()
  158. }
  159. )
  160. })
  161. })
  162. describe('when not authorized and web replies with a 403', function () {
  163. before(function (done) {
  164. return async.series(
  165. [
  166. cb => {
  167. return FixturesManager.setUpProject(
  168. {
  169. project_id: '403403403403403403403403', // forbidden
  170. privilegeLevel: 'owner',
  171. project: {
  172. name: 'Test Project',
  173. },
  174. },
  175. (e, { project_id, user_id }) => {
  176. this.project_id = project_id
  177. this.user_id = user_id
  178. cb(e)
  179. }
  180. )
  181. },
  182. cb => {
  183. this.client = RealTimeClient.connect()
  184. this.client.on('connectionAccepted', cb)
  185. },
  186. cb => {
  187. this.client.emit(
  188. 'joinProject',
  189. { project_id: this.project_id },
  190. (error, project, privilegeLevel, protocolVersion) => {
  191. this.error = error
  192. this.project = project
  193. this.privilegeLevel = privilegeLevel
  194. this.protocolVersion = protocolVersion
  195. cb()
  196. }
  197. )
  198. },
  199. ],
  200. done
  201. )
  202. })
  203. it('should return an error', function () {
  204. this.error.message.should.equal('not authorized')
  205. })
  206. it('should not have joined the project room', function (done) {
  207. RealTimeClient.getConnectedClient(
  208. this.client.socket.sessionid,
  209. (error, client) => {
  210. if (error) return done(error)
  211. expect(Array.from(client.rooms).includes(this.project_id)).to.equal(
  212. false
  213. )
  214. done()
  215. }
  216. )
  217. })
  218. })
  219. describe('when deleted and web replies with a 404', function () {
  220. before(function (done) {
  221. return async.series(
  222. [
  223. cb => {
  224. return FixturesManager.setUpProject(
  225. {
  226. project_id: '404404404404404404404404', // not-found
  227. privilegeLevel: 'owner',
  228. project: {
  229. name: 'Test Project',
  230. },
  231. },
  232. (e, { project_id, user_id }) => {
  233. this.project_id = project_id
  234. this.user_id = user_id
  235. cb(e)
  236. }
  237. )
  238. },
  239. cb => {
  240. this.client = RealTimeClient.connect()
  241. this.client.on('connectionAccepted', cb)
  242. },
  243. cb => {
  244. this.client.emit(
  245. 'joinProject',
  246. { project_id: this.project_id },
  247. (error, project, privilegeLevel, protocolVersion) => {
  248. this.error = error
  249. this.project = project
  250. this.privilegeLevel = privilegeLevel
  251. this.protocolVersion = protocolVersion
  252. cb()
  253. }
  254. )
  255. },
  256. ],
  257. done
  258. )
  259. })
  260. it('should return an error', function () {
  261. this.error.code.should.equal('ProjectNotFound')
  262. })
  263. it('should not have joined the project room', function (done) {
  264. RealTimeClient.getConnectedClient(
  265. this.client.socket.sessionid,
  266. (error, client) => {
  267. if (error) return done(error)
  268. expect(Array.from(client.rooms).includes(this.project_id)).to.equal(
  269. false
  270. )
  271. done()
  272. }
  273. )
  274. })
  275. })
  276. describe('when invalid', function () {
  277. before(function (done) {
  278. MockWebServer.joinProject.resetHistory()
  279. return async.series(
  280. [
  281. cb => {
  282. this.client = RealTimeClient.connect()
  283. return this.client.on('connectionAccepted', cb)
  284. },
  285. cb => {
  286. return this.client.emit(
  287. 'joinProject',
  288. { project_id: 'invalid-id' },
  289. error => {
  290. this.error = error
  291. return cb()
  292. }
  293. )
  294. },
  295. ],
  296. done
  297. )
  298. })
  299. it('should return an invalid id error', function () {
  300. this.error.message.should.equal('invalid id')
  301. })
  302. it('should not call to web', function () {
  303. MockWebServer.joinProject.called.should.equal(false)
  304. })
  305. })
  306. describe('when joining more than one project', function () {
  307. before(function (done) {
  308. return async.series(
  309. [
  310. cb => {
  311. return FixturesManager.setUpProject(
  312. {
  313. privilegeLevel: 'owner',
  314. project: {
  315. name: 'Other Project',
  316. },
  317. },
  318. (e, { project_id, user_id }) => {
  319. this.other_project_id = project_id
  320. this.other_user_id = user_id
  321. return cb(e)
  322. }
  323. )
  324. },
  325. cb => {
  326. return FixturesManager.setUpProject(
  327. {
  328. user_id: this.other_user_id,
  329. privilegeLevel: 'owner',
  330. project: {
  331. name: 'Test Project',
  332. },
  333. },
  334. (e, { project_id, user_id }) => {
  335. this.project_id = project_id
  336. this.user_id = user_id
  337. return cb(e)
  338. }
  339. )
  340. },
  341. cb => {
  342. this.client = RealTimeClient.connect()
  343. return this.client.on('connectionAccepted', cb)
  344. },
  345. cb => {
  346. return this.client.emit(
  347. 'joinProject',
  348. { project_id: this.project_id },
  349. (error, project, privilegeLevel, protocolVersion) => {
  350. this.project = project
  351. this.privilegeLevel = privilegeLevel
  352. this.protocolVersion = protocolVersion
  353. return cb(error)
  354. }
  355. )
  356. },
  357. cb => {
  358. return this.client.emit(
  359. 'joinProject',
  360. { project_id: this.other_project_id },
  361. error => {
  362. this.error = error
  363. return cb()
  364. }
  365. )
  366. },
  367. ],
  368. done
  369. )
  370. })
  371. return it('should return an error', function () {
  372. this.error.message.should.equal('cannot join multiple projects')
  373. })
  374. })
  375. return describe('when over rate limit', function () {
  376. before(function (done) {
  377. return async.series(
  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: '429429429429429429429429' }, // rate-limited
  387. error => {
  388. this.error = error
  389. return cb()
  390. }
  391. )
  392. },
  393. ],
  394. done
  395. )
  396. })
  397. return it('should return a TooManyRequests error code', function () {
  398. this.error.message.should.equal('rate-limit hit when joining project')
  399. return this.error.code.should.equal('TooManyRequests')
  400. })
  401. })
  402. })