JoinProjectTests.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. return describe('when over rate limit', function () {
  163. before(function (done) {
  164. return async.series(
  165. [
  166. (cb) => {
  167. this.client = RealTimeClient.connect()
  168. return this.client.on('connectionAccepted', cb)
  169. },
  170. (cb) => {
  171. return this.client.emit(
  172. 'joinProject',
  173. { project_id: 'rate-limited' },
  174. (error) => {
  175. this.error = error
  176. return cb()
  177. }
  178. )
  179. }
  180. ],
  181. done
  182. )
  183. })
  184. return it('should return a TooManyRequests error code', function () {
  185. this.error.message.should.equal('rate-limit hit when joining project')
  186. return this.error.code.should.equal('TooManyRequests')
  187. })
  188. })
  189. })