From 53c25412c3621d5e4a0da14045a4bc5bfc04c457 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Tue, 18 Nov 2014 12:11:20 -0400 Subject: [PATCH] Make auth.parseCredentials async --- lib/auth/auth.coffee | 8 ++++---- lib/auth/auth.spec.coffee | 29 ++++++++++++++++------------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/auth/auth.coffee b/lib/auth/auth.coffee index 1dfa51df..f88435ee 100644 --- a/lib/auth/auth.coffee +++ b/lib/auth/auth.coffee @@ -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) - } diff --git a/lib/auth/auth.spec.coffee b/lib/auth/auth.spec.coffee index 312fe5f1..d3b72fc0 100644 --- a/lib/auth/auth.spec.coffee +++ b/lib/auth/auth.spec.coffee @@ -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()