mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-18 21:27:51 +00:00
Require internet connection before attempting to use server module
This commit is contained in:
parent
927b7bc0eb
commit
a6099b227b
@ -11,6 +11,12 @@ janeDoeFixture = require('../../tests/fixtures/janedoe')
|
||||
|
||||
describe 'Auth:', ->
|
||||
|
||||
before ->
|
||||
mock.connection.init()
|
||||
|
||||
after ->
|
||||
mock.connection.restore()
|
||||
|
||||
beforeEach (done) ->
|
||||
mock.fs.init()
|
||||
data.prefix.set(config.dataPrefix, done)
|
||||
|
6
lib/connection/connection.coffee
Normal file
6
lib/connection/connection.coffee
Normal file
@ -0,0 +1,6 @@
|
||||
isOnline = require('is-online')
|
||||
|
||||
# A wrapper around isOnline in order
|
||||
# to be able to stub it with Sinon
|
||||
|
||||
exports.isOnline = isOnline
|
36
lib/connection/connection.spec.coffee
Normal file
36
lib/connection/connection.spec.coffee
Normal file
@ -0,0 +1,36 @@
|
||||
expect = require('chai').expect
|
||||
sinon = require('sinon')
|
||||
connection = require('./connection')
|
||||
|
||||
describe 'Connection:', ->
|
||||
|
||||
describe '#isOnline()', ->
|
||||
|
||||
stubIsOnline = (expectations, args...) ->
|
||||
isOnlineStub = sinon.stub(connection, 'isOnline')
|
||||
isOnlineStub.yields(args...)
|
||||
|
||||
connection.isOnline (error, isOnline) ->
|
||||
expectations.apply(null, arguments)
|
||||
isOnlineStub.restore()
|
||||
|
||||
it 'should be able to return true', (done) ->
|
||||
stubIsOnline (error, isOnline) ->
|
||||
expect(error).to.not.exist
|
||||
expect(isOnline).to.be.true
|
||||
done()
|
||||
, null, true
|
||||
|
||||
it 'should be able to return false', (done) ->
|
||||
stubIsOnline (error, isOnline) ->
|
||||
expect(error).to.not.exist
|
||||
expect(isOnline).to.be.false
|
||||
done()
|
||||
, null, false
|
||||
|
||||
it 'should be able to return an error', (done) ->
|
||||
stubIsOnline (error, isOnline) ->
|
||||
expect(error).to.be.an.instanceof(Error)
|
||||
expect(isOnline).to.not.exist
|
||||
done()
|
||||
, new Error()
|
@ -6,6 +6,7 @@ chaiAsPromised = require('chai-as-promised')
|
||||
expect = chai.expect
|
||||
chai.use(chaiAsPromised)
|
||||
|
||||
mock = require('../../tests/utils/mock')
|
||||
canvas = require('./_canvas')
|
||||
config = require('../config')
|
||||
|
||||
@ -21,6 +22,12 @@ RESPONSE =
|
||||
|
||||
describe 'Canvas:', ->
|
||||
|
||||
before ->
|
||||
mock.connection.init()
|
||||
|
||||
after ->
|
||||
mock.connection.restore()
|
||||
|
||||
beforeEach ->
|
||||
nock(config.remoteUrl)
|
||||
.get(URI.application)
|
||||
|
@ -2,6 +2,7 @@ _ = require('lodash')
|
||||
request = require('request')
|
||||
urlResolve = require('url').resolve
|
||||
async = require('async')
|
||||
connection = require('../connection/connection')
|
||||
config = require('../config')
|
||||
token = require('../token/token')
|
||||
|
||||
@ -13,6 +14,12 @@ exports.request = (options = {}, callback) ->
|
||||
async.waterfall [
|
||||
|
||||
(callback) ->
|
||||
connection.isOnline(callback)
|
||||
|
||||
(isOnline, callback) ->
|
||||
if not isOnline
|
||||
return callback(new Error('You need internet connection to perform this task'))
|
||||
|
||||
token.getToken(callback)
|
||||
|
||||
(savedToken, callback) ->
|
||||
|
@ -4,6 +4,7 @@ url = require('url')
|
||||
server = require('./server')
|
||||
config = require('../config')
|
||||
token = require('../token/token')
|
||||
data = require('../data/data')
|
||||
mock = require('../../tests/utils/mock')
|
||||
johnDoeFixture = require('../../tests/fixtures/johndoe.json')
|
||||
|
||||
@ -32,7 +33,13 @@ METHODS = [
|
||||
|
||||
describe 'Server:', ->
|
||||
|
||||
beforeEach ->
|
||||
before ->
|
||||
mock.connection.init()
|
||||
|
||||
after ->
|
||||
mock.connection.restore()
|
||||
|
||||
beforeEach (done) ->
|
||||
nock(TEST_URI).get(URI.nojson).reply(200, RESPONSE.nojson)
|
||||
nock(TEST_URI).get(URI.error).reply(400, status: STATUS.error)
|
||||
|
||||
@ -41,6 +48,7 @@ describe 'Server:', ->
|
||||
nock(TEST_URI)[lowercaseMethod](URI.ok).reply(200, status: STATUS.ok)
|
||||
|
||||
mock.fs.init()
|
||||
data.prefix.set(config.dataPrefix, done)
|
||||
|
||||
afterEach ->
|
||||
mock.fs.restore()
|
||||
@ -183,7 +191,7 @@ describe 'Server:', ->
|
||||
method: 'GET'
|
||||
url: URI.ok
|
||||
}, (error, response) ->
|
||||
authorizationHeader = response.request.headers.Authorization
|
||||
authorizationHeader = response?.request.headers.Authorization
|
||||
|
||||
expect(error).to.not.exist
|
||||
expect(authorizationHeader).to.exist
|
||||
@ -203,7 +211,7 @@ describe 'Server:', ->
|
||||
url: URI.ok
|
||||
}, (error, response) ->
|
||||
expect(error).to.not.exist
|
||||
authorizationHeader = response.request.headers.Authorization
|
||||
authorizationHeader = response?.request.headers.Authorization
|
||||
expect(authorizationHeader).to.not.exist
|
||||
done()
|
||||
|
||||
|
@ -44,6 +44,7 @@
|
||||
"inquirer": "~0.8.0",
|
||||
"cliff": "~0.1.9",
|
||||
"underscore.string": "~2.4.0",
|
||||
"typed-error": "~0.1.0"
|
||||
"typed-error": "~0.1.0",
|
||||
"is-online": "~3.0.0"
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
mockFs = require('mock-fs')
|
||||
sinon = require('sinon')
|
||||
config = require('../../lib/config')
|
||||
connection = require('../../lib/connection/connection')
|
||||
|
||||
exports.fs =
|
||||
|
||||
@ -16,3 +18,14 @@ exports.fs =
|
||||
|
||||
restore: ->
|
||||
mockFs.restore()
|
||||
|
||||
isOnlineStub = null
|
||||
|
||||
exports.connection =
|
||||
|
||||
init: ->
|
||||
isOnlineStub = sinon.stub(connection, 'isOnline')
|
||||
isOnlineStub.yields(null, true)
|
||||
|
||||
restore: ->
|
||||
isOnlineStub.restore()
|
||||
|
Loading…
Reference in New Issue
Block a user