UNPKG

authbase

Version:

AuthBase client library

111 lines (90 loc) 4.08 kB
var expect = require('expect.js'), mock = require('./mock')('https://api.authbase.io', 'id', 'secret'), AuthBase = require('../../lib/node'), errors = require('../../lib/node/errors'); describe('AuthBase Client - API', function() { describe('Initialization options', function() { function harness(config, expectedError) { return function() { expect(function() { AuthBase(config); }).to.throwException(function(e) { expect(e.code).to.be(expectedError.code); }); }; } it ('should raise an exception on instantiation when missing appId', harness({ appSecret: 'foo' }, errors.missingAppId)); it ('should raise an exception on instantiation when missing appSecret', harness({ appId: 'foo' }, errors.missingAppSecret)); }); describe('API calls', function() { var api = AuthBase({appId: 'id', appSecret: 'secret'}); function testAtEndpoint(proc, args, request) { return function() { it('should pass the payload as the second argument to the callback on success', function(done) { var res = request.reply(200, {data: {id: '123'}}); proc.apply(null, args.concat([function(err, user) { expect(err).to.be(undefined); expect(user.id).to.be('123'); res.done(); done(); }])); }); it('should pass the payload to the promise on success', function(done) { var res = request.reply(200, {data: {id: '123'}}); proc.apply(null, args) .then(function(user) { expect(user.id).to.be('123'); res.done(); done(); }).catch(done); }); it('should pass body to callback as error on failure', function(done) { var res = request.reply(400, { error: 'this is an error' }); proc.apply(null, args.concat([ function(err, user) { expect(user).to.be(undefined); expect(err).to.eql({error: 'this is an error'}); res.done(); done(); }])); }); it('should pass the payload to the promise on failure', function(done) { var res = request.reply(400, { errors: [] }); proc.apply(null, args) .then(function(user) { // should never hit this on error, so automatically fail expect(false).to.be(true); }).catch(function(err) { expect(err).to.eql({ errors: [] }); res.done(); done(); }).catch(done); }); it('should pass body to promise as error on failure', function(done) { var res = request.reply(400, { error: 'this is an error' }); proc.apply(null, args) .then(function(user) { // should never hit this on error, so automatically fail expect(false).to.be(true); }).catch(function(err) { expect(err).to.eql({error: 'this is an error'}); res.done(); done(); }).catch(done); }); }; } var payload = {email: 'foo', password: 'bar'}; describe('Register', testAtEndpoint(api.register, [payload], mock.dummyPost('/register', payload))); describe('Unregister', testAtEndpoint(api.unregister, [payload], mock.dummyPost('/unregister', payload))); describe('Verify', testAtEndpoint(api.verify, [payload], mock.dummyGet('/verify', payload))); describe('Set Profile', testAtEndpoint(api.setProfile, [123, 'value'], mock.dummyPost('/user/123/profile/', {data: 'value'}))); describe('Set Nested Profile', testAtEndpoint(api.setProfile, [123, 'this.is.a.path', 'value'], mock.dummyPost('/user/123/profile/this/is/a/path', {data: 'value'}))); describe('Get Profile', testAtEndpoint(api.getProfile, [123], mock.dummyGet('/user/123/profile/', undefined))); describe('Get Nested Profile', testAtEndpoint(api.getProfile, [123, 'this.is.a.path'], mock.dummyGet('/user/123/profile/this/is/a/path', undefined))); }); });