All files / server/api/utils validation.spec.js

100% Statements 39/39
100% Branches 0/0
77.27% Functions 17/22
100% Lines 33/33
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 541x 1x   1x   1x   1x 1x     1x 1x 1x 1x 1x   1x 1x 1x 1x     1x     1x 1x 1x 1x 1x   1x 1x 1x     1x 1x 1x 1x 1x     1x 1x     1x 1x        
const { expect } = require('../../spec/utils');
const utils = require('./validation');
 
describe('Validation Utils', () => {
 
  const validateValue = utils.validateValue;
 
  describe('validateValue', () => {
    it('should set the status of the validation error to a default value', async () => {
 
      let validationError;
      await validateValue('foo', 422, () => {
        let error = new Error('validation error');
        error.errors = [ { message: 'bug' } ];
        throw error;
      }).catch(err => validationError = err);
 
      expect(validationError).to.be.an.instanceof(Error);
      expect(validationError.message).to.equal('validation error');
      expect(validationError.errors).to.eql([ { message: 'bug' } ]);
      expect(validationError.status).to.equal(422);
    });
 
    it('should not change the status of a non-validation error', async () => {
 
      let nonValidationError;
      await validateValue('foo', 422, () => {
        let error = new Error('authorization error');
        error.status = 403;
        throw error;
      }).catch(err => nonValidationError = err);
 
      expect(nonValidationError).to.be.an.instanceof(Error);
      expect(nonValidationError.message).to.equal('authorization error');
      expect(nonValidationError.status).to.equal(403);
    });
 
    it('should not accept an invalid status code', () => {
      expect(() => validateValue('foo', undefined, () => {})).to.throw('Status must be an HTTP status code between 100 and 599, got undefined');
      expect(() => validateValue('foo', () => {})).to.throw('Status must be an HTTP status code between 100 and 599, got function');
      expect(() => validateValue('foo', -200, () => {})).to.throw('Status must be an HTTP status code between 100 and 599, got -200');
      expect(() => validateValue('foo', 1000, () => {})).to.throw('Status must be an HTTP status code between 100 and 599, got 1000');
    });
 
    it('should require at least one callback', () => {
      expect(() => validateValue('foo', 422)).to.throw('At least one callback is required');
    })
 
    it('should require all callbacks to be functions', () => {
      expect(() => validateValue('foo', 422, () => {}, 'bar')).to.throw('Additional arguments must be functions');
    });
  });
});