JoinProjectTests.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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('not authorized')
  205. })
  206. it('should not have joined the project room', function (done) {
  207. RealTimeClient.getConnectedClient(
  208. this.client.socket.sessionid,
  209. (error, client) => {
  210. expect(Array.from(client.rooms).includes(this.project_id)).to.equal(
  211. false
  212. )
  213. done()
  214. }
  215. )
  216. })
  217. })
  218. describe('when deleted and web replies with a 404', function () {
  219. before(function (done) {
  220. return async.series(
  221. [
  222. (cb) => {
  223. return FixturesManager.setUpProject(
  224. {
  225. project_id: 'not-found',
  226. privilegeLevel: 'owner',
  227. project: {
  228. name: 'Test Project'
  229. }
  230. },
  231. (e, { project_id, user_id }) => {
  232. this.project_id = project_id
  233. this.user_id = user_id
  234. cb(e)
  235. }
  236. )
  237. },
  238. (cb) => {
  239. this.client = RealTimeClient.connect()
  240. this.client.on('connectionAccepted', cb)
  241. },
  242. (cb) => {
  243. this.client.emit(
  244. 'joinProject',
  245. { project_id: this.project_id },
  246. (error, project, privilegeLevel, protocolVersion) => {
  247. this.error = error
  248. this.project = project
  249. this.privilegeLevel = privilegeLevel
  250. this.protocolVersion = protocolVersion
  251. cb()
  252. }
  253. )
  254. }
  255. ],
  256. done
  257. )
  258. })
  259. it('should return an error', function () {
  260. this.error.code.should.equal('ProjectNotFound')
  261. })
  262. it('should not have joined the project room', function (done) {
  263. RealTimeClient.getConnectedClient(
  264. this.client.socket.sessionid,
  265. (error, client) => {
  266. expect(Array.from(client.rooms).includes(this.project_id)).to.equal(
  267. false
  268. )
  269. done()
  270. }
  271. )
  272. })
  273. })
  274. return describe('when over rate limit', function () {
  275. before(function (done) {
  276. return async.series(
  277. [
  278. (cb) => {
  279. this.client = RealTimeClient.connect()
  280. return this.client.on('connectionAccepted', cb)
  281. },
  282. (cb) => {
  283. return this.client.emit(
  284. 'joinProject',
  285. { project_id: 'rate-limited' },
  286. (error) => {
  287. this.error = error
  288. return cb()
  289. }
  290. )
  291. }
  292. ],
  293. done
  294. )
  295. })
  296. return it('should return a TooManyRequests error code', function () {
  297. this.error.message.should.equal('rate-limit hit when joining project')
  298. return this.error.code.should.equal('TooManyRequests')
  299. })
  300. })
  301. })