Convert Token module into callback based

This commit is contained in:
Juan Cruz Viotti 2014-11-07 14:51:54 -04:00
parent 3f5da39990
commit 5d6d2f0511
2 changed files with 88 additions and 35 deletions

View File

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

View File

@ -1,4 +1,5 @@
expect = require('chai').expect expect = require('chai').expect
async = require('async')
token = require('./token') token = require('./token')
johnDoeFixture = require('../../tests/fixtures/johndoe.json') johnDoeFixture = require('../../tests/fixtures/johndoe.json')
@ -8,56 +9,105 @@ describe 'Token:', ->
describe 'given a user that is logged in', -> describe 'given a user that is logged in', ->
beforeEach -> beforeEach (done) ->
token.saveToken(johnDoeFixture.token) token.saveToken(johnDoeFixture.token, done)
describe '#saveToken()', -> describe '#saveToken()', ->
it 'should overwrite the old token', -> it 'should overwrite the old token', (done) ->
expect(token.getToken()).to.equal(johnDoeFixture.token) async.waterfall [
token.saveToken(janeDoeFixture.token)
expect(token.getToken()).to.not.equal(johnDoeFixture.token) (callback) ->
expect(token.getToken()).to.equal(janeDoeFixture.token) token.getToken(callback)
(savedToken, callback) ->
expect(savedToken).to.equal(johnDoeFixture.token)
token.saveToken(janeDoeFixture.token, callback)
(savedToken, callback) ->
token.getToken(callback)
(savedToken, callback) ->
expect(savedToken).to.equal(janeDoeFixture.token)
return callback()
], (error) ->
expect(error).to.not.exist
done()
describe '#hasToken()', -> describe '#hasToken()', ->
it 'should return true', -> it 'should return true', (done) ->
expect(token.hasToken()).to.be.true token.hasToken (hasToken) ->
expect(hasToken).to.be.true
done()
describe '#getToken()', -> describe '#getToken()', ->
it 'should return the token', -> it 'should return the token', (done) ->
expect(token.getToken()).to.equal(johnDoeFixture.token) token.getToken (error, savedToken) ->
expect(error).to.not.exist
expect(savedToken).to.equal(johnDoeFixture.token)
done()
describe '#clearToken()', -> describe '#clearToken()', ->
it 'should effectively clear the token', -> it 'should effectively clear the token', (done) ->
expect(token.getToken()).to.equal(johnDoeFixture.token) async.waterfall [
token.clearToken()
expect(token.getToken()).to.be.undefined (callback) ->
token.getToken(callback)
(savedToken, callback) ->
expect(savedToken).to.equal(johnDoeFixture.token)
token.clearToken(callback)
(callback) ->
token.getToken(callback)
(savedToken, callback) ->
expect(savedToken).to.be.undefined
return callback()
], (error) ->
expect(error).to.not.exist
done()
describe 'given a user that didn\'t log in', -> describe 'given a user that didn\'t log in', ->
beforeEach -> beforeEach (done) ->
token.clearToken() token.clearToken(done)
describe '#saveToken()', -> describe '#saveToken()', ->
it 'should save a token', -> it 'should save a token', (done) ->
token.saveToken(johnDoeFixture.token) async.waterfall [
expect(token.getToken()).to.equal(johnDoeFixture.token)
(callback) ->
token.saveToken(johnDoeFixture.token, callback)
(savedToken, callback) ->
token.getToken(callback)
(savedToken, callback) ->
expect(savedToken).to.equal(johnDoeFixture.token)
return callback()
], (error) ->
expect(error).to.not.exist
done()
describe '#hasToken()', -> describe '#hasToken()', ->
it 'should return false', -> it 'should return false', (done) ->
expect(token.hasToken()).to.be.false token.hasToken (hasToken) ->
expect(hasToken).to.be.false
done()
describe '#getToken()', -> describe '#getToken()', ->
it 'should return undefined', -> it 'should return undefined', (done) ->
expect(token.getToken()).to.be.undefined token.getToken (error, savedToken) ->
expect(error).to.not.exist
describe '#clearToken()', -> expect(savedToken).to.be.undefined
done()
it 'should not throw an error', ->
expect(token.clearToken).to.not.throw(Error)