Add register function to resin/auth

This commit is contained in:
Juan Cruz Viotti 2014-12-24 10:59:25 -04:00
parent 8624ff74d9
commit 33e209def5
3 changed files with 79 additions and 0 deletions

View File

@ -131,3 +131,41 @@ exports.logout = (callback = _.noop) ->
data.remove(usernameKey, callback)
], _.unary(callback))
# Register to Resin.io
#
# @param {Object} credentials in the form of username, password and email
# @option credentials {String} username the username
# @option credentials {String} password user password
# @option credentials {String} email the user email
# @param {Function} callback callback (error, token)
#
# @example Register to Resin.io
# resin.auth.register {
# username: 'johndoe'
# password: 'secret'
# email: 'johndoe@gmail.com'
# }, (error, token) ->
# throw error if error?
# console.log(token)
#
exports.register = (credentials = {}, callback) ->
if not credentials.username?
return callback(new Error('Missing username'))
if not credentials.password?
return callback(new Error('Missing password'))
if not credentials.email?
return callback(new Error('Missing email'))
async.waterfall([
(callback) ->
url = settings.get('urls.register')
server.post(url, credentials, callback)
(response, body, callback) ->
return callback(null, body)
], callback)

View File

@ -24,6 +24,46 @@ describe 'Auth:', ->
afterEach ->
mock.fs.restore()
describe '#register()', ->
beforeEach ->
@credentials =
username: johnDoeFixture.credentials.username
password: johnDoeFixture.credentials.password
email: 'johndoe@gmail.com'
nock(settings.get('remoteUrl'))
.post(settings.get('urls.register'), @credentials)
.reply(201, johnDoeFixture.token)
describe 'given invalid credentials', ->
it 'should fail if no email', (done) ->
auth.register _.omit(@credentials, 'email'), (error, token) ->
expect(error).to.be.an.instanceof(Error)
expect(token).to.not.exist
done()
it 'should fail if no username', (done) ->
auth.register _.omit(@credentials, 'username'), (error, token) ->
expect(error).to.be.an.instanceof(Error)
expect(token).to.not.exist
done()
it 'should fail if no password', (done) ->
auth.register _.omit(@credentials, 'password'), (error, token) ->
expect(error).to.be.an.instanceof(Error)
expect(token).to.not.exist
done()
describe 'given valid credentials', ->
it 'should be able to register a username', (done) ->
auth.register @credentials, (error, token) ->
expect(error).to.not.exist
expect(token).to.equal(johnDoeFixture.token)
done()
describe 'given valid credentials', ->
beforeEach ->

View File

@ -36,6 +36,7 @@ settings =
urls:
signup: '/signup'
preferences: '/preferences'
register: '/user/register'
keys: '/user/keys'
identify: '/blink'
authenticate: '/login_'