2014-11-25 13:37:53 +00:00
|
|
|
_ = require('lodash')
|
|
|
|
nock = require('nock')
|
|
|
|
sinon = require('sinon')
|
|
|
|
expect = require('chai').expect
|
2014-12-05 15:51:52 +00:00
|
|
|
resin = require('../resin')
|
2014-11-27 14:06:11 +00:00
|
|
|
cliPermissions = require('./cli-permissions')
|
2014-12-05 15:51:52 +00:00
|
|
|
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()
|
2014-12-05 15:51:52 +00:00
|
|
|
resin.data.prefix.set(resin.settings.get('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-12-05 15:51:52 +00:00
|
|
|
resin.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()
|
|
|
|
|
|
|
|
describe 'if logged in', ->
|
|
|
|
|
|
|
|
beforeEach (done) ->
|
2014-12-05 15:51:52 +00:00
|
|
|
nock(resin.settings.get('remoteUrl'))
|
2014-11-25 13:37:53 +00:00
|
|
|
.post('/login_', johnDoeFixture.credentials)
|
|
|
|
.reply(200, johnDoeFixture.token)
|
|
|
|
|
2014-12-05 15:51:52 +00:00
|
|
|
resin.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()
|