JoinProjectTests.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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: '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: '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. return describe('when over rate limit', function () {
  273. before(function (done) {
  274. return async.series(
  275. [
  276. (cb) => {
  277. this.client = RealTimeClient.connect()
  278. return this.client.on('connectionAccepted', cb)
  279. },
  280. (cb) => {
  281. return this.client.emit(
  282. 'joinProject',
  283. { project_id: 'rate-limited' },
  284. (error) => {
  285. this.error = error
  286. return cb()
  287. }
  288. )
  289. }
  290. ],
  291. done
  292. )
  293. })
  294. return it('should return a TooManyRequests error code', function () {
  295. this.error.message.should.equal('rate-limit hit when joining project')
  296. return this.error.code.should.equal('TooManyRequests')
  297. })
  298. })
  299. })