This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Let validator handle url arguments too

  • Loading branch information...
stenington committed Aug 15, 2013
1 parent 91492f2 commit d95513aeb135aaace469dbd792ca584c9f434b5b
Showing with 79 additions and 3 deletions.
  1. +15 −2 index.js
  2. +64 −1 test/full.test.js
View
@@ -5,6 +5,7 @@ const request = require('request');
const dataurl = require('dataurl');
const dateutil = require('dateutil');
const deepEqual = require('deep-equal');
const re = require('./lib/regex');
const resources = require('./lib/resources');
function isOldAssertion(assertion) {
@@ -256,7 +257,9 @@ function validate(input, callback) {
if (typeof input === 'string') {
if (isSignedBadge(input))
return fullValidateSignedAssertion(input, callback);
return callback(makeError('input', 'not a valid signed badge', { input: input }));
if (isUrl(input))
return getUrlAndValidate(input, callback);
return callback(makeError('input', 'not a valid signed badge or url', { input: input }));
}
return callback(makeError('input', 'input must be a string or object', { input: input }));
}
@@ -293,6 +296,17 @@ function checkRevoked(list, assertion) {
return makeError('verify-revoked', msg);
}
function getUrlAndValidate(url, callback) {
const options = {url: url, json: true, required: true};
return resources.getUrl(options, function(ex, result) {
if (result.error) {
result.error.field = 'assertion';
return callback(result.error);
}
return validate(result.body, callback);
});
}
function fullValidateOldAssertion(assertion, callback) {
const structuralErrors = validateAssertion(assertion);
if (structuralErrors)
@@ -429,7 +443,6 @@ function getInternalClass(thing) {
return Object.prototype.toString.call(thing);
}
const re = require('./lib/regex');
const isUrl = regexToValidator(re.url, 'must be a URL');
const isAbsoluteUrl = regexToValidator(re.absoluteUrl, 'must be an absolute URL');
const isEmail = regexToValidator(re.email, 'must be an email address');
View
@@ -5,7 +5,8 @@ const nock = require('nock');
const generators = require('./test-generators');
const keys = require('./test-keys');
var httpScope = nock('https://example.org');
var ORIGIN = 'https://example.org';
var httpScope = nock(ORIGIN);
test('validate, signed', function (t) {
const assertion = generators['1.0.0-assertion']({
@@ -93,6 +94,28 @@ test('validate, new hosted', function (t) {
});
});
test('validate, new hosted by url', function (t) {
const assertion = generators['1.0.0-assertion']();
const badge = generators['1.0.0-badge']();
const issuer = generators['1.0.0-issuer']();
httpScope
.get('/').reply(200, 'root')
.get('/assertion').reply(200, JSON.stringify(assertion))
.get('/assertion').reply(200, JSON.stringify(assertion))
.get('/badge').reply(200, JSON.stringify(badge))
.get('/issuer').reply(200, JSON.stringify(issuer))
.get('/assertion-image').reply(200, 'assertion-image', {'content-type': 'image/png'})
.get('/badge-image').reply(200, 'badge-image', {'content-type': 'image/png'})
.get('/issuer-image').reply(200, 'issuer-image')
.get('/evidence').reply(200, 'evidence')
.get('/criteria').reply(200, 'criteria')
.get('/revocation-list').reply(200, '{"found":true}')
validator(ORIGIN + '/assertion', function (err, data) {
t.notOk(err, 'should have no errors');
t.end();
});
});
test('validate, new, passed object when should pass signature', function (t) {
const assertion = generators['1.0.0-assertion']({'verify.type': 'signed'});
validator(assertion, function (err, data) {
@@ -142,6 +165,24 @@ test('validate, old style', function (t) {
});
});
test('validate, old style by url', function (t) {
const assertion = generators['0.5.0']();
httpScope
.get('/').reply(200, 'root')
.get('/image').reply(200, 'image', {'content-type': 'image/png'})
.get('/evidence').reply(200, 'evidence')
.get('/criteria').reply(200, 'criteria')
.get('/assertion').reply(200, assertion)
const originalCriteria = assertion.badge.criteria;
validator(ORIGIN + '/assertion', function (err, data) {
t.notOk(err, 'no errors');
t.same(data.version, '0.5.0');
t.same(data.structures.assertion.badge, data.structures.badge);
t.same(data.structures.badge.criteria, originalCriteria);
t.end();
});
});
test('validate, old style: invalid structure', function (t) {
const assertion = generators['0.5.0']({'badge.criteria': null});
validator(assertion, function (err, data) {
@@ -151,6 +192,28 @@ test('validate, old style: invalid structure', function (t) {
});
});
test('validate, old style by url: invalid structure', function (t) {
const assertion = generators['0.5.0']({'badge.criteria': null});
httpScope
.get('/assertion').reply(200, assertion)
validator(ORIGIN + '/assertion', function (err, data) {
t.same(err.code, 'structure');
t.ok(err.extra['badge.criteria'], 'should be a criteria error');
t.end();
});
});
test('validate by url: assertion unreachable', function (t) {
const assertion = generators['0.5.0']();
httpScope
.get('/assertion').reply(404);
validator(ORIGIN + '/assertion', function (err, data) {
t.same(err.code, 'http-status');
t.same(err.field, 'assertion');
t.end();
});
});
function forEach(obj, fn) {
Object.keys(obj).forEach(function (key) {
return fn(key, obj[key]);

0 comments on commit d95513a

Please sign in to comment.