JoinProjectTests.js 12 KB

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