JoinProjectTests.js 12 KB

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