balena-cli/lib/resin/cli/cli-permissions.spec.coffee

85 lines
2.1 KiB
CoffeeScript
Raw Normal View History

2014-11-25 13:37:53 +00:00
_ = require('lodash')
nock = require('nock')
sinon = require('sinon')
expect = require('chai').expect
2014-11-27 14:06:11 +00:00
data = require('../data/data')
auth = require('../auth/auth')
settings = require('../settings')
2014-11-27 14:06:11 +00:00
cliPermissions = require('./cli-permissions')
johnDoeFixture = require('../../../tests/fixtures/johndoe')
mock = require('../../../tests/utils/mock')
2014-11-25 13:37:53 +00:00
2014-11-27 14:06:11 +00:00
describe 'CLI Permissions:', ->
2014-11-25 13:37:53 +00:00
2014-11-27 14:06:11 +00:00
describe '#user()', ->
2014-11-25 13:37:53 +00:00
before ->
mock.connection.init()
after ->
mock.connection.restore()
2014-11-26 13:19:02 +00:00
beforeEach (done) ->
mock.fs.init()
data.prefix.set(settings.dataPrefix, done)
2014-11-26 13:19:02 +00:00
afterEach ->
mock.fs.restore()
2014-11-25 13:37:53 +00:00
describe 'if not logged in', ->
beforeEach (done) ->
2014-11-27 14:06:11 +00:00
auth.logout(done)
2014-11-25 13:37:53 +00:00
it 'should not call the function', (done) ->
spy = sinon.spy()
2014-11-27 14:06:11 +00:00
cliPermissions.user(spy, _.noop)()
2014-11-25 13:37:53 +00:00
_.defer ->
expect(spy).to.not.have.been.called
done()
it 'it should call the second function with an error', (done) ->
2014-11-27 14:06:11 +00:00
func = cliPermissions.user _.noop, (error) ->
2014-11-25 13:37:53 +00:00
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', ->
2014-11-27 14:06:11 +00:00
func = cliPermissions.user(_.noop)
2014-11-25 13:37:53 +00:00
try
func()
catch error
expect(error).to.be.an.instanceof(Error)
describe 'if logged in', ->
beforeEach (done) ->
nock(settings.remoteUrl)
2014-11-25 13:37:53 +00:00
.post('/login_', johnDoeFixture.credentials)
.reply(200, johnDoeFixture.token)
2014-11-27 14:06:11 +00:00
auth.login(johnDoeFixture.credentials, done)
2014-11-25 13:37:53 +00:00
it 'should call the function with the correct arguments', (done) ->
args = [ 1, 2, 3, 'foo', 'bar' ]
spy = sinon.spy()
2014-11-27 14:06:11 +00:00
cliPermissions.user(spy, _.noop).apply(null, args)
2014-11-25 13:37:53 +00:00
_.defer ->
expect(spy).to.have.been.calledWith(args...)
done()
it 'should not call the second function', (done) ->
spy = sinon.spy()
2014-11-27 14:06:11 +00:00
cliPermissions.user(_.noop, spy)()
2014-11-25 13:37:53 +00:00
_.defer ->
expect(spy).to.not.have.been.called
done()