test the _checkSessions function.

This commit is contained in:
Shane Kilkelly 2016-07-04 14:04:10 +01:00
parent 699ba21032
commit df0a7bc547
2 changed files with 127 additions and 0 deletions

View file

@ -86,6 +86,7 @@ module.exports = UserSessionsManager =
if err
logger.err {err, user_id: user._id}, "error while updating ttl on UserSessions set"
return callback(err)
callback(null)
_checkSessions: (user, callback=(err)->) ->
if !user

View file

@ -165,6 +165,7 @@ describe 'UserSessionsManager', ->
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) ->
@ -258,6 +259,7 @@ describe 'UserSessionsManager', ->
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) ->
@ -301,3 +303,127 @@ describe 'UserSessionsManager', ->
@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()