mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-18 21:27:51 +00:00
Convert Token module into callback based
This commit is contained in:
parent
3f5da39990
commit
5d6d2f0511
@ -1,15 +1,18 @@
|
||||
# TODO: Persist token in a secure manner
|
||||
data = require('../data/data')
|
||||
|
||||
token = null
|
||||
|
||||
exports.saveToken = (newToken) ->
|
||||
exports.saveToken = (newToken, callback) ->
|
||||
token = newToken
|
||||
return callback(null, token)
|
||||
|
||||
exports.hasToken = ->
|
||||
return token?
|
||||
exports.hasToken = (callback) ->
|
||||
return callback(token?)
|
||||
|
||||
exports.getToken = ->
|
||||
return token or undefined
|
||||
exports.getToken = (callback) ->
|
||||
return callback(null, token or undefined)
|
||||
|
||||
exports.clearToken = ->
|
||||
exports.clearToken = (callback) ->
|
||||
token = null
|
||||
return callback?(null)
|
||||
|
@ -1,4 +1,5 @@
|
||||
expect = require('chai').expect
|
||||
async = require('async')
|
||||
token = require('./token')
|
||||
|
||||
johnDoeFixture = require('../../tests/fixtures/johndoe.json')
|
||||
@ -8,56 +9,105 @@ describe 'Token:', ->
|
||||
|
||||
describe 'given a user that is logged in', ->
|
||||
|
||||
beforeEach ->
|
||||
token.saveToken(johnDoeFixture.token)
|
||||
beforeEach (done) ->
|
||||
token.saveToken(johnDoeFixture.token, done)
|
||||
|
||||
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)
|
||||
it 'should overwrite the old token', (done) ->
|
||||
async.waterfall [
|
||||
|
||||
(callback) ->
|
||||
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()', ->
|
||||
|
||||
it 'should return true', ->
|
||||
expect(token.hasToken()).to.be.true
|
||||
it 'should return true', (done) ->
|
||||
token.hasToken (hasToken) ->
|
||||
expect(hasToken).to.be.true
|
||||
done()
|
||||
|
||||
describe '#getToken()', ->
|
||||
|
||||
it 'should return the token', ->
|
||||
expect(token.getToken()).to.equal(johnDoeFixture.token)
|
||||
it 'should return the token', (done) ->
|
||||
token.getToken (error, savedToken) ->
|
||||
expect(error).to.not.exist
|
||||
expect(savedToken).to.equal(johnDoeFixture.token)
|
||||
done()
|
||||
|
||||
describe '#clearToken()', ->
|
||||
|
||||
it 'should effectively clear the token', ->
|
||||
expect(token.getToken()).to.equal(johnDoeFixture.token)
|
||||
token.clearToken()
|
||||
expect(token.getToken()).to.be.undefined
|
||||
it 'should effectively clear the token', (done) ->
|
||||
async.waterfall [
|
||||
|
||||
(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', ->
|
||||
|
||||
beforeEach ->
|
||||
token.clearToken()
|
||||
beforeEach (done) ->
|
||||
token.clearToken(done)
|
||||
|
||||
describe '#saveToken()', ->
|
||||
|
||||
it 'should save a token', ->
|
||||
token.saveToken(johnDoeFixture.token)
|
||||
expect(token.getToken()).to.equal(johnDoeFixture.token)
|
||||
it 'should save a token', (done) ->
|
||||
async.waterfall [
|
||||
|
||||
(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()', ->
|
||||
|
||||
it 'should return false', ->
|
||||
expect(token.hasToken()).to.be.false
|
||||
it 'should return false', (done) ->
|
||||
token.hasToken (hasToken) ->
|
||||
expect(hasToken).to.be.false
|
||||
done()
|
||||
|
||||
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)
|
||||
it 'should return undefined', (done) ->
|
||||
token.getToken (error, savedToken) ->
|
||||
expect(error).to.not.exist
|
||||
expect(savedToken).to.be.undefined
|
||||
done()
|
||||
|
Loading…
Reference in New Issue
Block a user