Remove mochainon dependency and replace with direct testing dependencies

Change-type: patch
Signed-off-by: Lucian <lucian.buzzo@gmail.com>
This commit is contained in:
Lucian 2019-08-07 12:31:03 +01:00
parent 35110e0610
commit 15dfdc2229
5 changed files with 802 additions and 225 deletions

928
npm-shrinkwrap.json generated

File diff suppressed because it is too large Load Diff

View File

@ -102,6 +102,8 @@
"@types/tar-stream": "1.6.0",
"@types/through2": "2.0.33",
"catch-uncommitted": "^1.3.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"ent": "^2.2.0",
"filehound": "^1.17.0",
"fs-extra": "^8.0.1",
@ -110,13 +112,14 @@
"gulp-inline-source": "^2.1.0",
"gulp-mocha": "^2.0.0",
"gulp-shell": "^0.5.2",
"mochainon": "^2.0.0",
"mocha": "^6.2.0",
"pkg": "^4.4.0",
"prettier": "1.17.0",
"publish-release": "^1.6.0",
"resin-lint": "^3.0.1",
"rewire": "^3.0.2",
"shell-escape": "^0.2.0",
"sinon": "^7.4.1",
"ts-node": "^8.1.0",
"typescript": "3.4.3"
},

View File

@ -1,5 +1,7 @@
m = require('mochainon')
chai = require('chai')
chaiAsPromised = require('chai-as-promised')
request = require('request')
sinon = require('sinon')
Promise = require('bluebird')
path = require('path')
fs = require('fs')
@ -8,6 +10,8 @@ server = require('../../build/auth/server')
utils = require('../../build/auth/utils')
tokens = require('./tokens.json')
chai.use(chaiAsPromised)
options =
port: 3000
path: '/auth'
@ -24,34 +28,34 @@ describe 'Server:', ->
it 'should get 404 if posting to an unknown path', (done) ->
promise = server.awaitForToken(options)
m.chai.expect(promise).to.be.rejectedWith('Unknown path or verb')
chai.expect(promise).to.be.rejectedWith('Unknown path or verb')
request.post "http://localhost:#{options.port}/foobarbaz",
form:
token: tokens.johndoe.token
, (error, response, body) ->
m.chai.expect(error).to.not.exist
m.chai.expect(response.statusCode).to.equal(404)
m.chai.expect(body).to.equal('Not found')
chai.expect(error).to.not.exist
chai.expect(response.statusCode).to.equal(404)
chai.expect(body).to.equal('Not found')
done()
it 'should get 404 if not using the correct verb', (done) ->
promise = server.awaitForToken(options)
m.chai.expect(promise).to.be.rejectedWith('Unknown path or verb')
chai.expect(promise).to.be.rejectedWith('Unknown path or verb')
request.get "http://localhost:#{options.port}#{options.path}",
form:
token: tokens.johndoe.token
, (error, response, body) ->
m.chai.expect(error).to.not.exist
m.chai.expect(response.statusCode).to.equal(404)
m.chai.expect(body).to.equal('Not found')
chai.expect(error).to.not.exist
chai.expect(response.statusCode).to.equal(404)
chai.expect(body).to.equal('Not found')
done()
describe 'given the token authenticates with the server', ->
beforeEach ->
@loginIfTokenValidStub = m.sinon.stub(utils, 'loginIfTokenValid')
@loginIfTokenValidStub = sinon.stub(utils, 'loginIfTokenValid')
@loginIfTokenValidStub.returns(Promise.resolve(true))
afterEach ->
@ -59,22 +63,22 @@ describe 'Server:', ->
it 'should eventually be the token', (done) ->
promise = server.awaitForToken(options)
m.chai.expect(promise).to.eventually.equal(tokens.johndoe.token)
chai.expect(promise).to.eventually.equal(tokens.johndoe.token)
request.post "http://localhost:#{options.port}#{options.path}",
form:
token: tokens.johndoe.token
, (error, response, body) ->
m.chai.expect(error).to.not.exist
m.chai.expect(response.statusCode).to.equal(200)
chai.expect(error).to.not.exist
chai.expect(response.statusCode).to.equal(200)
getPage('success').then (expectedBody) ->
m.chai.expect(body).to.equal(expectedBody)
chai.expect(body).to.equal(expectedBody)
done()
describe 'given the token does not authenticate with the server', ->
beforeEach ->
@loginIfTokenValidStub = m.sinon.stub(utils, 'loginIfTokenValid')
@loginIfTokenValidStub = sinon.stub(utils, 'loginIfTokenValid')
@loginIfTokenValidStub.returns(Promise.resolve(false))
afterEach ->
@ -82,43 +86,43 @@ describe 'Server:', ->
it 'should be rejected', (done) ->
promise = server.awaitForToken(options)
m.chai.expect(promise).to.be.rejectedWith('Invalid token')
chai.expect(promise).to.be.rejectedWith('Invalid token')
request.post "http://localhost:#{options.port}#{options.path}",
form:
token: tokens.johndoe.token
, (error, response, body) ->
m.chai.expect(error).to.not.exist
m.chai.expect(response.statusCode).to.equal(401)
chai.expect(error).to.not.exist
chai.expect(response.statusCode).to.equal(401)
getPage('error').then (expectedBody) ->
m.chai.expect(body).to.equal(expectedBody)
chai.expect(body).to.equal(expectedBody)
done()
it 'should be rejected if no token', (done) ->
promise = server.awaitForToken(options)
m.chai.expect(promise).to.be.rejectedWith('No token')
chai.expect(promise).to.be.rejectedWith('No token')
request.post "http://localhost:#{options.port}#{options.path}",
form:
token: ''
, (error, response, body) ->
m.chai.expect(error).to.not.exist
m.chai.expect(response.statusCode).to.equal(401)
chai.expect(error).to.not.exist
chai.expect(response.statusCode).to.equal(401)
getPage('error').then (expectedBody) ->
m.chai.expect(body).to.equal(expectedBody)
chai.expect(body).to.equal(expectedBody)
done()
it 'should be rejected if token is malformed', (done) ->
promise = server.awaitForToken(options)
m.chai.expect(promise).to.be.rejectedWith('Invalid token')
chai.expect(promise).to.be.rejectedWith('Invalid token')
request.post "http://localhost:#{options.port}#{options.path}",
form:
token: 'asdf'
, (error, response, body) ->
m.chai.expect(error).to.not.exist
m.chai.expect(response.statusCode).to.equal(401)
chai.expect(error).to.not.exist
chai.expect(response.statusCode).to.equal(401)
getPage('error').then (expectedBody) ->
m.chai.expect(body).to.equal(expectedBody)
chai.expect(body).to.equal(expectedBody)
done()

View File

@ -1,4 +1,5 @@
m = require('mochainon')
chai = require('chai')
sinon = require('sinon')
url = require('url')
Promise = require('bluebird')
@ -14,7 +15,7 @@ describe 'Utils:', ->
it 'should eventually be a valid url', ->
utils.getDashboardLoginURL('https://127.0.0.1:3000/callback').then (loginUrl) ->
m.chai.expect ->
chai.expect ->
url.parse(loginUrl)
.to.not.throw(Error)
@ -25,7 +26,7 @@ describe 'Utils:', ->
loginUrl: utils.getDashboardLoginURL('https://127.0.0.1:3000/callback')
.then ({ dashboardUrl, loginUrl }) ->
protocol = url.parse(loginUrl).protocol
m.chai.expect(protocol).to.equal(url.parse(dashboardUrl).protocol)
chai.expect(protocol).to.equal(url.parse(dashboardUrl).protocol)
it 'should correctly escape a callback url without a path', ->
Promise.props
@ -33,7 +34,7 @@ describe 'Utils:', ->
loginUrl: utils.getDashboardLoginURL('http://127.0.0.1:3000')
.then ({ dashboardUrl, loginUrl }) ->
expectedUrl = "#{dashboardUrl}/login/cli/http%253A%252F%252F127.0.0.1%253A3000"
m.chai.expect(loginUrl).to.equal(expectedUrl)
chai.expect(loginUrl).to.equal(expectedUrl)
it 'should correctly escape a callback url with a path', ->
Promise.props
@ -41,30 +42,30 @@ describe 'Utils:', ->
loginUrl: utils.getDashboardLoginURL('http://127.0.0.1:3000/callback')
.then ({ dashboardUrl, loginUrl }) ->
expectedUrl = "#{dashboardUrl}/login/cli/http%253A%252F%252F127.0.0.1%253A3000%252Fcallback"
m.chai.expect(loginUrl).to.equal(expectedUrl)
chai.expect(loginUrl).to.equal(expectedUrl)
describe '.loginIfTokenValid()', ->
it 'should eventually be false if token is undefined', ->
promise = utils.loginIfTokenValid(undefined)
m.chai.expect(promise).to.eventually.be.false
chai.expect(promise).to.eventually.be.false
it 'should eventually be false if token is null', ->
promise = utils.loginIfTokenValid(null)
m.chai.expect(promise).to.eventually.be.false
chai.expect(promise).to.eventually.be.false
it 'should eventually be false if token is an empty string', ->
promise = utils.loginIfTokenValid('')
m.chai.expect(promise).to.eventually.be.false
chai.expect(promise).to.eventually.be.false
it 'should eventually be false if token is a string containing only spaces', ->
promise = utils.loginIfTokenValid(' ')
m.chai.expect(promise).to.eventually.be.false
chai.expect(promise).to.eventually.be.false
describe 'given the token does not authenticate with the server', ->
beforeEach ->
@balenaAuthIsLoggedInStub = m.sinon.stub(balena.auth, 'isLoggedIn')
@balenaAuthIsLoggedInStub = sinon.stub(balena.auth, 'isLoggedIn')
@balenaAuthIsLoggedInStub.returns(Promise.resolve(false))
afterEach ->
@ -72,7 +73,7 @@ describe 'Utils:', ->
it 'should eventually be false', ->
promise = utils.loginIfTokenValid(tokens.johndoe.token)
m.chai.expect(promise).to.eventually.be.false
chai.expect(promise).to.eventually.be.false
describe 'given there was a token already', ->
@ -81,10 +82,10 @@ describe 'Utils:', ->
it 'should preserve the old token', ->
balena.auth.getToken().then (originalToken) ->
m.chai.expect(originalToken).to.equal(tokens.janedoe.token)
chai.expect(originalToken).to.equal(tokens.janedoe.token)
return utils.loginIfTokenValid(tokens.johndoe.token)
.then(balena.auth.getToken).then (currentToken) ->
m.chai.expect(currentToken).to.equal(tokens.janedoe.token)
chai.expect(currentToken).to.equal(tokens.janedoe.token)
describe 'given there was no token', ->
@ -95,12 +96,12 @@ describe 'Utils:', ->
utils.loginIfTokenValid(tokens.johndoe.token).then ->
balena.auth.isLoggedIn()
.then (isLoggedIn) ->
m.chai.expect(isLoggedIn).to.equal(false)
chai.expect(isLoggedIn).to.equal(false)
describe 'given the token does authenticate with the server', ->
beforeEach ->
@balenaAuthIsLoggedInStub = m.sinon.stub(balena.auth, 'isLoggedIn')
@balenaAuthIsLoggedInStub = sinon.stub(balena.auth, 'isLoggedIn')
@balenaAuthIsLoggedInStub.returns(Promise.resolve(true))
afterEach ->
@ -108,4 +109,4 @@ describe 'Utils:', ->
it 'should eventually be true', ->
promise = utils.loginIfTokenValid(tokens.johndoe.token)
m.chai.expect(promise).to.eventually.be.true
chai.expect(promise).to.eventually.be.true

View File

@ -1,4 +1,3 @@
require 'mocha'
chai = require 'chai'
_ = require 'lodash'
path = require('path')