| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- /* eslint-disable
- camelcase,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS101: Remove unnecessary use of Array.from
- * DS102: Remove unnecessary code created because of implicit returns
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const { expect } = require('chai')
- const RealTimeClient = require('./helpers/RealTimeClient')
- const MockWebServer = require('./helpers/MockWebServer')
- const FixturesManager = require('./helpers/FixturesManager')
- const async = require('async')
- describe('joinProject', function () {
- describe('when authorized', function () {
- before(function (done) {
- return async.series(
- [
- cb => {
- return FixturesManager.setUpProject(
- {
- privilegeLevel: 'owner',
- project: {
- name: 'Test Project',
- },
- },
- (e, { project_id, user_id }) => {
- this.project_id = project_id
- this.user_id = user_id
- return cb(e)
- }
- )
- },
- cb => {
- this.client = RealTimeClient.connect()
- return this.client.on('connectionAccepted', cb)
- },
- cb => {
- return this.client.emit(
- 'joinProject',
- { project_id: this.project_id },
- (error, project, privilegeLevel, protocolVersion) => {
- this.project = project
- this.privilegeLevel = privilegeLevel
- this.protocolVersion = protocolVersion
- return cb(error)
- }
- )
- },
- ],
- done
- )
- })
- it('should get the project from web', function () {
- return MockWebServer.joinProject
- .calledWith(this.project_id, this.user_id)
- .should.equal(true)
- })
- it('should return the project', function () {
- return this.project.should.deep.equal({
- name: 'Test Project',
- })
- })
- it('should return the privilege level', function () {
- return this.privilegeLevel.should.equal('owner')
- })
- it('should return the protocolVersion', function () {
- return this.protocolVersion.should.equal(2)
- })
- it('should have joined the project room', function (done) {
- return RealTimeClient.getConnectedClient(
- this.client.socket.sessionid,
- (error, client) => {
- if (error) return done(error)
- expect(Array.from(client.rooms).includes(this.project_id)).to.equal(
- true
- )
- return done()
- }
- )
- })
- return it('should have marked the user as connected', function (done) {
- return this.client.emit(
- 'clientTracking.getConnectedUsers',
- (error, users) => {
- if (error) return done(error)
- let connected = false
- for (const user of Array.from(users)) {
- if (
- user.client_id === this.client.publicId &&
- user.user_id === this.user_id
- ) {
- connected = true
- break
- }
- }
- expect(connected).to.equal(true)
- return done()
- }
- )
- })
- })
- describe('when not authorized', function () {
- before(function (done) {
- return async.series(
- [
- cb => {
- return FixturesManager.setUpProject(
- {
- privilegeLevel: null,
- project: {
- name: 'Test Project',
- },
- },
- (e, { project_id, user_id }) => {
- this.project_id = project_id
- this.user_id = user_id
- return cb(e)
- }
- )
- },
- cb => {
- this.client = RealTimeClient.connect()
- return this.client.on('connectionAccepted', cb)
- },
- cb => {
- return this.client.emit(
- 'joinProject',
- { project_id: this.project_id },
- (error, project, privilegeLevel, protocolVersion) => {
- this.error = error
- this.project = project
- this.privilegeLevel = privilegeLevel
- this.protocolVersion = protocolVersion
- return cb()
- }
- )
- },
- ],
- done
- )
- })
- it('should return an error', function () {
- return this.error.message.should.equal('not authorized')
- })
- return it('should not have joined the project room', function (done) {
- return RealTimeClient.getConnectedClient(
- this.client.socket.sessionid,
- (error, client) => {
- if (error) return done(error)
- expect(Array.from(client.rooms).includes(this.project_id)).to.equal(
- false
- )
- return done()
- }
- )
- })
- })
- describe('when not authorized and web replies with a 403', function () {
- before(function (done) {
- return async.series(
- [
- cb => {
- return FixturesManager.setUpProject(
- {
- project_id: '403403403403403403403403', // forbidden
- privilegeLevel: 'owner',
- project: {
- name: 'Test Project',
- },
- },
- (e, { project_id, user_id }) => {
- this.project_id = project_id
- this.user_id = user_id
- cb(e)
- }
- )
- },
- cb => {
- this.client = RealTimeClient.connect()
- this.client.on('connectionAccepted', cb)
- },
- cb => {
- this.client.emit(
- 'joinProject',
- { project_id: this.project_id },
- (error, project, privilegeLevel, protocolVersion) => {
- this.error = error
- this.project = project
- this.privilegeLevel = privilegeLevel
- this.protocolVersion = protocolVersion
- cb()
- }
- )
- },
- ],
- done
- )
- })
- it('should return an error', function () {
- this.error.message.should.equal('not authorized')
- })
- it('should not have joined the project room', function (done) {
- RealTimeClient.getConnectedClient(
- this.client.socket.sessionid,
- (error, client) => {
- if (error) return done(error)
- expect(Array.from(client.rooms).includes(this.project_id)).to.equal(
- false
- )
- done()
- }
- )
- })
- })
- describe('when deleted and web replies with a 404', function () {
- before(function (done) {
- return async.series(
- [
- cb => {
- return FixturesManager.setUpProject(
- {
- project_id: '404404404404404404404404', // not-found
- privilegeLevel: 'owner',
- project: {
- name: 'Test Project',
- },
- },
- (e, { project_id, user_id }) => {
- this.project_id = project_id
- this.user_id = user_id
- cb(e)
- }
- )
- },
- cb => {
- this.client = RealTimeClient.connect()
- this.client.on('connectionAccepted', cb)
- },
- cb => {
- this.client.emit(
- 'joinProject',
- { project_id: this.project_id },
- (error, project, privilegeLevel, protocolVersion) => {
- this.error = error
- this.project = project
- this.privilegeLevel = privilegeLevel
- this.protocolVersion = protocolVersion
- cb()
- }
- )
- },
- ],
- done
- )
- })
- it('should return an error', function () {
- this.error.code.should.equal('ProjectNotFound')
- })
- it('should not have joined the project room', function (done) {
- RealTimeClient.getConnectedClient(
- this.client.socket.sessionid,
- (error, client) => {
- if (error) return done(error)
- expect(Array.from(client.rooms).includes(this.project_id)).to.equal(
- false
- )
- done()
- }
- )
- })
- })
- describe('when invalid', function () {
- before(function (done) {
- MockWebServer.joinProject.resetHistory()
- return async.series(
- [
- cb => {
- this.client = RealTimeClient.connect()
- return this.client.on('connectionAccepted', cb)
- },
- cb => {
- return this.client.emit(
- 'joinProject',
- { project_id: 'invalid-id' },
- error => {
- this.error = error
- return cb()
- }
- )
- },
- ],
- done
- )
- })
- it('should return an invalid id error', function () {
- this.error.message.should.equal('invalid id')
- })
- it('should not call to web', function () {
- MockWebServer.joinProject.called.should.equal(false)
- })
- })
- describe('when joining more than one project', function () {
- before(function (done) {
- return async.series(
- [
- cb => {
- return FixturesManager.setUpProject(
- {
- privilegeLevel: 'owner',
- project: {
- name: 'Other Project',
- },
- },
- (e, { project_id, user_id }) => {
- this.other_project_id = project_id
- this.other_user_id = user_id
- return cb(e)
- }
- )
- },
- cb => {
- return FixturesManager.setUpProject(
- {
- user_id: this.other_user_id,
- privilegeLevel: 'owner',
- project: {
- name: 'Test Project',
- },
- },
- (e, { project_id, user_id }) => {
- this.project_id = project_id
- this.user_id = user_id
- return cb(e)
- }
- )
- },
- cb => {
- this.client = RealTimeClient.connect()
- return this.client.on('connectionAccepted', cb)
- },
- cb => {
- return this.client.emit(
- 'joinProject',
- { project_id: this.project_id },
- (error, project, privilegeLevel, protocolVersion) => {
- this.project = project
- this.privilegeLevel = privilegeLevel
- this.protocolVersion = protocolVersion
- return cb(error)
- }
- )
- },
- cb => {
- return this.client.emit(
- 'joinProject',
- { project_id: this.other_project_id },
- error => {
- this.error = error
- return cb()
- }
- )
- },
- ],
- done
- )
- })
- return it('should return an error', function () {
- this.error.message.should.equal('cannot join multiple projects')
- })
- })
- return describe('when over rate limit', function () {
- before(function (done) {
- return async.series(
- [
- cb => {
- this.client = RealTimeClient.connect()
- return this.client.on('connectionAccepted', cb)
- },
- cb => {
- return this.client.emit(
- 'joinProject',
- { project_id: '429429429429429429429429' }, // rate-limited
- error => {
- this.error = error
- return cb()
- }
- )
- },
- ],
- done
- )
- })
- return it('should return a TooManyRequests error code', function () {
- this.error.message.should.equal('rate-limit hit when joining project')
- return this.error.code.should.equal('TooManyRequests')
- })
- })
- })
|