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