balena-cli/lib/resin/token/token.spec.coffee

121 lines
2.6 KiB
CoffeeScript
Raw Normal View History

expect = require('chai').expect
async = require('async')
token = require('./token')
2014-11-26 16:42:10 +00:00
mock = require('../../../tests/utils/mock')
2014-11-26 16:42:10 +00:00
johnDoeFixture = require('../../../tests/fixtures/johndoe.json')
janeDoeFixture = require('../../../tests/fixtures/janedoe.json')
2014-11-07 16:43:10 +00:00
describe 'Token:', ->
2014-11-14 19:48:37 +00:00
beforeEach ->
mock.fs.init()
afterEach ->
mock.fs.restore()
describe 'given a user that is logged in', ->
beforeEach (done) ->
token.saveToken(johnDoeFixture.token, done)
describe '#saveToken()', ->
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)
(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', (done) ->
token.hasToken (hasToken) ->
expect(hasToken).to.be.true
done()
describe '#getToken()', ->
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', (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 (done) ->
token.clearToken(done)
describe '#saveToken()', ->
it 'should save a token', (done) ->
async.waterfall [
(callback) ->
token.saveToken(johnDoeFixture.token, callback)
2014-11-14 19:18:08 +00:00
(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', (done) ->
token.hasToken (hasToken) ->
expect(hasToken).to.be.false
done()
describe '#getToken()', ->
it 'should return undefined', (done) ->
token.getToken (error, savedToken) ->
expect(error).to.not.exist
expect(savedToken).to.be.undefined
done()