Implement precarious token persistence mechanism

This commit is contained in:
Juan Cruz Viotti 2014-10-31 15:19:16 -04:00
parent 463ed6473b
commit 41d73eaf98
5 changed files with 86 additions and 1 deletions

View File

@ -2,7 +2,7 @@ expect = require('chai').expect
nock = require('nock')
auth = require('./auth')
config = require('../../config')
johnDoeFixture = require('./fixtures/johndoe')
johnDoeFixture = require('../../../tests/fixtures/johndoe')
describe 'Auth', ->

15
lib/token/token.coffee Normal file
View File

@ -0,0 +1,15 @@
# TODO: Persist token in a secure manner
token = null
exports.saveToken = (newToken) ->
token = newToken
exports.hasToken = ->
return token?
exports.getToken = ->
return token or undefined
exports.clearToken = ->
token = null

View File

@ -0,0 +1,63 @@
expect = require('chai').expect
token = require('./token')
johnDoeFixture = require('../../tests/fixtures/johndoe.json')
janeDoeFixture = require('../../tests/fixtures/janedoe.json')
describe 'Token', ->
describe 'given a user that is logged in', ->
beforeEach ->
token.saveToken(johnDoeFixture.token)
describe '#saveToken()', ->
it 'should overwrite the old token', ->
expect(token.getToken()).to.equal(johnDoeFixture.token)
token.saveToken(janeDoeFixture.token)
expect(token.getToken()).to.not.equal(johnDoeFixture.token)
expect(token.getToken()).to.equal(janeDoeFixture.token)
describe '#hasToken()', ->
it 'should return true', ->
expect(token.hasToken()).to.be.true
describe '#getToken()', ->
it 'should return the token', ->
expect(token.getToken()).to.equal(johnDoeFixture.token)
describe '#clearToken()', ->
it 'should effectively clear the token', ->
expect(token.getToken()).to.equal(johnDoeFixture.token)
token.clearToken()
expect(token.getToken()).to.be.undefined
describe 'given a user that didn\'t log in', ->
beforeEach ->
token.clearToken()
describe '#saveToken()', ->
it 'should save a token', ->
token.saveToken(johnDoeFixture.token)
expect(token.getToken()).to.equal(johnDoeFixture.token)
describe '#hasToken()', ->
it 'should return false', ->
expect(token.hasToken()).to.be.false
describe '#getToken()', ->
it 'should return undefined', ->
expect(token.getToken()).to.be.undefined
describe '#clearToken()', ->
it 'should not throw an error', ->
expect(token.clearToken).to.not.throw(Error)

7
tests/fixtures/janedoe.json vendored Normal file
View File

@ -0,0 +1,7 @@
{
"token": "eyJ0eXAiOiJHS1QiLCJhbGciOiJajkajsiausa98938493jkjacI6Imp2aW90dGkiLCJlbWFpbCI6Imp1YW5jaGl2aW998a98dcISJDKSAVhd897d89asdiasd98a98dcISJDKSAVhd897d89asdiasd98a98dcISJDKSAVhd897d89asdiasd98a98dcISJDKSAVhd897d89asdiasd98a98dcISJDKSAVhd897d89asdiasd98a98dcISJDKSAVhd897d89asdiasd98a98dcISJDKSAVhd897d89asdiasd21lIiwiYWRtaW4ubG9naW5fYXNfdXNlciJdLCJpYXQiOjE0MTQ3NzMyMTN9.j4gMqZlnVohJt8jLpzpJWZS6rFUpNCWDwa13woo58aY",
"credentials": {
"username": "janedoe",
"password": "mysecret"
}
}