2014-10-31 18:47:18 +00:00
|
|
|
expect = require('chai').expect
|
|
|
|
nock = require('nock')
|
2014-11-17 18:40:32 +00:00
|
|
|
_ = require('lodash')
|
2014-11-14 13:51:59 +00:00
|
|
|
async = require('async')
|
2014-10-31 18:47:18 +00:00
|
|
|
auth = require('./auth')
|
2014-11-26 17:02:22 +00:00
|
|
|
data = require('../data/data')
|
2014-11-26 17:42:05 +00:00
|
|
|
config = require('../config')
|
2014-11-26 17:02:22 +00:00
|
|
|
mock = require('../../../tests/utils/mock')
|
|
|
|
johnDoeFixture = require('../../../tests/fixtures/johndoe')
|
|
|
|
janeDoeFixture = require('../../../tests/fixtures/janedoe')
|
2014-10-31 18:47:18 +00:00
|
|
|
|
2014-11-07 16:43:10 +00:00
|
|
|
describe 'Auth:', ->
|
2014-10-31 18:47:18 +00:00
|
|
|
|
2014-11-24 18:55:05 +00:00
|
|
|
before ->
|
|
|
|
mock.connection.init()
|
|
|
|
|
|
|
|
after ->
|
|
|
|
mock.connection.restore()
|
|
|
|
|
2014-11-18 12:34:40 +00:00
|
|
|
beforeEach (done) ->
|
2014-11-14 19:48:37 +00:00
|
|
|
mock.fs.init()
|
2014-11-26 17:02:22 +00:00
|
|
|
data.prefix.set(config.dataPrefix, done)
|
2014-11-14 19:48:37 +00:00
|
|
|
|
|
|
|
afterEach ->
|
|
|
|
mock.fs.restore()
|
|
|
|
|
2014-10-31 18:47:18 +00:00
|
|
|
describe 'given valid credentials', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
2014-11-18 12:53:07 +00:00
|
|
|
nock(config.remoteUrl)
|
2014-10-31 18:47:18 +00:00
|
|
|
.post('/login_', johnDoeFixture.credentials)
|
|
|
|
.reply(200, johnDoeFixture.token)
|
|
|
|
|
2014-11-14 13:51:59 +00:00
|
|
|
describe '#authenticate()', ->
|
2014-10-31 18:47:18 +00:00
|
|
|
|
|
|
|
it 'should return a token string', (done) ->
|
2014-11-14 13:51:59 +00:00
|
|
|
auth.authenticate johnDoeFixture.credentials, (error, token) ->
|
2014-10-31 18:47:18 +00:00
|
|
|
return done(error) if error?
|
|
|
|
expect(token).to.be.a('string')
|
|
|
|
expect(token).to.equal(johnDoeFixture.token)
|
|
|
|
done()
|
|
|
|
|
2014-11-14 13:51:59 +00:00
|
|
|
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)
|
|
|
|
|
2014-11-14 19:34:14 +00:00
|
|
|
(callback) ->
|
2014-11-14 13:51:59 +00:00
|
|
|
auth.isLoggedIn (isLoggedIn) ->
|
|
|
|
return callback(null, isLoggedIn)
|
|
|
|
|
|
|
|
(isLoggedIn, callback) ->
|
|
|
|
expect(isLoggedIn).to.be.true
|
|
|
|
return callback(null)
|
|
|
|
|
|
|
|
], (error) ->
|
|
|
|
expect(error).to.not.exist
|
|
|
|
done()
|
|
|
|
|
2014-10-31 18:47:18 +00:00
|
|
|
describe 'given invalid credentials', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
2014-11-18 12:53:07 +00:00
|
|
|
nock(config.remoteUrl)
|
2014-10-31 18:47:18 +00:00
|
|
|
.post('/login_')
|
|
|
|
.reply(401)
|
|
|
|
|
2014-11-14 13:51:59 +00:00
|
|
|
describe '#authenticate()', ->
|
2014-10-31 18:47:18 +00:00
|
|
|
|
|
|
|
it 'should return an error', (done) ->
|
2014-11-14 13:51:59 +00:00
|
|
|
auth.authenticate johnDoeFixture.credentials, (error, token) ->
|
2014-10-31 18:47:18 +00:00
|
|
|
expect(error).to.exist
|
|
|
|
expect(error).to.be.an.instanceof(Error)
|
2014-11-14 13:51:59 +00:00
|
|
|
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) ->
|
2014-11-18 12:53:07 +00:00
|
|
|
nock(config.remoteUrl)
|
2014-11-14 13:51:59 +00:00
|
|
|
.post('/login_', johnDoeFixture.credentials)
|
|
|
|
.reply(200, johnDoeFixture.token)
|
|
|
|
|
2014-11-18 12:53:07 +00:00
|
|
|
nock(config.remoteUrl)
|
2014-11-14 13:51:59 +00:00
|
|
|
.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)
|
|
|
|
|
2014-11-14 19:34:14 +00:00
|
|
|
(callback) ->
|
2014-11-14 13:51:59 +00:00
|
|
|
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
|
2014-10-31 18:47:18 +00:00
|
|
|
done()
|
2014-11-17 18:40:32 +00:00
|
|
|
|
|
|
|
describe '#parseCredentials', ->
|
|
|
|
|
|
|
|
describe 'given colon separated credentials', ->
|
|
|
|
|
|
|
|
username = null
|
|
|
|
password = null
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
username = 'johndoe'
|
|
|
|
password = 'mysecret'
|
|
|
|
|
2014-11-18 16:11:20 +00:00
|
|
|
it 'should parse the credentials correctly', (done) ->
|
|
|
|
auth.parseCredentials "#{username}:#{password}", (error, credentials) ->
|
|
|
|
expect(error).to.not.exist
|
|
|
|
expect(credentials.username).to.equal(username)
|
|
|
|
expect(credentials.password).to.equal(password)
|
|
|
|
done()
|
2014-11-17 18:40:32 +00:00
|
|
|
|
2014-11-18 16:11:20 +00:00
|
|
|
it 'should throw an error if it has two or more colons', (done) ->
|
|
|
|
auth.parseCredentials "#{username}:#{password}:#{username}", (error, credentials) ->
|
|
|
|
expect(error).to.be.an.instanceof(Error)
|
|
|
|
expect(credentials).to.not.exist
|
|
|
|
done()
|
2014-11-17 18:40:32 +00:00
|
|
|
|
2014-11-18 16:11:20 +00:00
|
|
|
it 'should throw an error if only the username is passed', (done) ->
|
|
|
|
auth.parseCredentials username, (error, credentials) ->
|
|
|
|
expect(error).to.be.an.instanceof(Error)
|
|
|
|
expect(credentials).to.not.exist
|
|
|
|
done()
|