From 0b09e53479afef97e94aaf19234a09d808813b49 Mon Sep 17 00:00:00 2001 From: Ivan Tivonenko Date: Wed, 29 Jul 2015 03:22:17 +0300 Subject: [PATCH] cover api/common/schema-validator.js with tests --- src/api/common/schema-validator.js | 6 +++- src/api/common/validate.js | 2 +- test/api-test.js | 47 +++++++++++++++++++++++++-- test/fixtures/schemas/ledgerhash.json | 7 ++++ 4 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/schemas/ledgerhash.json diff --git a/src/api/common/schema-validator.js b/src/api/common/schema-validator.js index 9d58f3f8..da0e3a58 100644 --- a/src/api/common/schema-validator.js +++ b/src/api/common/schema-validator.js @@ -60,4 +60,8 @@ function schemaValidate(schemaName, object) { } SCHEMAS = loadSchemas(path.join(__dirname, './schemas')); -module.exports = schemaValidate; +module.exports = { + schemaValidate: schemaValidate, + loadSchema: loadSchema, + SCHEMAS: SCHEMAS +}; diff --git a/src/api/common/validate.js b/src/api/common/validate.js index 2184acf4..e217007e 100644 --- a/src/api/common/validate.js +++ b/src/api/common/validate.js @@ -2,7 +2,7 @@ const _ = require('lodash'); const core = require('./utils').core; const ValidationError = require('./errors').ValidationError; -const schemaValidate = require('./schema-validator'); +const schemaValidate = require('./schema-validator').schemaValidate; function error(text) { return new ValidationError(text); diff --git a/test/api-test.js b/test/api-test.js index 4c18b634..1c11708c 100644 --- a/test/api-test.js +++ b/test/api-test.js @@ -2,6 +2,7 @@ 'use strict'; const _ = require('lodash'); const assert = require('assert-diff'); +const path = require('path'); const setupAPI = require('./setup-api'); const fixtures = require('./fixtures/api'); const requests = fixtures.requests; @@ -15,7 +16,7 @@ const validate = require('../src/api/common/validate'); const RippleError = require('../src/core/rippleerror').RippleError; const utils = require('../src/api/ledger/utils'); const ledgerClosed = require('./fixtures/api/rippled/ledger-close-newer'); -const schemaValidate = require('../src/api/common/schema-validator'); +const schemaValidator = require('../src/api/common/schema-validator'); const orderbook = { base: { @@ -36,7 +37,7 @@ function checkResult(expected, schemaName, done, error, response) { // console.log(JSON.stringify(response, null, 2)); assert.deepEqual(response, expected); if (schemaName) { - schemaValidate(schemaName, response); + schemaValidator.schemaValidate(schemaName, response); } done(); } @@ -154,7 +155,7 @@ describe('RippleAPI', function() { withDeterministicPRNG(() => { const result = this.api.sign(requests.sign, secret); assert.deepEqual(result, responses.sign); - schemaValidate('sign', result); + schemaValidator.schemaValidate('sign', result); }); }); @@ -571,6 +572,46 @@ describe('RippleAPI', function() { }); }); + describe('schema-validator', function() { + beforeEach(function() { + const schema = schemaValidator.loadSchema(path.join(__dirname, + './fixtures/schemas/ledgerhash.json')); + schemaValidator.SCHEMAS.ledgerhash = schema; + }); + + it('valid', function() { + assert.doesNotThrow(function() { + schemaValidator.schemaValidate('ledgerhash', + '0F7ED9F40742D8A513AE86029462B7A6768325583DF8EE21B7EC663019DD6A0F'); + }); + }); + + it('invalid', function() { + assert.throws(function() { + schemaValidator.schemaValidate('ledgerhash', 'invalid'); + }, this.api.errors.ValidationError); + }); + + it('invalid - empty value', function() { + assert.throws(function() { + schemaValidator.schemaValidate('ledgerhash', ''); + }, this.api.errors.ValidationError); + }); + + it('load schema error', function() { + assert.throws(function() { + schemaValidator.loadSchema('/bad/file/name'); + }, Error); + }); + + it('schema not found error', function() { + assert.throws(function() { + schemaValidator.schemaValidate('unexisting', 'anything'); + }, /schema not found/); + }); + + }); + it('validator', function() { const noSecret = {address: address}; assert.throws(_.partial(validate.addressAndSecret, noSecret), diff --git a/test/fixtures/schemas/ledgerhash.json b/test/fixtures/schemas/ledgerhash.json new file mode 100644 index 00000000..bc67b99d --- /dev/null +++ b/test/fixtures/schemas/ledgerhash.json @@ -0,0 +1,7 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "ledgerhash", + "description": "A ledger hash", + "type": "string", + "format": "ledgerHash" +}