Implement auth.parseCredentials()

This commit is contained in:
Juan Cruz Viotti 2014-11-17 14:40:32 -04:00
parent 8e2217b9d5
commit b0fe4f41c1
2 changed files with 40 additions and 0 deletions

View File

@ -1,4 +1,5 @@
async = require('async')
_ = require('lodash')
server = require('../../server/server')
token = require('../../token/token')
@ -25,3 +26,14 @@ 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
exports.parseCredentials = (credentials) ->
result = credentials.split(':')
if result.length isnt 2
throw new Error('Invalid credentials')
return {
username: _.first(result)
password: _.last(result)
}

View File

@ -1,5 +1,6 @@
expect = require('chai').expect
nock = require('nock')
_ = require('lodash')
async = require('async')
auth = require('./auth')
config = require('../../config')
@ -160,3 +161,30 @@ describe 'Auth:', ->
], (error) ->
expect(error).to.not.exist
done()
describe '#parseCredentials', ->
describe 'given colon separated credentials', ->
username = null
password = null
beforeEach ->
username = 'johndoe'
password = 'mysecret'
it 'should parse the credentials correctly', ->
parsedCredentials = auth.parseCredentials("#{username}:#{password}")
expect(parsedCredentials.username).to.equal(username)
expect(parsedCredentials.password).to.equal(password)
it 'should throw an error if it has two or more colons', ->
parseFunction = _.partial(auth.parseCredentials, "#{username}:#{password}:#{username}")
expect(parseFunction).to.throw(Error)
parseFunction = _.partial(auth.parseCredentials, "#{username}:#{password}:#{username}:#{password}")
expect(parseFunction).to.throw(Error)
it 'should throw an error if only the username is passed', ->
parseFunction = _.partial(auth.parseCredentials, username)
expect(parseFunction).to.throw(Error)