mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-04-07 11:26:41 +00:00
Make auth.parseCredentials async
This commit is contained in:
parent
d671832b5a
commit
53c25412c3
@ -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)
|
||||
}
|
||||
|
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user