Refactor tests

This commit is contained in:
Juan Cruz Viotti 2014-12-05 12:52:31 -04:00
parent 4b74ec7fc4
commit ceb2a1cf63
10 changed files with 172 additions and 194 deletions

View File

@ -3,34 +3,6 @@ _ = require('lodash')
sinon = require('sinon')
connection = require('./connection')
CONNECTION_PARAMETERS =
validEthernet:
network: 'ethernet'
validEthernetPlusExtra:
network: 'ethernet'
foo: 'bar'
hello: 'world'
validWifi:
network: 'wifi'
wifiSsid: 'mySsid'
wifiKey: 'mySecret'
ethernetAndWifiOptions:
network: 'ethernet'
wifiSsid: 'mySsid'
wifiKey: 'mySecret'
ethernetAndUndefinedWifi:
network: 'ethernet'
wifiSsid: undefined
wifiKey: undefined
wifiWithoutOptions:
network: 'wifi'
unknownWithOptions:
network: 'foobar'
wifiSsid: 'mySsid'
wifiKey: 'mySecret'
unknownWithoutOptions:
network: 'foobar'
describe 'Connection:', ->
describe '#isOnline()', ->
@ -94,27 +66,43 @@ describe 'Connection:', ->
describe 'if it succeeds', ->
it 'should pass the parameters as the second argument', ->
params = CONNECTION_PARAMETERS.validEthernet
params =
network: 'ethernet'
connection.parseConnectionParameters params, (error, parameters) ->
expect(parameters).to.deep.equal(params)
it 'should discard extra parameters', ->
params = CONNECTION_PARAMETERS.validEthernetPlusExtra
params =
network: 'ethernet'
foo: 'bar'
hello: 'world'
connection.parseConnectionParameters params, (error, parameters) ->
expect(parameters).to.deep.equal(CONNECTION_PARAMETERS.validEthernet)
expect(parameters).to.deep.equal(network: 'ethernet')
describe 'if network is ethernet', ->
it 'should succeed if no wifi options', (done) ->
params = CONNECTION_PARAMETERS.validEthernet
params =
network: 'ethernet'
checkParamsSuccess(params, done)
it 'should fail if it has wifi options', (done) ->
params = CONNECTION_PARAMETERS.ethernetAndWifiOptions
params =
network: 'ethernet'
wifiSsid: 'mySsid'
wifiKey: 'mySecret'
checkParamsFailure(params, done)
it 'should discard undefined wifi related options', (done) ->
params = CONNECTION_PARAMETERS.ethernetAndUndefinedWifi
params =
network: 'ethernet'
wifiSsid: undefined
wifiKey: undefined
connection.parseConnectionParameters params, (error, result) ->
expect(error).to.not.exist
expect(result).to.deep.equal(network: 'ethernet')
@ -123,19 +111,31 @@ describe 'Connection:', ->
describe 'if network is wifi', ->
it 'should succeed if has options', (done) ->
params = CONNECTION_PARAMETERS.validWifi
params =
network: 'wifi'
wifiSsid: 'mySsid'
wifiKey: 'mySecret'
checkParamsSuccess(params, done)
it 'should fail if missing options', (done) ->
params = CONNECTION_PARAMETERS.wifiWithoutOptions
params =
network: 'wifi'
checkParamsFailure(params, done)
describe 'if network is unknown', ->
it 'should fail with options', (done) ->
params = CONNECTION_PARAMETERS.unknownWithOptions
params =
network: 'foobar'
wifiSsid: 'mySsid'
wifiKey: 'mySecret'
checkParamsFailure(params, done)
it 'should fail without options', (done) ->
params = CONNECTION_PARAMETERS.unknownWithoutOptions
params =
network: 'foobar'
checkParamsFailure(params, done)

View File

@ -4,21 +4,18 @@ _ = require('lodash')
helpers = require('./helpers')
resin = require('../resin')
STRING =
numbers: '1234567812345678'
describe 'Helpers:', ->
describe '#formatLongString()', ->
it 'should format a string', ->
result = helpers.formatLongString(STRING.numbers, 4)
result = helpers.formatLongString('1234567812345678', 4)
expect(result).to.equal('1234\n5678\n1234\n5678')
it 'should return the same string if n is null/undefined', ->
for value in [ undefined, null ]
result = helpers.formatLongString(STRING.numbers, value)
expect(result).to.equal(STRING.numbers)
result = helpers.formatLongString('1234567890', value)
expect(result).to.equal('1234567890')
it 'should throw an error if input is not a string', ->
for value in [
@ -32,9 +29,9 @@ describe 'Helpers:', ->
expect(fn).to.throw
it 'should return the same string if n > string.length', ->
stringLength = STRING.numbers.length
result = helpers.formatLongString(STRING.numbers, stringLength + 1)
expect(result).to.equal(STRING.numbers)
string = '1234567812345678'
result = helpers.formatLongString(string, string.length + 1)
expect(result).to.equal(string)
describe '#isDeviceUUIDValid()', ->

View File

@ -8,14 +8,10 @@ dataPrefix = require('./data-prefix')
settings = require('../settings')
mock = require('../../../tests/utils/mock')
PREFIXES =
main: settings.get('dataPrefix')
new: "#{settings.get('dataPrefix')}-new"
invalid: { path: '/abc' }
describe 'DataPrefix:', ->
beforeEach ->
@prefix = settings.get('dataPrefix')
mock.fs.init()
afterEach ->
@ -34,17 +30,17 @@ describe 'DataPrefix:', ->
describe '#set()', ->
beforeEach (done) ->
rimraf(PREFIXES.main, done)
rimraf(@prefix, done)
it 'should be able to set a prefix', (done) ->
expect(dataPrefix.get()).to.not.exist
dataPrefix.set PREFIXES.main, (error) ->
dataPrefix.set @prefix, (error) =>
expect(error).to.not.exist
expect(dataPrefix.get()).to.equal(PREFIXES.main)
expect(dataPrefix.get()).to.equal(@prefix)
done()
it 'should throw an error if passing an invalid path', (done) ->
dataPrefix.set PREFIXES.invalid, (error) ->
dataPrefix.set { path: '/abc' }, (error) ->
expect(error).to.be.an.instanceof(Error)
done()
@ -53,15 +49,15 @@ describe 'DataPrefix:', ->
async.waterfall [
(callback) ->
fs.exists PREFIXES.main, (exists) ->
fs.exists @prefix, (exists) ->
return callback(null, exists)
(exists, callback) ->
(exists, callback) =>
expect(exists).to.be.false
dataPrefix.set(PREFIXES.main, callback)
dataPrefix.set(@prefix, callback)
(callback) ->
fsUtils.isDirectory(PREFIXES.main, callback)
(callback) =>
fsUtils.isDirectory(@prefix, callback)
(isDirectory, callback) ->
expect(isDirectory).to.be.true
@ -74,25 +70,26 @@ describe 'DataPrefix:', ->
describe 'given a prefix', ->
beforeEach (done) ->
dataPrefix.set(PREFIXES.main, done)
dataPrefix.set(@prefix, done)
describe '#get()', ->
it 'should return the saved prefix', ->
expect(dataPrefix.get()).to.equal(PREFIXES.main)
expect(dataPrefix.get()).to.equal(@prefix)
describe '#set()', ->
it 'should be able to override the prefix', (done) ->
expect(dataPrefix.get()).to.equal(PREFIXES.main)
dataPrefix.set PREFIXES.new, (error) ->
newPrefix = "#{settings.get('dataPrefix')}-new"
expect(dataPrefix.get()).to.equal(@prefix)
dataPrefix.set newPrefix, (error) ->
expect(error).to.not.exist
expect(dataPrefix.get()).to.equal(PREFIXES.new)
expect(dataPrefix.get()).to.equal(newPrefix)
done()
describe '#clear()', ->
it 'should clear the prefix', ->
expect(dataPrefix.get()).to.equal(PREFIXES.main)
expect(dataPrefix.get()).to.equal(@prefix)
dataPrefix.clear()
expect(dataPrefix.get()).to.not.exist

View File

@ -6,28 +6,6 @@ async = require('async')
settings = require('../settings')
data = require('./data')
FILES_FIXTURES =
hello:
filename: 'hello_world.test'
contents: 'Hello World!'
nested:
filename: 'nested/hello_world.test'
contents: 'Nested Hello World!'
FILESYSTEM =
text:
name: "#{settings.get('dataPrefix')}/text"
contents: 'Hello World'
key: 'text'
directory:
name: "#{settings.get('dataPrefix')}/directory"
contents: {}
key: 'directory'
nested:
name: "#{settings.get('dataPrefix')}/nested/text"
contents: 'Nested Hello World'
key: 'nested/text'
describe 'Data:', ->
describe 'given no prefix', ->
@ -58,6 +36,20 @@ describe 'Data:', ->
describe 'given a prefix', ->
FILESYSTEM =
text:
name: "#{settings.get('dataPrefix')}/text"
contents: 'Hello World'
key: 'text'
directory:
name: "#{settings.get('dataPrefix')}/directory"
contents: {}
key: 'directory'
nested:
name: "#{settings.get('dataPrefix')}/nested/text"
contents: 'Nested Hello World'
key: 'nested/text'
beforeEach (done) ->
mock.fs.init(FILESYSTEM)
data.prefix.set(settings.get('dataPrefix'), done)
@ -144,6 +136,14 @@ describe 'Data:', ->
expect(error).to.not.exist
done()
FILES_FIXTURES =
hello:
filename: 'hello_world.test'
contents: 'Hello World!'
nested:
filename: 'nested/hello_world.test'
contents: 'Nested Hello World!'
it('should be able to write a file', writeAndCheckFixture(FILES_FIXTURES.hello))
it('should be able to write a nested file', writeAndCheckFixture(FILES_FIXTURES.nested))

View File

@ -4,14 +4,6 @@ fsUtils = require('./fs-utils')
settings = require('../../settings')
data = require('../../data/data')
FILESYSTEM =
text:
name: '/tmp/text'
contents: 'Hello World'
directory:
name: '/tmp/directory'
contents: {}
describe 'FsUtils:', ->
describe '#isValidPath()', ->
@ -39,6 +31,14 @@ describe 'FsUtils:', ->
describe '#isDirectory()', ->
FILESYSTEM =
text:
name: '/tmp/text'
contents: 'Hello World'
directory:
name: '/tmp/directory'
contents: {}
beforeEach (done) ->
mock.fs.init(FILESYSTEM)
data.prefix.set(settings.get('dataPrefix'), done)

View File

@ -3,18 +3,16 @@ sinon = require('sinon')
log = require('../log/log')
errors = require('./errors')
MESSAGES =
helloWorld: 'Hello World'
describe 'Errors:', ->
describe '#handle()', ->
it 'should log the error message to stderr', ->
message = 'Hello World'
logErrorStub = sinon.stub(log, 'error')
error = new Error(MESSAGES.helloWorld)
error = new Error(message)
errors.handle(error, false)
expect(logErrorStub).to.have.been.calledWith(MESSAGES.helloWorld)
expect(logErrorStub).to.have.been.calledWith(message)
logErrorStub.restore()
it 'should do nothing if error is not an instance of Error', ->
@ -41,12 +39,12 @@ describe 'Errors:', ->
logErrorStub.restore()
it 'should exit if the last parameter is true', ->
error = new Error(MESSAGES.helloWorld)
error = new Error()
checkProcessExitOption error, true, (processExitStub) ->
expect(processExitStub).to.have.been.calledWith(1)
it 'should not exit if the last parameter is false', ->
error = new Error(MESSAGES.helloWorld)
error = new Error()
checkProcessExitOption error, false, (processExitStub) ->
expect(processExitStub).to.not.have.been.called

View File

@ -5,14 +5,6 @@ expect = chai.expect
sinon = require('sinon')
log = require('./log')
MESSAGE =
foobar: 'Foo Bar'
# Very handy to check that the real console functions
# were called without printing anything, and preventing
# us from having to mock console, which is used by Mocha
empty: ''
describe 'Log:', ->
testConsoleFunction = (logName, consoleName, message, assertions) ->
@ -38,22 +30,22 @@ describe 'Log:', ->
describe '#error()', ->
it 'should output to console.error', ->
testConsoleFunctionBeingCalled('error', 'error', MESSAGE.empty)
testConsoleFunctionBeingCalled('error', 'error', '')
describe '#warning()', ->
it 'should output to console.warn', ->
testConsoleFunctionBeingCalled('warning', 'warn', MESSAGE.empty)
testConsoleFunctionBeingCalled('warning', 'warn', '')
describe '#info()', ->
it 'should output to console.info', ->
testConsoleFunctionBeingCalled('info', 'info', MESSAGE.empty)
testConsoleFunctionBeingCalled('info', 'info', '')
describe '#out()', ->
it 'should output to console.log', ->
testConsoleFunctionBeingCalled('out', 'log', MESSAGE.empty)
testConsoleFunctionBeingCalled('out', 'log', '')
describe '#array()', ->
@ -123,19 +115,19 @@ describe 'Log:', ->
describe '#error()', ->
it 'should still output to console.error', ->
testConsoleFunctionBeingCalled('error', 'error', MESSAGE.empty)
testConsoleFunctionBeingCalled('error', 'error', '')
describe '#warning()', ->
it 'should still output to console.warn', ->
testConsoleFunctionBeingCalled('warning', 'warn', MESSAGE.empty)
testConsoleFunctionBeingCalled('warning', 'warn', '')
describe '#info()', ->
it 'should not call console.info', ->
testConsoleFunctionNotBeingCalled('info', 'info', MESSAGE.empty)
testConsoleFunctionNotBeingCalled('info', 'info', '')
describe '#out()', ->
it 'should not call console.log', ->
testConsoleFunctionBeingCalled('out', 'log', MESSAGE.empty)
testConsoleFunctionBeingCalled('out', 'log', '')

View File

@ -11,20 +11,25 @@ mock = require('../../../tests/utils/mock')
canvas = require('./_canvas')
settings = require('../settings')
URI =
application: url.resolve(settings.get('apiPrefix'), 'application')
RESPONSE =
applications:
d: [
{ id: 1 }
{ id: 2 }
]
describe 'Canvas:', ->
URI =
application: url.resolve(settings.get('apiPrefix'), 'application')
RESPONSE =
applications:
d: [
{ id: 1 }
{ id: 2 }
]
beforeEach (done) ->
mock.fs.init()
nock(settings.get('remoteUrl'))
.get(URI.application)
.reply(200, RESPONSE.applications)
data.prefix.set(settings.get('dataPrefix'), done)
afterEach ->
@ -36,11 +41,6 @@ describe 'Canvas:', ->
after ->
mock.connection.restore()
beforeEach ->
nock(settings.get('remoteUrl'))
.get(URI.application)
.reply(200, RESPONSE.applications)
it 'should construct the correct url', ->
promise = canvas.get
resource: 'application'

View File

@ -1,23 +1,9 @@
_ = require('lodash')
chai = require('chai')
chai.use(require('chai-string'))
expect = chai.expect
os = require('./os')
APPS =
validEthernet:
id: 91
params:
network: 'ethernet'
invalidNetworkType:
id: 91
params:
network: 'foobar'
validWifi:
id: 91
params:
network: 'wifi'
wifiSsid: 'MYSSID'
describe 'OS:', ->
describe '#generateCacheName()', ->
@ -25,12 +11,21 @@ describe 'OS:', ->
describe 'given network is ethernet', ->
it 'should construct a correct name', ->
application = APPS.validEthernet
application =
id: 91
params:
network: 'ethernet'
result = os.generateCacheName(application.id, application.params)
expect(result).to.equal("#{application.id}-ethernet-#{Date.now()}")
expect(result).to.match(new RegExp("#{application.id}-ethernet-\\d\+\$"))
describe 'given network is wifi', ->
it 'should construct a correct name', ->
application = APPS.validWifi
application =
id: 91
params:
network: 'wifi'
wifiSsid: 'MYSSID'
result = os.generateCacheName(application.id, application.params)
expect(result).to.equal("#{application.id}-wifi-#{application.params.wifiSsid}-#{Date.now()}")
expect(result).to.match(new RegExp("#{application.id}-wifi-#{application.params.wifiSsid}-\\d\+\$"))

View File

@ -10,20 +10,6 @@ data = require('../data/data')
mock = require('../../../tests/utils/mock')
johnDoeFixture = require('../../../tests/fixtures/johndoe.json')
TEST_URI = settings.get('remoteUrl')
URI =
ok: '/ok'
nojson: '/nojson'
error: '/error'
RESPONSE =
nojson: 'NO JSON RESPONSE'
STATUS =
ok: 'ok'
error: 'error'
METHODS = [
'GET'
'HEAD'
@ -42,12 +28,25 @@ describe 'Server:', ->
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)
@uris =
ok: '/ok'
nojson: '/nojson'
error: '/error'
@responses =
nojson: 'NO JSON @responses'
@status =
ok: 'ok'
error: 'error'
testUri = settings.get('remoteUrl')
nock(testUri).get(@uris.nojson).reply(200, @responses.nojson)
nock(testUri).get(@uris.error).reply(400, status: @status.error)
for method in METHODS
lowercaseMethod = method.toLowerCase()
nock(TEST_URI)[lowercaseMethod](URI.ok).reply(200, status: STATUS.ok)
nock(testUri)[lowercaseMethod](@uris.ok).reply(200, status: @status.ok)
mock.fs.init()
data.prefix.set(settings.get('dataPrefix'), done)
@ -60,16 +59,16 @@ describe 'Server:', ->
it 'should make a real HTTP request', (done) ->
server.request {
method: 'GET'
url: URI.ok
}, (error, response) ->
url: @uris.ok
}, (error, response) =>
return done(error) if error?
expect(response.body.status).to.equal(STATUS.ok)
expect(response.body.status).to.equal(@status.ok)
expect(response.statusCode).to.equal(200)
done()
it 'should make a GET request if method is omitted', (done) ->
server.request {
url: URI.ok
url: @uris.ok
}, (error, response) ->
return done(error) if error?
expect(response.request.method).to.equal('GET')
@ -79,7 +78,7 @@ describe 'Server:', ->
return (done) ->
server.request {
method: type
url: URI.ok
url: @uris.ok
}, (error, response) ->
return done(error) if error?
expect(response.request.method).to.equal(type)
@ -91,16 +90,16 @@ describe 'Server:', ->
it 'should get a raw response of response is not JSON', (done) ->
server.request {
method: 'GET'
url: URI.nojson
}, (error, response) ->
url: @uris.nojson
}, (error, response) =>
return done(error) if error?
expect(response.body).to.equal(RESPONSE.nojson)
expect(response.body).to.equal(@responses.nojson)
done()
it 'should parse the body', (done) ->
server.request {
method: 'GET'
url: URI.ok
url: @uris.ok
}, (error, response, body) ->
expect(error).to.not.exist
expect(body).to.be.an.object
@ -112,7 +111,7 @@ describe 'Server:', ->
server.request {
method: 'POST'
url: URI.ok
url: @uris.ok
json: body
}, (error, response) ->
return done(error) if error?
@ -122,7 +121,7 @@ describe 'Server:', ->
it 'should throw an error if method is unknown', (done) ->
server.request {
method: 'FOO'
url: URI.ok
url: @uris.ok
}, (error, response) ->
expect(error).to.exist
expect(error).to.be.an.instanceof(Error)
@ -131,7 +130,7 @@ describe 'Server:', ->
it 'should throw an error if the status code is >= 400', (done) ->
server.request {
method: 'GET'
url: URI.error
url: @uris.error
}, (error, response) ->
expect(error).to.exist
expect(error).to.be.an.instanceof(Error)
@ -140,10 +139,10 @@ describe 'Server:', ->
it 'should accept a full url', (done) ->
server.request {
method: 'GET'
url: url.resolve(settings.get('remoteUrl'), URI.ok)
}, (error, response) ->
url: url.resolve(settings.get('remoteUrl'), @uris.ok)
}, (error, response) =>
expect(error).to.not.exist
expect(response.body.status).to.equal(STATUS.ok)
expect(response.body.status).to.equal(@status.ok)
done()
it 'should allow piping files', (done) ->
@ -152,22 +151,22 @@ describe 'Server:', ->
server.request {
method: 'GET'
url: URI.nojson
url: @uris.nojson
pipe: fs.createWriteStream(outputFile)
}, (error) ->
}, (error) =>
expect(error).to.not.exist
expect(onProgressSpy).to.have.been.called
fs.readFile outputFile, { encoding: 'utf8' }, (error, contents) ->
fs.readFile outputFile, { encoding: 'utf8' }, (error, contents) =>
expect(error).to.not.exist
expect(contents).to.equal(RESPONSE.nojson)
expect(contents).to.equal(@responses.nojson)
done()
, onProgressSpy
checkRequestTypeWithoutBody = (type) ->
return (done) ->
lowercaseType = type.toLowerCase()
server[lowercaseType] URI.ok, (error, response) ->
server[lowercaseType] @uris.ok, (error, response) ->
return done(error) if error?
expect(response.request.method).to.equal(type)
done()
@ -184,7 +183,7 @@ describe 'Server:', ->
checkRequestTypeWithBody = (type, body) ->
return (done) ->
lowercaseType = type.toLowerCase()
server[lowercaseType] URI.ok, body, (error, response) ->
server[lowercaseType] @uris.ok, body, (error, response) ->
return done(error) if error?
expect(response.request.method).to.equal(type)
done()
@ -209,7 +208,7 @@ describe 'Server:', ->
server.request {
method: 'GET'
url: URI.ok
url: @uris.ok
}, (error, response) ->
authorizationHeader = response?.request.headers.Authorization
@ -228,7 +227,7 @@ describe 'Server:', ->
it 'should not send the Authorization header', (done) ->
server.request {
method: 'GET'
url: URI.ok
url: @uris.ok
}, (error, response) ->
expect(error).to.not.exist
authorizationHeader = response?.request.headers.Authorization