From ea13f7e11e479163c1b479ea5885cbcb21939d76 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Tue, 25 Nov 2014 09:37:53 -0400 Subject: [PATCH] Test failIfNotLoggedIn auth hook --- lib/hooks/auth.coffee | 7 +++- lib/hooks/auth.spec.coffee | 76 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 lib/hooks/auth.spec.coffee diff --git a/lib/hooks/auth.coffee b/lib/hooks/auth.coffee index d5c18771..ee90589a 100644 --- a/lib/hooks/auth.coffee +++ b/lib/hooks/auth.coffee @@ -1,11 +1,14 @@ +_ = require('lodash') auth = require('../auth/auth') messages = require('../messages/messages') -exports.failIfNotLoggedIn = (fn) -> +exports.failIfNotLoggedIn = (fn, onError) -> return -> args = arguments auth.isLoggedIn (isLoggedIn) -> + if not isLoggedIn - throw new Error(messages.errors.loginRequired) + error = new Error(messages.errors.loginRequired) + return onError?(error) or throw error fn.apply(null, args) diff --git a/lib/hooks/auth.spec.coffee b/lib/hooks/auth.spec.coffee new file mode 100644 index 00000000..6349af63 --- /dev/null +++ b/lib/hooks/auth.spec.coffee @@ -0,0 +1,76 @@ +_ = require('lodash') +nock = require('nock') +sinon = require('sinon') +expect = require('chai').expect +config = require('../config') +auth = require('../auth/auth') +authHooks = require('./auth') +johnDoeFixture = require('../../tests/fixtures/johndoe') +mock = require('../../tests/utils/mock') + +describe 'Auth Hooks:', -> + + describe '#failIfNotLoggedIn()', -> + + before -> + mock.connection.init() + + after -> + mock.connection.restore() + + describe 'if not logged in', -> + + beforeEach (done) -> + auth.logout(done) + + it 'should not call the function', (done) -> + spy = sinon.spy() + authHooks.failIfNotLoggedIn(spy, _.noop)() + + _.defer -> + expect(spy).to.not.have.been.called + done() + + it 'it should call the second function with an error', (done) -> + func = authHooks.failIfNotLoggedIn _.noop, (error) -> + expect(error).to.be.an.instanceof(Error) + done() + func() + + # TODO: expect(func).to.throw(Error) doesn't catches + # the error as it's being thrown inside an async function + # (auth.isLoggedIn). A try/catch works, but it still results + # in the error being printed in Mocha reporter. + xit 'should throw an error if no error handler function', -> + func = authHooks.failIfNotLoggedIn(_.noop) + try + func() + catch error + expect(error).to.be.an.instanceof(Error) + + describe 'if logged in', -> + + beforeEach (done) -> + nock(config.remoteUrl) + .post('/login_', johnDoeFixture.credentials) + .reply(200, johnDoeFixture.token) + + auth.login(johnDoeFixture.credentials, done) + + it 'should call the function with the correct arguments', (done) -> + args = [ 1, 2, 3, 'foo', 'bar' ] + + spy = sinon.spy() + authHooks.failIfNotLoggedIn(spy, _.noop).apply(null, args) + + _.defer -> + expect(spy).to.have.been.calledWith(args...) + done() + + it 'should not call the second function', (done) -> + spy = sinon.spy() + authHooks.failIfNotLoggedIn(_.noop, spy)() + + _.defer -> + expect(spy).to.not.have.been.called + done()