From 58300acb773952dbc315210343d836ecb4e5dc9c Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Fri, 14 Nov 2014 09:51:59 -0400 Subject: [PATCH] Implement Auth module --- lib/cli-modules/auth/auth.coffee | 25 ++++- lib/cli-modules/auth/auth.spec.coffee | 127 +++++++++++++++++++++++++- 2 files changed, 145 insertions(+), 7 deletions(-) diff --git a/lib/cli-modules/auth/auth.coffee b/lib/cli-modules/auth/auth.coffee index 116700f6..6209742a 100644 --- a/lib/cli-modules/auth/auth.coffee +++ b/lib/cli-modules/auth/auth.coffee @@ -1,8 +1,27 @@ -server = require('../../server/server') +async = require('async') -exports.getToken = (credentials, callback) -> +server = require('../../server/server') +token = require('../../token/token') + +exports.authenticate = (credentials, callback) -> server.post '/login_', credentials, (error, response) -> return callback(error, response?.body) exports.login = (credentials, callback) -> - exports.getToken(credentials, callback) + async.waterfall([ + + (callback) -> + exports.authenticate(credentials, callback) + + (authToken, callback) -> + token.saveToken(authToken, callback) + + ], callback) + +# Handy aliases +exports.isLoggedIn = token.hasToken +exports.getToken = token.getToken + +# TODO: Maybe we should post to /logout or something +# like that to invalidate the token on the server? +exports.logout = token.clearToken diff --git a/lib/cli-modules/auth/auth.spec.coffee b/lib/cli-modules/auth/auth.spec.coffee index 8192b100..173a5fa9 100644 --- a/lib/cli-modules/auth/auth.spec.coffee +++ b/lib/cli-modules/auth/auth.spec.coffee @@ -1,8 +1,10 @@ expect = require('chai').expect nock = require('nock') +async = require('async') auth = require('./auth') config = require('../../config') johnDoeFixture = require('../../../tests/fixtures/johndoe') +janeDoeFixture = require('../../../tests/fixtures/janedoe') describe 'Auth:', -> @@ -13,15 +15,41 @@ describe 'Auth:', -> .post('/login_', johnDoeFixture.credentials) .reply(200, johnDoeFixture.token) - describe '#getToken()', -> + describe '#authenticate()', -> it 'should return a token string', (done) -> - auth.getToken johnDoeFixture.credentials, (error, token) -> + auth.authenticate johnDoeFixture.credentials, (error, token) -> return done(error) if error? expect(token).to.be.a('string') expect(token).to.equal(johnDoeFixture.token) done() + describe '#login()', -> + + it 'should save the token', (done) -> + async.waterfall [ + + (callback) -> + auth.isLoggedIn (isLoggedIn) -> + return callback(null, isLoggedIn) + + (isLoggedIn, callback) -> + expect(isLoggedIn).to.be.false + auth.login(johnDoeFixture.credentials, callback) + + (authToken, callback) -> + expect(authToken).to.be.a.string + auth.isLoggedIn (isLoggedIn) -> + return callback(null, isLoggedIn) + + (isLoggedIn, callback) -> + expect(isLoggedIn).to.be.true + return callback(null) + + ], (error) -> + expect(error).to.not.exist + done() + describe 'given invalid credentials', -> beforeEach -> @@ -29,10 +57,101 @@ describe 'Auth:', -> .post('/login_') .reply(401) - describe '#getToken()', -> + describe '#authenticate()', -> it 'should return an error', (done) -> - auth.getToken johnDoeFixture.credentials, (error, token) -> + auth.authenticate johnDoeFixture.credentials, (error, token) -> expect(error).to.exist expect(error).to.be.an.instanceof(Error) + expect(token).to.be.undefined + done() + + describe '#login()', -> + + it 'should return an error', (done) -> + auth.login johnDoeFixture.credentials, (error, token) -> + expect(error).to.exist + expect(error).to.be.an.instanceof(Error) + expect(token).to.be.undefined + done() + + describe 'given a logged in user', -> + + beforeEach (done) -> + nock(config.baseUrl) + .post('/login_', johnDoeFixture.credentials) + .reply(200, johnDoeFixture.token) + + nock(config.baseUrl) + .post('/login_', janeDoeFixture.credentials) + .reply(200, janeDoeFixture.token) + + auth.login(johnDoeFixture.credentials, done) + + describe '#login()', -> + + it 'should override the old user', (done) -> + async.waterfall [ + + (callback) -> + auth.getToken(callback) + + (token, callback) -> + expect(token).to.equal(johnDoeFixture.token) + auth.login(janeDoeFixture.credentials, callback) + + (token, callback) -> + expect(token).to.be.a.string + auth.getToken(callback) + + (token, callback) -> + expect(token).to.equal(janeDoeFixture.token) + return callback(null) + + ], (error) -> + expect(error).to.not.exist + done() + + describe '#isLoggedIn()', -> + + it 'should return true', (done) -> + auth.isLoggedIn (isLoggedIn) -> + expect(isLoggedIn).to.be.true + done() + + describe '#getToken()', -> + + it 'should return the saved token', (done) -> + auth.getToken (error, token) -> + expect(error).to.not.exist + expect(token).to.equal(johnDoeFixture.token) + done() + + describe '#logout()', -> + + it 'should effectively logout the user', (done) -> + async.waterfall [ + + (callback) -> + auth.isLoggedIn (isLoggedIn) -> + return callback(null, isLoggedIn) + + (isLoggedIn, callback) -> + expect(isLoggedIn).to.be.true + auth.logout(callback) + + (callback) -> + auth.isLoggedIn (isLoggedIn) -> + return callback(null, isLoggedIn) + + (isLoggedIn, callback) -> + expect(isLoggedIn).to.be.false + auth.getToken(callback) + + (token, callback) -> + expect(token).to.be.undefined + return callback(null) + + ], (error) -> + expect(error).to.not.exist done()