From 41d73eaf98567e46bfa71ac288ad1b534e8dadbf Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Fri, 31 Oct 2014 15:19:16 -0400 Subject: [PATCH] Implement precarious token persistence mechanism --- lib/cli-modules/auth/auth.spec.coffee | 2 +- lib/token/token.coffee | 15 +++++ lib/token/token.spec.coffee | 63 +++++++++++++++++++ tests/fixtures/janedoe.json | 7 +++ .../auth => tests}/fixtures/johndoe.json | 0 5 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 lib/token/token.coffee create mode 100644 lib/token/token.spec.coffee create mode 100644 tests/fixtures/janedoe.json rename {lib/cli-modules/auth => tests}/fixtures/johndoe.json (100%) diff --git a/lib/cli-modules/auth/auth.spec.coffee b/lib/cli-modules/auth/auth.spec.coffee index eb94fbe1..285535d3 100644 --- a/lib/cli-modules/auth/auth.spec.coffee +++ b/lib/cli-modules/auth/auth.spec.coffee @@ -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', -> diff --git a/lib/token/token.coffee b/lib/token/token.coffee new file mode 100644 index 00000000..b07a8bd1 --- /dev/null +++ b/lib/token/token.coffee @@ -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 diff --git a/lib/token/token.spec.coffee b/lib/token/token.spec.coffee new file mode 100644 index 00000000..3bd1f2c1 --- /dev/null +++ b/lib/token/token.spec.coffee @@ -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) diff --git a/tests/fixtures/janedoe.json b/tests/fixtures/janedoe.json new file mode 100644 index 00000000..9bd10fa3 --- /dev/null +++ b/tests/fixtures/janedoe.json @@ -0,0 +1,7 @@ +{ + "token": "eyJ0eXAiOiJHS1QiLCJhbGciOiJajkajsiausa98938493jkjacI6Imp2aW90dGkiLCJlbWFpbCI6Imp1YW5jaGl2aW998a98dcISJDKSAVhd897d89asdiasd98a98dcISJDKSAVhd897d89asdiasd98a98dcISJDKSAVhd897d89asdiasd98a98dcISJDKSAVhd897d89asdiasd98a98dcISJDKSAVhd897d89asdiasd98a98dcISJDKSAVhd897d89asdiasd98a98dcISJDKSAVhd897d89asdiasd21lIiwiYWRtaW4ubG9naW5fYXNfdXNlciJdLCJpYXQiOjE0MTQ3NzMyMTN9.j4gMqZlnVohJt8jLpzpJWZS6rFUpNCWDwa13woo58aY", + "credentials": { + "username": "janedoe", + "password": "mysecret" + } +} diff --git a/lib/cli-modules/auth/fixtures/johndoe.json b/tests/fixtures/johndoe.json similarity index 100% rename from lib/cli-modules/auth/fixtures/johndoe.json rename to tests/fixtures/johndoe.json