Implement basic Auth module that fetches a token

This commit is contained in:
Juan Cruz Viotti 2014-10-31 14:47:18 -04:00
parent b52d1ccb6d
commit 463ed6473b
3 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,8 @@
server = require('../../server/server')
exports.getToken = (credentials, callback) ->
server.post '/login_', credentials, (error, response) ->
return callback(error, response?.body)
exports.login = (credentials, callback) ->
exports.getToken(credentials, callback)

View File

@ -0,0 +1,38 @@
expect = require('chai').expect
nock = require('nock')
auth = require('./auth')
config = require('../../config')
johnDoeFixture = require('./fixtures/johndoe')
describe 'Auth', ->
describe 'given valid credentials', ->
beforeEach ->
nock(config.baseUrl)
.post('/login_', johnDoeFixture.credentials)
.reply(200, johnDoeFixture.token)
describe '#getToken()', ->
it 'should return a token string', (done) ->
auth.getToken johnDoeFixture.credentials, (error, token) ->
return done(error) if error?
expect(token).to.be.a('string')
expect(token).to.equal(johnDoeFixture.token)
done()
describe 'given invalid credentials', ->
beforeEach ->
nock(config.baseUrl)
.post('/login_')
.reply(401)
describe '#getToken()', ->
it 'should return an error', (done) ->
auth.getToken johnDoeFixture.credentials, (error, token) ->
expect(error).to.exist
expect(error).to.be.an.instanceof(Error)
done()

View File

@ -0,0 +1,7 @@
{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6Imp2aW90dGkiLCJlbWFpbCI6Imp1YW5jaGl2aW90dGlAZ21haWwuY29tIiwiZ2l0bGFiX2lkIjoxNjUsImhhc1Bhc3N3b3JkU2V0Ijp0cnVlLCJwdWJsaWNfa2V5Ijp0cnVlLCJmZWF0dXJlcyI6W10sImlkIjoxNzUsInBlcm1pc3Npb25zIjpbImV3YS5kZXZpY2UuZ2V0IiwiYWRtaW4uY3JlYXRlX2ludml0ZXMiLCJhZG1pbi5ob21lIiwiYWRtaW4ubG9naW5fYXNfdXNlciJdLCJpYXQiOjE0MTQ3NzMyMTN9.j4gMqZlnVohJt8jLpzpJWZS6rFUpNCWDwa13woo58aY",
"credentials": {
"username": "johndoe",
"password": "mysecret"
}
}