Make auth.parseCredentials async

This commit is contained in:
Juan Cruz Viotti 2014-11-18 12:11:20 -04:00
parent d671832b5a
commit 53c25412c3
2 changed files with 20 additions and 17 deletions

View File

@ -27,13 +27,13 @@ exports.getToken = token.getToken
# like that to invalidate the token on the server?
exports.logout = token.clearToken
exports.parseCredentials = (credentials) ->
exports.parseCredentials = (credentials, callback) ->
result = credentials.split(':')
if result.length isnt 2
throw new Error('Invalid credentials. The expected input is username:password.')
error = new Error('Invalid credentials. The expected input is username:password.')
return callback?(error)
return {
callback? null,
username: _.first(result)
password: _.last(result)
}

View File

@ -175,18 +175,21 @@ describe 'Auth:', ->
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 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()
it 'should throw an error if it has two or more colons', ->
parseFunction = _.partial(auth.parseCredentials, "#{username}:#{password}:#{username}")
expect(parseFunction).to.throw(Error)
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()
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)
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()