JoinProjectTests.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const { expect } = require('chai')
  14. const RealTimeClient = require('./helpers/RealTimeClient')
  15. const MockWebServer = require('./helpers/MockWebServer')
  16. const FixturesManager = require('./helpers/FixturesManager')
  17. const async = require('async')
  18. describe('joinProject', function () {
  19. describe('when authorized', function () {
  20. before(function (done) {
  21. return async.series(
  22. [
  23. cb => {
  24. return FixturesManager.setUpProject(
  25. {
  26. privilegeLevel: 'owner',
  27. project: {
  28. name: 'Test Project',
  29. },
  30. },
  31. (e, { project_id, user_id }) => {
  32. this.project_id = project_id
  33. this.user_id = user_id
  34. return cb(e)
  35. }
  36. )
  37. },
  38. cb => {
  39. this.client = RealTimeClient.connect()
  40. return this.client.on('connectionAccepted', cb)
  41. },
  42. cb => {
  43. return this.client.emit(
  44. 'joinProject',
  45. { project_id: this.project_id },
  46. (error, project, privilegeLevel, protocolVersion) => {
  47. this.project = project
  48. this.privilegeLevel = privilegeLevel
  49. this.protocolVersion = protocolVersion
  50. return cb(error)
  51. }
  52. )
  53. },
  54. ],
  55. done
  56. )
  57. })
  58. it('should get the project from web', function () {
  59. return MockWebServer.joinProject
  60. .calledWith(this.project_id, this.user_id)
  61. .should.equal(true)
  62. })
  63. it('should return the project', function () {
  64. return this.project.should.deep.equal({
  65. name: 'Test Project',
  66. })
  67. })
  68. it('should return the privilege level', function () {
  69. return this.privilegeLevel.should.equal('owner')
  70. })
  71. it('should return the protocolVersion', function () {
  72. return this.protocolVersion.should.equal(2)
  73. })
  74. it('should have joined the project room', function (done) {
  75. return RealTimeClient.getConnectedClient(
  76. this.client.socket.sessionid,
  77. (error, client) => {
  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. let connected = false
  90. for (const user of Array.from(users)) {
  91. if (
  92. user.client_id === this.client.publicId &&
  93. user.user_id === this.user_id
  94. ) {
  95. connected = true
  96. break
  97. }
  98. }
  99. expect(connected).to.equal(true)
  100. return done()
  101. }
  102. )
  103. })
  104. })
  105. describe('when not authorized', function () {
  106. before(function (done) {
  107. return async.series(
  108. [
  109. cb => {
  110. return FixturesManager.setUpProject(
  111. {
  112. privilegeLevel: null,
  113. project: {
  114. name: 'Test Project',
  115. },
  116. },
  117. (e, { project_id, user_id }) => {
  118. this.project_id = project_id
  119. this.user_id = user_id
  120. return cb(e)
  121. }
  122. )
  123. },
  124. cb => {
  125. this.client = RealTimeClient.connect()
  126. return this.client.on('connectionAccepted', cb)
  127. },
  128. cb => {
  129. return this.client.emit(
  130. 'joinProject',
  131. { project_id: this.project_id },
  132. (error, project, privilegeLevel, protocolVersion) => {
  133. this.error = error
  134. this.project = project
  135. this.privilegeLevel = privilegeLevel
  136. this.protocolVersion = protocolVersion
  137. return cb()
  138. }
  139. )
  140. },
  141. ],
  142. done
  143. )
  144. })
  145. it('should return an error', function () {
  146. return this.error.message.should.equal('not authorized')
  147. })
  148. return it('should not have joined the project room', function (done) {
  149. return RealTimeClient.getConnectedClient(
  150. this.client.socket.sessionid,
  151. (error, client) => {
  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, user_id }) => {
  174. this.project_id = project_id
  175. this.user_id = user_id
  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. 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, user_id }) => {
  230. this.project_id = project_id
  231. this.user_id = user_id
  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. expect(Array.from(client.rooms).includes(this.project_id)).to.equal(
  265. false
  266. )
  267. done()
  268. }
  269. )
  270. })
  271. })
  272. describe('when invalid', function () {
  273. before(function (done) {
  274. MockWebServer.joinProject.resetHistory()
  275. return async.series(
  276. [
  277. cb => {
  278. this.client = RealTimeClient.connect()
  279. return this.client.on('connectionAccepted', cb)
  280. },
  281. cb => {
  282. return this.client.emit(
  283. 'joinProject',
  284. { project_id: 'invalid-id' },
  285. error => {
  286. this.error = error
  287. return cb()
  288. }
  289. )
  290. },
  291. ],
  292. done
  293. )
  294. })
  295. it('should return an invalid id error', function () {
  296. this.error.message.should.equal('invalid id')
  297. })
  298. it('should not call to web', function () {
  299. MockWebServer.joinProject.called.should.equal(false)
  300. })
  301. })
  302. describe('when joining more than one project', function () {
  303. before(function (done) {
  304. return async.series(
  305. [
  306. cb => {
  307. return FixturesManager.setUpProject(
  308. {
  309. privilegeLevel: 'owner',
  310. project: {
  311. name: 'Other Project',
  312. },
  313. },
  314. (e, { project_id, user_id }) => {
  315. this.other_project_id = project_id
  316. this.other_user_id = user_id
  317. return cb(e)
  318. }
  319. )
  320. },
  321. cb => {
  322. return FixturesManager.setUpProject(
  323. {
  324. user_id: this.other_user_id,
  325. privilegeLevel: 'owner',
  326. project: {
  327. name: 'Test Project',
  328. },
  329. },
  330. (e, { project_id, user_id }) => {
  331. this.project_id = project_id
  332. this.user_id = user_id
  333. return cb(e)
  334. }
  335. )
  336. },
  337. cb => {
  338. this.client = RealTimeClient.connect()
  339. return this.client.on('connectionAccepted', cb)
  340. },
  341. cb => {
  342. return this.client.emit(
  343. 'joinProject',
  344. { project_id: this.project_id },
  345. (error, project, privilegeLevel, protocolVersion) => {
  346. this.project = project
  347. this.privilegeLevel = privilegeLevel
  348. this.protocolVersion = protocolVersion
  349. return cb(error)
  350. }
  351. )
  352. },
  353. cb => {
  354. return this.client.emit(
  355. 'joinProject',
  356. { project_id: this.other_project_id },
  357. error => {
  358. this.error = error
  359. return cb()
  360. }
  361. )
  362. },
  363. ],
  364. done
  365. )
  366. })
  367. return it('should return an error', function () {
  368. this.error.message.should.equal('cannot join multiple projects')
  369. })
  370. })
  371. return describe('when over rate limit', function () {
  372. before(function (done) {
  373. return async.series(
  374. [
  375. cb => {
  376. this.client = RealTimeClient.connect()
  377. return this.client.on('connectionAccepted', cb)
  378. },
  379. cb => {
  380. return this.client.emit(
  381. 'joinProject',
  382. { project_id: '429429429429429429429429' }, // rate-limited
  383. error => {
  384. this.error = error
  385. return cb()
  386. }
  387. )
  388. },
  389. ],
  390. done
  391. )
  392. })
  393. return it('should return a TooManyRequests error code', function () {
  394. this.error.message.should.equal('rate-limit hit when joining project')
  395. return this.error.code.should.equal('TooManyRequests')
  396. })
  397. })
  398. })