JoinProjectTests.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 chai = require('chai')
  14. const { expect } = chai
  15. chai.should()
  16. const RealTimeClient = require('./helpers/RealTimeClient')
  17. const MockWebServer = require('./helpers/MockWebServer')
  18. const FixturesManager = require('./helpers/FixturesManager')
  19. const async = require('async')
  20. describe('joinProject', function () {
  21. describe('when authorized', function () {
  22. before(function (done) {
  23. return async.series(
  24. [
  25. (cb) => {
  26. return FixturesManager.setUpProject(
  27. {
  28. privilegeLevel: 'owner',
  29. project: {
  30. name: 'Test Project'
  31. }
  32. },
  33. (e, { project_id, user_id }) => {
  34. this.project_id = project_id
  35. this.user_id = user_id
  36. return cb(e)
  37. }
  38. )
  39. },
  40. (cb) => {
  41. this.client = RealTimeClient.connect()
  42. return this.client.on('connectionAccepted', cb)
  43. },
  44. (cb) => {
  45. return this.client.emit(
  46. 'joinProject',
  47. { project_id: this.project_id },
  48. (error, project, privilegeLevel, protocolVersion) => {
  49. this.project = project
  50. this.privilegeLevel = privilegeLevel
  51. this.protocolVersion = protocolVersion
  52. return cb(error)
  53. }
  54. )
  55. }
  56. ],
  57. done
  58. )
  59. })
  60. it('should get the project from web', function () {
  61. return MockWebServer.joinProject
  62. .calledWith(this.project_id, this.user_id)
  63. .should.equal(true)
  64. })
  65. it('should return the project', function () {
  66. return this.project.should.deep.equal({
  67. name: 'Test Project'
  68. })
  69. })
  70. it('should return the privilege level', function () {
  71. return this.privilegeLevel.should.equal('owner')
  72. })
  73. it('should return the protocolVersion', function () {
  74. return this.protocolVersion.should.equal(2)
  75. })
  76. it('should have joined the project room', function (done) {
  77. return RealTimeClient.getConnectedClient(
  78. this.client.socket.sessionid,
  79. (error, client) => {
  80. expect(Array.from(client.rooms).includes(this.project_id)).to.equal(
  81. true
  82. )
  83. return done()
  84. }
  85. )
  86. })
  87. return it('should have marked the user as connected', function (done) {
  88. return this.client.emit(
  89. 'clientTracking.getConnectedUsers',
  90. (error, users) => {
  91. let connected = false
  92. for (const user of Array.from(users)) {
  93. if (
  94. user.client_id === this.client.publicId &&
  95. user.user_id === this.user_id
  96. ) {
  97. connected = true
  98. break
  99. }
  100. }
  101. expect(connected).to.equal(true)
  102. return done()
  103. }
  104. )
  105. })
  106. })
  107. describe('when not authorized', function () {
  108. before(function (done) {
  109. return async.series(
  110. [
  111. (cb) => {
  112. return FixturesManager.setUpProject(
  113. {
  114. privilegeLevel: null,
  115. project: {
  116. name: 'Test Project'
  117. }
  118. },
  119. (e, { project_id, user_id }) => {
  120. this.project_id = project_id
  121. this.user_id = user_id
  122. return cb(e)
  123. }
  124. )
  125. },
  126. (cb) => {
  127. this.client = RealTimeClient.connect()
  128. return this.client.on('connectionAccepted', cb)
  129. },
  130. (cb) => {
  131. return this.client.emit(
  132. 'joinProject',
  133. { project_id: this.project_id },
  134. (error, project, privilegeLevel, protocolVersion) => {
  135. this.error = error
  136. this.project = project
  137. this.privilegeLevel = privilegeLevel
  138. this.protocolVersion = protocolVersion
  139. return cb()
  140. }
  141. )
  142. }
  143. ],
  144. done
  145. )
  146. })
  147. it('should return an error', function () {
  148. return this.error.message.should.equal('not authorized')
  149. })
  150. return it('should not have joined the project room', function (done) {
  151. return RealTimeClient.getConnectedClient(
  152. this.client.socket.sessionid,
  153. (error, client) => {
  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: '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(
  205. 'Something went wrong in real-time service'
  206. )
  207. })
  208. it('should not have joined the project room', function (done) {
  209. RealTimeClient.getConnectedClient(
  210. this.client.socket.sessionid,
  211. (error, client) => {
  212. expect(Array.from(client.rooms).includes(this.project_id)).to.equal(
  213. false
  214. )
  215. done()
  216. }
  217. )
  218. })
  219. })
  220. return describe('when over rate limit', function () {
  221. before(function (done) {
  222. return async.series(
  223. [
  224. (cb) => {
  225. this.client = RealTimeClient.connect()
  226. return this.client.on('connectionAccepted', cb)
  227. },
  228. (cb) => {
  229. return this.client.emit(
  230. 'joinProject',
  231. { project_id: 'rate-limited' },
  232. (error) => {
  233. this.error = error
  234. return cb()
  235. }
  236. )
  237. }
  238. ],
  239. done
  240. )
  241. })
  242. return it('should return a TooManyRequests error code', function () {
  243. this.error.message.should.equal('rate-limit hit when joining project')
  244. return this.error.code.should.equal('TooManyRequests')
  245. })
  246. })
  247. })