| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484 |
- sinon = require('sinon')
- chai = require('chai')
- should = chai.should()
- expect = chai.expect
- modulePath = "../../../../app/js/Features/User/UserSessionsManager.js"
- SandboxedModule = require('sandboxed-module')
- describe 'UserSessionsManager', ->
- beforeEach ->
- @user =
- _id: "abcd"
- email: "user@example.com"
- @sessionId = 'some_session_id'
- @rclient =
- multi: sinon.stub()
- exec: sinon.stub()
- get: sinon.stub()
- del: sinon.stub()
- sadd: sinon.stub()
- srem: sinon.stub()
- smembers: sinon.stub()
- expire: sinon.stub()
- @rclient.multi.returns(@rclient)
- @rclient.get.returns(@rclient)
- @rclient.del.returns(@rclient)
- @rclient.sadd.returns(@rclient)
- @rclient.srem.returns(@rclient)
- @rclient.smembers.returns(@rclient)
- @rclient.expire.returns(@rclient)
- @rclient.exec.callsArgWith(0, null)
- @redis =
- createClient: () => @rclient
- @logger =
- err: sinon.stub()
- error: sinon.stub()
- log: sinon.stub()
- @settings =
- redis:
- web: {}
- @UserSessionsManager = SandboxedModule.require modulePath, requires:
- "redis-sharelatex": @redis
- "logger-sharelatex": @logger
- "settings-sharelatex": @settings
- describe '_sessionSetKey', ->
- it 'should build the correct key', ->
- result = @UserSessionsManager._sessionSetKey(@user)
- result.should.equal 'UserSessions:abcd'
- describe '_sessionKey', ->
- it 'should build the correct key', ->
- result = @UserSessionsManager._sessionKey(@sessionId)
- result.should.equal 'sess:some_session_id'
- describe 'trackSession', ->
- beforeEach ->
- @call = (callback) =>
- @UserSessionsManager.trackSession @user, @sessionId, callback
- @rclient.exec.callsArgWith(0, null)
- @_checkSessions = sinon.stub(@UserSessionsManager, '_checkSessions').returns(null)
- afterEach ->
- @_checkSessions.restore()
- it 'should not produce an error', (done) ->
- @call (err) =>
- expect(err).to.not.be.instanceof Error
- done()
- it 'should call the appropriate redis methods', (done) ->
- @call (err) =>
- @rclient.multi.callCount.should.equal 1
- @rclient.sadd.callCount.should.equal 1
- @rclient.expire.callCount.should.equal 1
- @rclient.exec.callCount.should.equal 1
- done()
- it 'should call _checkSessions', (done) ->
- @call (err) =>
- @_checkSessions.callCount.should.equal 1
- done()
- describe 'when rclient produces an error', ->
- beforeEach ->
- @rclient.exec.callsArgWith(0, new Error('woops'))
- it 'should produce an error', (done) ->
- @call (err) =>
- expect(err).to.be.instanceof Error
- done()
- it 'should not call _checkSessions', (done) ->
- @call (err) =>
- @_checkSessions.callCount.should.equal 0
- done()
- describe 'when no user is supplied', ->
- beforeEach ->
- @call = (callback) =>
- @UserSessionsManager.trackSession null, @sessionId, callback
- it 'should not produce an error', (done) ->
- @call (err) =>
- expect(err).to.not.be.instanceof Error
- expect(err).to.equal null
- done()
- it 'should not call the appropriate redis methods', (done) ->
- @call (err) =>
- @rclient.multi.callCount.should.equal 0
- @rclient.sadd.callCount.should.equal 0
- @rclient.expire.callCount.should.equal 0
- @rclient.exec.callCount.should.equal 0
- done()
- it 'should not call _checkSessions', (done) ->
- @call (err) =>
- @_checkSessions.callCount.should.equal 0
- done()
- describe 'when no sessionId is supplied', ->
- beforeEach ->
- @call = (callback) =>
- @UserSessionsManager.trackSession @user, null, callback
- it 'should not produce an error', (done) ->
- @call (err) =>
- expect(err).to.not.be.instanceof Error
- expect(err).to.equal null
- done()
- it 'should not call the appropriate redis methods', (done) ->
- @call (err) =>
- @rclient.multi.callCount.should.equal 0
- @rclient.sadd.callCount.should.equal 0
- @rclient.expire.callCount.should.equal 0
- @rclient.exec.callCount.should.equal 0
- done()
- it 'should not call _checkSessions', (done) ->
- @call (err) =>
- @_checkSessions.callCount.should.equal 0
- done()
- describe 'untrackSession', ->
- beforeEach ->
- @call = (callback) =>
- @UserSessionsManager.untrackSession @user, @sessionId, callback
- @rclient.exec.callsArgWith(0, null)
- @_checkSessions = sinon.stub(@UserSessionsManager, '_checkSessions').returns(null)
- afterEach ->
- @_checkSessions.restore()
- it 'should not produce an error', (done) ->
- @call (err) =>
- expect(err).to.not.be.instanceof Error
- expect(err).to.equal undefined
- done()
- it 'should call the appropriate redis methods', (done) ->
- @call (err) =>
- @rclient.multi.callCount.should.equal 1
- @rclient.srem.callCount.should.equal 1
- @rclient.expire.callCount.should.equal 1
- @rclient.exec.callCount.should.equal 1
- done()
- it 'should call _checkSessions', (done) ->
- @call (err) =>
- @_checkSessions.callCount.should.equal 1
- done()
- describe 'when rclient produces an error', ->
- beforeEach ->
- @rclient.exec.callsArgWith(0, new Error('woops'))
- it 'should produce an error', (done) ->
- @call (err) =>
- expect(err).to.be.instanceof Error
- done()
- it 'should not call _checkSessions', (done) ->
- @call (err) =>
- @_checkSessions.callCount.should.equal 0
- done()
- describe 'when no user is supplied', ->
- beforeEach ->
- @call = (callback) =>
- @UserSessionsManager.untrackSession null, @sessionId, callback
- it 'should not produce an error', (done) ->
- @call (err) =>
- expect(err).to.not.be.instanceof Error
- expect(err).to.equal null
- done()
- it 'should not call the appropriate redis methods', (done) ->
- @call (err) =>
- @rclient.multi.callCount.should.equal 0
- @rclient.srem.callCount.should.equal 0
- @rclient.expire.callCount.should.equal 0
- @rclient.exec.callCount.should.equal 0
- done()
- it 'should not call _checkSessions', (done) ->
- @call (err) =>
- @_checkSessions.callCount.should.equal 0
- done()
- describe 'when no sessionId is supplied', ->
- beforeEach ->
- @call = (callback) =>
- @UserSessionsManager.untrackSession @user, null, callback
- it 'should not produce an error', (done) ->
- @call (err) =>
- expect(err).to.not.be.instanceof Error
- expect(err).to.equal null
- done()
- it 'should not call the appropriate redis methods', (done) ->
- @call (err) =>
- @rclient.multi.callCount.should.equal 0
- @rclient.srem.callCount.should.equal 0
- @rclient.expire.callCount.should.equal 0
- @rclient.exec.callCount.should.equal 0
- done()
- it 'should not call _checkSessions', (done) ->
- @call (err) =>
- @_checkSessions.callCount.should.equal 0
- done()
- ##
- describe 'revokeAllUserSessions', ->
- beforeEach ->
- @sessionKeys = ['sess:one', 'sess:two']
- @retain = []
- @rclient.smembers.callsArgWith(1, null, @sessionKeys)
- @rclient.exec.callsArgWith(0, null)
- @call = (callback) =>
- @UserSessionsManager.revokeAllUserSessions @user, @retain, callback
- it 'should not produce an error', (done) ->
- @call (err) =>
- expect(err).to.not.be.instanceof Error
- expect(err).to.equal null
- done()
- it 'should call the appropriate redis methods', (done) ->
- @call (err) =>
- @rclient.smembers.callCount.should.equal 1
- @rclient.multi.callCount.should.equal 1
- @rclient.del.callCount.should.equal 1
- expect(@rclient.del.firstCall.args[0]).to.deep.equal @sessionKeys
- @rclient.srem.callCount.should.equal 1
- expect(@rclient.srem.firstCall.args[1]).to.deep.equal @sessionKeys
- @rclient.exec.callCount.should.equal 1
- done()
- describe 'when a session is retained', ->
- beforeEach ->
- @sessionKeys = ['sess:one', 'sess:two', 'sess:three', 'sess:four']
- @retain = ['two']
- @rclient.smembers.callsArgWith(1, null, @sessionKeys)
- @rclient.exec.callsArgWith(0, null)
- @call = (callback) =>
- @UserSessionsManager.revokeAllUserSessions @user, @retain, callback
- it 'should not produce an error', (done) ->
- @call (err) =>
- expect(err).to.not.be.instanceof Error
- expect(err).to.equal null
- done()
- it 'should call the appropriate redis methods', (done) ->
- @call (err) =>
- @rclient.smembers.callCount.should.equal 1
- @rclient.multi.callCount.should.equal 1
- @rclient.del.callCount.should.equal 1
- @rclient.srem.callCount.should.equal 1
- @rclient.exec.callCount.should.equal 1
- done()
- it 'should remove all sessions except for the retained one', (done) ->
- @call (err) =>
- expect(@rclient.del.firstCall.args[0]).to.deep.equal(['sess:one', 'sess:three', 'sess:four'])
- expect(@rclient.srem.firstCall.args[1]).to.deep.equal(['sess:one', 'sess:three', 'sess:four'])
- done()
- describe 'when rclient produces an error', ->
- beforeEach ->
- @rclient.exec.callsArgWith(0, new Error('woops'))
- it 'should produce an error', (done) ->
- @call (err) =>
- expect(err).to.be.instanceof Error
- done()
- describe 'when no user is supplied', ->
- beforeEach ->
- @call = (callback) =>
- @UserSessionsManager.revokeAllUserSessions null, @retain, callback
- it 'should not produce an error', (done) ->
- @call (err) =>
- expect(err).to.not.be.instanceof Error
- expect(err).to.equal null
- done()
- it 'should not call the appropriate redis methods', (done) ->
- @call (err) =>
- @rclient.smembers.callCount.should.equal 0
- @rclient.multi.callCount.should.equal 0
- @rclient.del.callCount.should.equal 0
- @rclient.srem.callCount.should.equal 0
- @rclient.exec.callCount.should.equal 0
- done()
- describe 'when there are no keys to delete', ->
- beforeEach ->
- @rclient.smembers.callsArgWith(1, null, [])
- it 'should not produce an error', (done) ->
- @call (err) =>
- expect(err).to.not.be.instanceof Error
- expect(err).to.equal null
- done()
- it 'should not do the delete operation', (done) ->
- @call (err) =>
- @rclient.smembers.callCount.should.equal 1
- @rclient.multi.callCount.should.equal 0
- @rclient.del.callCount.should.equal 0
- @rclient.srem.callCount.should.equal 0
- @rclient.exec.callCount.should.equal 0
- done()
- describe 'touch', ->
- beforeEach ->
- @rclient.expire.callsArgWith(2, null)
- @call = (callback) =>
- @UserSessionsManager.touch @user, callback
- it 'should not produce an error', (done) ->
- @call (err) =>
- expect(err).to.not.be.instanceof Error
- expect(err).to.equal null
- done()
- it 'should call rclient.expire', (done) ->
- @call (err) =>
- @rclient.expire.callCount.should.equal 1
- done()
- describe 'when rclient produces an error', ->
- beforeEach ->
- @rclient.expire.callsArgWith(2, new Error('woops'))
- it 'should produce an error', (done) ->
- @call (err) =>
- expect(err).to.be.instanceof Error
- done()
- describe 'when no user is supplied', ->
- beforeEach ->
- @call = (callback) =>
- @UserSessionsManager.touch null, callback
- it 'should not produce an error', (done) ->
- @call (err) =>
- expect(err).to.not.be.instanceof Error
- expect(err).to.equal null
- done()
- it 'should not call expire', (done) ->
- @call (err) =>
- @rclient.expire.callCount.should.equal 0
- done()
- describe '_checkSessions', ->
- beforeEach ->
- @call = (callback) =>
- @UserSessionsManager._checkSessions @user, callback
- @sessionKeys = ['one', 'two']
- @rclient.smembers.callsArgWith(1, null, @sessionKeys)
- @rclient.get.callsArgWith(1, null, 'some-value')
- @rclient.srem.callsArgWith(2, null, {})
- it 'should not produce an error', (done) ->
- @call (err) =>
- expect(err).to.not.be.instanceof Error
- expect(err).to.equal undefined
- done()
- it 'should call the appropriate redis methods', (done) ->
- @call (err) =>
- @rclient.smembers.callCount.should.equal 1
- @rclient.get.callCount.should.equal 2
- @rclient.srem.callCount.should.equal 0
- done()
- describe 'when one of the keys is not present in redis', ->
- beforeEach ->
- @rclient.get.onCall(0).callsArgWith(1, null, 'some-val')
- @rclient.get.onCall(1).callsArgWith(1, null, null)
- it 'should not produce an error', (done) ->
- @call (err) =>
- expect(err).to.not.be.instanceof Error
- expect(err).to.equal undefined
- done()
- it 'should remove that key from the set', (done) ->
- @call (err) =>
- @rclient.smembers.callCount.should.equal 1
- @rclient.get.callCount.should.equal 2
- @rclient.srem.callCount.should.equal 1
- @rclient.srem.firstCall.args[1].should.equal 'two'
- done()
- describe 'when no user is supplied', ->
- beforeEach ->
- @call = (callback) =>
- @UserSessionsManager._checkSessions null, callback
- it 'should not produce an error', (done) ->
- @call (err) =>
- expect(err).to.not.be.instanceof Error
- expect(err).to.equal null
- done()
- it 'should not call redis methods', (done) ->
- @call (err) =>
- @rclient.smembers.callCount.should.equal 0
- @rclient.get.callCount.should.equal 0
- done()
- describe 'when one of the get operations produces an error', ->
- beforeEach ->
- @rclient.get.onCall(0).callsArgWith(1, new Error('woops'), null)
- @rclient.get.onCall(1).callsArgWith(1, null, null)
- it 'should produce an error', (done) ->
- @call (err) =>
- expect(err).to.be.instanceof Error
- done()
- it 'should call the right redis methods, bailing out early', (done) ->
- @call (err) =>
- @rclient.smembers.callCount.should.equal 1
- @rclient.get.callCount.should.equal 1
- @rclient.srem.callCount.should.equal 0
- done()
|