All files / server/api/validators geojson.spec.js

100% Statements 88/88
100% Branches 0/0
100% Functions 31/31
100% Lines 87/87
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 3551x   1x 1x 1x   1x   1x   1x   1x 1x     1x 1x     1x 1x 1x     1x 1x 1x                       1x 1x 1x                         1x 1x 1x                       1x                   8x   8x 8x   8x 8x 8x                             12x 12x       12x 11x   12x       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   1x                         1x   1x         1x   1x                       1x   1x         1x   1x                         1x   1x         1x   1x                             10x 10x 9x   10x           20x 21x 20x    
const _ = require('lodash');
 
const { expect } = require('../../spec/utils');
const { dsl } = require('../utils/validation');
const geoJsonValidators = require('./geojson');
 
describe('GeoJSON validators', function() {
 
  describe('bboxString', function() {
 
    const validateBboxString = geoJsonValidators.bboxString;
 
    it('should be a function', function() {
      expect(validateBboxString).to.be.a('function');
    });
 
    it('should create a validator function', function() {
      expect(validateBboxString()).to.be.a('function');
    });
 
    it('should add no error for a valid bounding box', async function() {
      const err = await validate('10,20,30,40');
      expect(err).to.equal(undefined);
    });
 
    it('should add an error if the bounding box is not a string', async function() {
      const err = await validate(10203040);
      expectErrors(err, [
        {
          cause: 'wrongType',
          location: 'bbox',
          message: 'must be a string',
          validator: 'bboxString',
          value: 10203040,
          valueSet: true
        }
      ]);
    });
 
    it('should add an error if the bounding box has the wrong number of coordinates', async function() {
      const err = await validate('10,20,30');
      expectErrors(err, [
        {
          cause: 'wrongLength',
          actualLength: 3,
          location: 'bbox',
          message: 'must have 4 comma-separated coordinates; got 3',
          validator: 'bboxString',
          value: '10,20,30',
          valueSet: true
        }
      ]);
    });
 
    it('should add an error if one of the coordinates is not a number', async function() {
      const err = await validate('10,asd,30,40');
      expectErrors(err, [
        {
          location: 'bbox[1]',
          message: 'must be a number between -90 and 90',
          validator: 'latitude',
          value: NaN,
          valueSet: true
        }
      ]);
    });
 
    // Generate 8 tests to check the longitude/latitude bounds.
    [
      { index: 0, value: -190, problem: 'the longitude of the south-west corner is too small', message: 'must be a number between -180 and 180', validator: 'longitude' },
      { index: 0, value: 213.4, problem: 'the longitude of the south-west corner is too large', message: 'must be a number between -180 and 180', validator: 'longitude' },
      { index: 1, value: -91, problem: 'the latitude of the south-west corner is too small', message: 'must be a number between -90 and 90', validator: 'latitude' },
      { index: 1, value: 90.001, problem: 'the latitude of the south-west corner is too large', message: 'must be a number between -90 and 90', validator: 'latitude' },
      { index: 2, value: -180.5, problem: 'the longitude of the north-east corner is too small', message: 'must be a number between -180 and 180', validator: 'longitude' },
      { index: 2, value: 181, problem: 'the longitude of the north-east corner is too large', message: 'must be a number between -180 and 180', validator: 'longitude' },
      { index: 3, value: -180, problem: 'the latitude of the north-east corner is too small', message: 'must be a number between -90 and 90', validator: 'latitude' },
      { index: 3, value: 1000, problem: 'the latitude of the north-east corner is too large', message: 'must be a number between -90 and 90', validator: 'latitude' }
    ].forEach(config => {
      it(`should add an error if ${config.problem}`, async function() {
 
        const coordinates = [ 10, 20, 30, 40 ];
        coordinates[config.index] = config.value;
 
        const err = await validate(coordinates.join(','));
        expect(err).not.to.equal(undefined);
        expectErrors(err, [
          {
            location: `bbox[${config.index}]`,
            message: config.message,
            validator: config.validator,
            value: config.value,
            valueSet: true
          }
        ]);
      });
    })
 
    async function validate(value) {
 
      let validationError;
      await dsl(function(ctx) {
        ctx.set({
          location: 'bbox'
        });
 
        return ctx.validate(ctx.value(value), ctx.while(ctx.noError(ctx.atCurrentLocation())), validateBboxString());
      }).catch(err => validationError = err);
 
      return validationError;
    }
  });
 
  describe('point', function() {
 
    const validatePoint = geoJsonValidators.point;
 
    it('should be a function', function() {
      expect(validatePoint).to.be.a('function');
    });
 
    it('should create a validator function', function() {
      expect(validatePoint()).to.be.a('function');
    });
 
    it('should add no error for a valid point', async function() {
 
      const err = await validate({
        type: 'Point',
        coordinates: [ 24, 42 ]
      });
 
      expect(err).to.equal(undefined);
    });
 
    it('should add an error if the point is missing properties', async function() {
 
      const point = {
        type: 'Point'
      };
 
      const err = await validate(point);
 
      expectErrors(err, [
        {
          cause: 'missingProperties',
          expectedProperties: [ 'type', 'coordinates' ],
          missingProperties: [ 'coordinates' ],
          message: 'must have properties "type", "coordinates"',
          validator: 'properties',
          value: point,
          valueSet: true
        },
        {
          message: 'is required',
          type: 'json',
          location: '/coordinates',
          validator: 'required',
          value: undefined,
          valueSet: false
        }
      ]);
    });
 
    it('should add an error if the point has extra properties', async function() {
 
      const point = {
        type: 'Point',
        coordinates: [ 24, 42 ],
        foo: 'bar',
        baz: 'qux'
      };
 
      const err = await validate(point);
 
      expectErrors(err, [
        {
          cause: 'extraProperties',
          expectedProperties: [ 'type', 'coordinates' ],
          extraProperties: [ 'foo', 'baz' ],
          message: 'must not have other properties than "type", "coordinates"',
          validator: 'properties',
          value: point,
          valueSet: true
        }
      ]);
    });
 
    it('should add an error if the coordinates are not an array', async function() {
 
      const point = {
        type: 'Point',
        coordinates: 'foo'
      };
 
      const err = await validate(point);
 
      expectErrors(err, [
        {
          message: 'must be of type array',
          type: 'json',
          location: '/coordinates',
          types: [ 'array' ],
          validator: 'type',
          value: 'foo',
          valueSet: true
        }
      ]);
    });
 
    it('should add an error if there are fewer than 2 coordinates', async function() {
 
      const point = {
        type: 'Point',
        coordinates: [ 24 ]
      };
 
      const err = await validate(point);
 
      expectErrors(err, [
        {
          message: 'must be an array of 2 numbers (longitude & latitude)',
          type: 'json',
          location: '/coordinates',
          validator: 'coordinates',
          value: [ 24 ],
          valueSet: true
        }
      ]);
    });
 
    it('should add an error if there are more than 2 coordinates', async function() {
 
      const point = {
        type: 'Point',
        coordinates: [ 24, 42, 66 ]
      };
 
      const err = await validate(point);
 
      expectErrors(err, [
        {
          message: 'must be an array of 2 numbers (longitude & latitude)',
          type: 'json',
          location: '/coordinates',
          validator: 'coordinates',
          value: [ 24, 42, 66 ],
          valueSet: true
        }
      ]);
    });
 
    it('should add an error if the longitude is not a number', async function() {
 
      const point = {
        type: 'Point',
        coordinates: [ 'foo', 42 ]
      };
 
      const err = await validate(point);
 
      expectErrors(err, [
        {
          message: 'must be of type number',
          type: 'json',
          location: '/coordinates/0',
          types: [ 'number' ],
          validator: 'type',
          value: 'foo',
          valueSet: true
        }
      ]);
    });
 
    it('should add an error if the longitude is out of bounds', async function() {
 
      const point = {
        type: 'Point',
        coordinates: [ -248, 42 ]
      };
 
      const err = await validate(point);
 
      expectErrors(err, [
        {
          message: 'must be a number between -180 and 180',
          type: 'json',
          location: '/coordinates/0',
          validator: 'longitude',
          value: -248,
          valueSet: true
        }
      ]);
    });
 
    it('should add an error if the latitude is not a number', async function() {
 
      const point = {
        type: 'Point',
        coordinates: [ 24, 'bar' ]
      };
 
      const err = await validate(point);
 
      expectErrors(err, [
        {
          message: 'must be of type number',
          type: 'json',
          location: '/coordinates/1',
          types: [ 'number' ],
          validator: 'type',
          value: 'bar',
          valueSet: true
        }
      ]);
    });
 
    it('should add an error if the latitude is out of bounds', async function() {
 
      const point = {
        type: 'Point',
        coordinates: [ 42, 100 ]
      };
 
      const err = await validate(point);
 
      expectErrors(err, [
        {
          message: 'must be a number between -90 and 90',
          type: 'json',
          location: '/coordinates/1',
          validator: 'latitude',
          value: 100,
          valueSet: true
        }
      ]);
    });
 
    async function validate(point) {
 
      let validationError;
      await dsl(function(ctx) {
        return ctx.validate(ctx.value(point), ctx.while(ctx.noError(ctx.atCurrentLocation())), validatePoint());
      }).catch(err => validationError = err);
 
      return validationError;
    }
  });
});
 
function expectErrors(validationError, expectedErrors) {
  expect(validationError).not.to.equal(undefined);
  const actualErrors = validationError.errors.map(err => _.omit(err, 'stack'));
  expect(actualErrors).to.have.objects(expectedErrors);
}