Files
shields/lib/validate.spec.js
Paul Melnikow 282520041d Add [GitlabPipeline] badge (#2325)
There's a lot of demand for the Gitlab badges (#541) and the PR has been lingering, so I thought I'd start off one of the simple ones as a new-style service. This one is SVG-based, so it shouldn't require the API-token logic which could use some more testing and will require us to create an app and configure it on our server.

We don't have any validation in place for `queryParams`. Probably this should be added to BaseService, though for the time being I extracted a helper function.

Thanks to @LVMBDV for getting this work started in #1838!
2018-11-18 10:06:47 -05:00

88 lines
2.4 KiB
JavaScript

'use strict'
const Joi = require('joi')
const { expect } = require('chai')
const sinon = require('sinon')
const trace = require('../services/trace')
const { InvalidParameter } = require('../services/errors')
const validate = require('./validate')
describe('validate', function() {
const schema = Joi.object({
requiredString: Joi.string().required(),
}).required()
let sandbox
beforeEach(function() {
sandbox = sinon.createSandbox()
})
afterEach(function() {
sandbox.restore()
})
beforeEach(function() {
sandbox.stub(trace, 'logTrace')
})
const ErrorClass = InvalidParameter
const prettyErrorMessage = 'parameter does not match schema'
const traceErrorMessage = 'Params did not match schema'
const traceSuccessMessage = 'Params after validation'
const options = {
ErrorClass,
prettyErrorMessage,
traceErrorMessage,
traceSuccessMessage,
}
context('schema is not provided', function() {
it('throws the expected programmer error', function() {
try {
validate(options, { requiredString: 'bar' }, undefined)
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(Error)
expect(e.message).to.equal('A Joi schema is required')
}
})
})
context('data matches schema', function() {
it('logs the data', function() {
validate(options, { requiredString: 'bar' }, schema)
expect(trace.logTrace).to.be.calledWithMatch(
'validate',
sinon.match.string,
traceSuccessMessage,
{ requiredString: 'bar' },
{ deep: true }
)
})
})
context('data does not match schema', function() {
it('logs the data and throws the expected error', async function() {
try {
validate(
options,
{ requiredString: ['this', "shouldn't", 'work'] },
schema
)
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(InvalidParameter)
expect(e.message).to.equal(
'Invalid Parameter: child "requiredString" fails because ["requiredString" must be a string]'
)
expect(e.prettyMessage).to.equal(prettyErrorMessage)
}
expect(trace.logTrace).to.be.calledWithMatch(
'validate',
sinon.match.string,
traceErrorMessage,
'child "requiredString" fails because ["requiredString" must be a string]'
)
})
})
})