blockstack
Version:
The Blockstack Javascript library for authentication, identity, and storage.
307 lines (250 loc) • 17.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.runProofStatementUnitTests = runProofStatementUnitTests;
exports.runOwnerAddressBasedProofsUnitTests = runOwnerAddressBasedProofsUnitTests;
exports.runInBodyIdentityVerificationTests = runInBodyIdentityVerificationTests;
exports.runProofUtilsUnitTests = runProofUtilsUnitTests;
exports.runProofServicesUnitTests = runProofServicesUnitTests;
exports.runProofsUnitTests = runProofsUnitTests;
var _blueTape = require('blue-tape');
var _blueTape2 = _interopRequireDefault(_blueTape);
var _tape = require('tape');
var _tape2 = _interopRequireDefault(_tape);
var _fetchMock = require('fetch-mock');
var _fetchMock2 = _interopRequireDefault(_fetchMock);
var _lib = require('../../../lib');
var _sampleData = require('./sampleData');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function mockRequests() {
var naval = _sampleData.sampleVerifications.naval;
var larry = _sampleData.sampleVerifications.larry;
_fetchMock2.default.get(naval.facebook.url, naval.facebook.body);
_fetchMock2.default.get(naval.github.url + '/raw', naval.github.body);
_fetchMock2.default.get(naval.twitter.url, naval.twitter.body);
_fetchMock2.default.get(larry.facebook.url, larry.facebook.body);
}
function testProofs(profile, username, totalProofs) {
(0, _blueTape2.default)('Profiles ' + username, function (t) {
// FetchMock.get('https://www.facebook.com/larry.salibra/posts/10100341028448093', 'hi')
mockRequests();
return (0, _lib.validateProofs)(profile, undefined, username).then(function (proofs) {
t.ok(proofs, 'Proofs must have been created');
t.equal(proofs instanceof Array, true, 'Proofs should be an Array');
t.equal(proofs.length, totalProofs, 'Should have a proof for each of the ' + totalProofs + ' claimed accounts');
t.equal(proofs.filter(function (x) {
return x.valid;
}).length, totalProofs, 'Should all be valid claims');
_fetchMock2.default.restore();
});
});
}
function brokenProofs() {
var naval = _sampleData.sampleVerifications.naval;
// adding a 'b' to the url, so they don't overlap with other mocked fetches.
var navalAccounts = [{
'@type': 'Account',
service: 'facebook',
identifier: 'navalr',
proofType: 'http',
proofUrl: 'https://facebook.com/navalr/posts/10152190734077261b'
}, {
'@type': 'Account',
service: 'twitter',
identifier: 'naval',
proofType: 'http',
proofUrl: 'https://twitter.com/naval/status/486609266212499456b'
}, {
'@type': 'Account',
service: 'github',
identifier: 'navalr',
proofType: 'http',
proofUrl: 'https://gist.github.com/navalr/f31a74054f859ec0ac6ab'
}];
(0, _tape2.default)('brokenProofs', function (t) {
_fetchMock2.default.get(naval.facebook.url + 'b', naval.facebook.body);
_fetchMock2.default.get(naval.github.url + 'b/raw', naval.github.body);
_fetchMock2.default.get(naval.twitter.url + 'b', { body: '', status: 400 });
t.plan(2);
(0, _lib.validateProofs)({ account: navalAccounts }, undefined, 'naval.id').then(function (proofs) {
t.equal(proofs.length, 3);
t.equal(proofs.filter(function (x) {
return x.valid;
}).length, 2);
_fetchMock2.default.restore();
});
});
}
function runProofStatementUnitTests() {
(0, _tape2.default)('getProofStatement', function (t) {
t.plan(7);
var larry = _sampleData.sampleVerifications.larry;
var naval = _sampleData.sampleVerifications.naval;
var ken = _sampleData.sampleAddressBasedVerifications.ken;
t.equal(_lib.profileServices.facebook.getProofStatement(larry.facebook.body), 'Verifying that "larry.id" is my Blockstack ID.', 'Should extract proof statement from Facebook page meta tags');
t.equal(_lib.profileServices.twitter.getProofStatement(naval.twitter.body), 'Verifying myself: My Bitcoin username is +naval. https://t.co/DdpZv8tMAH #bitcoin', 'Should extract proof statement from Twitter page meta tags');
t.equal(_lib.profileServices.twitter.getProofStatement(ken.twitter.body), 'Verifying my Blockstack ID is secured with the address 1AtFqXxcckuoEN4iMNNe7n83c5nugxpzb5', 'Should extract address-based proof statement from Twitter page meta tags');
t.equal(_lib.profileServices.instagram.getProofStatement(ken.instagram.body), 'Verifying my Blockstack ID is secured with the address 1AtFqXxcckuoEN4iMNNe7n83c5nugxpzb5', 'Should extract address-based proof statement from Instagram meta tags');
t.equal(_lib.profileServices.hackerNews.getProofStatement(ken.hackerNews.body), 'Verifying my Blockstack ID is secured with the address 1AtFqXxcckuoEN4iMNNe7n83c5nugxpzb5', 'Should extract address-based proof statement from Hacker News profile');
t.equal(_lib.profileServices.linkedIn.getProofStatement(ken.linkedIn.body), 'Verifying my Blockstack ID is secured with the address 1AtFqXxcckuoEN4iMNNe7n83c5nugxpzb5', 'Should extract address-based proof statement from Hacker News profile');
t.equal(_lib.profileServices.linkedIn.getProofStatement(ken.linkedInBroken.body), '', 'Should not crash on broken LinkedIn proof link');
});
}
function runOwnerAddressBasedProofsUnitTests() {
(0, _tape2.default)('containsValidAddressProofStatement', function (t) {
t.plan(12);
var larry = _sampleData.sampleAddressBasedVerifications.larry;
var ken = _sampleData.sampleAddressBasedVerifications.ken;
var facebookProofStatement = _lib.profileServices.facebook.getProofStatement(larry.facebook.body);
var twitterProofStatement = _lib.profileServices.twitter.getProofStatement(ken.twitter.body);
var githubProofStatement = _lib.profileServices.github.getProofStatement(ken.github.body);
var instagramProofStatement = _lib.profileServices.instagram.getProofStatement(ken.instagram.body);
var hackerNewsProofStatement = _lib.profileServices.hackerNews.getProofStatement(ken.hackerNews.body);
var linkedInProofStatement = _lib.profileServices.linkedIn.getProofStatement(ken.linkedIn.body);
t.equals((0, _lib.containsValidAddressProofStatement)(facebookProofStatement, '1EyuZ8qxdhHjcnTChwQLyQaN3cmdK55DkH', true), true, 'Facebook post meta tags should contain valid bitcoin address proof statement');
t.equals((0, _lib.containsValidAddressProofStatement)(facebookProofStatement, 'differentBitcoinAddress', true), false, 'Facebook post meta tags should not contain valid bitcoin address proof statement');
t.equals((0, _lib.containsValidAddressProofStatement)(twitterProofStatement, '1AtFqXxcckuoEN4iMNNe7n83c5nugxpzb5', true), true, 'Twitter status meta tags should contain valid bitcoin address proof statement');
t.equals((0, _lib.containsValidAddressProofStatement)(twitterProofStatement, 'differentBitcoinAddress', true), false, 'Twitter status meta tags should not contain valid bitcoin address proof statement');
t.equals((0, _lib.containsValidAddressProofStatement)(githubProofStatement, '1AtFqXxcckuoEN4iMNNe7n83c5nugxpzb5', true), true, 'Github gist body should contain valid bitcoin address proof statement');
t.equals((0, _lib.containsValidAddressProofStatement)(githubProofStatement, 'differentBitcoinAddress', true), false, 'Github gist body should not contain valid bitcoin address proof statement');
t.equals((0, _lib.containsValidAddressProofStatement)(instagramProofStatement, '1AtFqXxcckuoEN4iMNNe7n83c5nugxpzb5', true), true, 'Instagram body should contain valid bitcoin address proof statement');
t.equals((0, _lib.containsValidAddressProofStatement)(instagramProofStatement, 'differentBitcoinAddress', true), false, 'Instagram body should not contain valid bitcoin address proof statement');
t.equals((0, _lib.containsValidAddressProofStatement)(hackerNewsProofStatement, '1AtFqXxcckuoEN4iMNNe7n83c5nugxpzb5', true), true, 'Hacker News body should contain valid bitcoin address proof statement');
t.equals((0, _lib.containsValidAddressProofStatement)(hackerNewsProofStatement, 'differentBitcoinAddress', true), false, 'Hacker News body should not contain valid bitcoin address proof statement');
t.equals((0, _lib.containsValidAddressProofStatement)(linkedInProofStatement, '1AtFqXxcckuoEN4iMNNe7n83c5nugxpzb5', true), true, 'LinkedIn body should contain valid bitcoin address proof statement');
t.equals((0, _lib.containsValidAddressProofStatement)(linkedInProofStatement, 'differentBitcoinAddress', true), false, 'LinkedIn body should not contain valid bitcoin address proof statement');
});
}
function runInBodyIdentityVerificationTests() {
(0, _tape2.default)('getProofIdentity', function (t) {
t.plan(3);
var ken = _sampleData.sampleAddressBasedVerifications.ken;
t.equal(_lib.profileServices.instagram.getProofIdentity(ken.instagram.body), 'blckstcktest', 'Should extract social proof identity from Instagram proof page body');
t.equal(_lib.profileServices.instagram.getProofIdentity(ken.instagramRegression.body), 'blckstcktest', 'Should extract social proof identity from Instagram proof page body');
t.equal(_lib.profileServices.linkedIn.getProofIdentity(ken.linkedIn.body), 'blck-stck', 'Should extract social proof identity from LinkedIn proof page body');
});
}
function runProofUtilsUnitTests() {
(0, _tape2.default)('containsValidProofStatement', function (t) {
t.plan(9);
var naval = _sampleData.sampleVerifications.naval;
t.equal((0, _lib.containsValidProofStatement)(naval.facebook.body, 'naval.id'), true, 'Facebook post body should contain valid proof statement for naval.id');
t.equal((0, _lib.containsValidProofStatement)(naval.github.body, 'naval.id'), true, 'Github gist post body should contain valid proof statement for naval.id');
t.equal((0, _lib.containsValidProofStatement)(naval.twitter.body, 'naval.id'), true, 'Twitter post body should contain valid proof statement for naval.id');
var larry = _sampleData.sampleVerifications.larry;
t.equal((0, _lib.containsValidProofStatement)(naval.facebook.body, 'larry.id'), false, 'Github gist post body should not contain valid proof statement for larry.id');
t.equal((0, _lib.containsValidProofStatement)(naval.github.body, 'larry.id'), false, 'Github gist post body should not contain valid proof statement for larry.id');
t.equal((0, _lib.containsValidProofStatement)(naval.twitter.body, 'larry.id'), false, 'Github gist post body should not contain valid proof statement for larry.id');
t.equal((0, _lib.containsValidProofStatement)(larry.facebook.body, 'larry.id'), true, 'Facebook post body should contain valid proof statement for larry.id');
var subdomainId = 'subdomainiac.id.blockstack';
t.equal((0, _lib.containsValidProofStatement)('verifying that ' + subdomainId + ' is my blockstack id', subdomainId), true, 'Subdomain IDs work as proofs');
t.throws(function () {
(0, _lib.containsValidProofStatement)(larry.facebook.body, 'larry');
}, /Error/, 'Using non-fully qualified blockstack name should throw exception');
});
}
function runProofServicesUnitTests() {
(0, _tape2.default)('normalize Facebook URLs', function (t) {
t.plan(6);
t.equal(_lib.profileServices.facebook.normalizeFacebookUrl({
service: 'facebook',
proof_url: 'https://www.facebook.com/navalr/posts/10152190734077261',
identifier: 'navalr'
}), 'https://www.facebook.com/navalr/posts/10152190734077261', 'Facebook URL should be normalized');
t.equal(_lib.profileServices.facebook.normalizeFacebookUrl({
service: 'facebook',
proof_url: 'https://facebook.com/navalr/posts/10152190734077261',
identifier: 'navalr'
}), 'https://www.facebook.com/navalr/posts/10152190734077261', 'Facebook URL should be normalized');
t.equal(_lib.profileServices.facebook.normalizeFacebookUrl({
service: 'facebook',
proof_url: 'https://www.facebook.com/larrysalibra/posts/10100341028448093',
identifier: 'larrysalibra'
}), 'https://www.facebook.com/larrysalibra/posts/10100341028448093', 'Facebook URL should be normalized');
t.notEqual(_lib.profileServices.facebook.normalizeFacebookUrl({
service: 'facebook',
proof_url: 'https://www.facebook.com/larry.salibra/posts/10100341028448093',
identifier: 'larry.salibra'
}), 'https://www.facebook.com/larrysalibra/posts/10100341028448093', 'Facebook URL should be normalized');
t.notEqual(_lib.profileServices.facebook.normalizeFacebookUrl({
service: 'facebook',
proof_url: 'https://facebook.com/larry.salibra/posts/10100341028448093',
identifier: 'larry.salibra'
}), 'https://www.facebook.com/larrysalibra/posts/10100341028448093', 'Facebook URL should be normalized');
t.equal(_lib.profileServices.facebook.normalizeFacebookUrl({
service: 'facebook',
proof_url: 'https://facebook.com/larrysalibra/posts/10100341028448093',
identifier: 'larrysalibra'
}), 'https://www.facebook.com/larrysalibra/posts/10100341028448093', 'Facebook URL should be normalized');
});
(0, _tape2.default)('normalize Instagarm URLs', function (t) {
t.plan(4);
t.equal(_lib.profileServices.instagram.normalizeInstagramUrl({
service: 'instagram',
proof_url: 'https://www.instagram.com/p/BZ7KMM0A-Qc/',
identifier: 'blckstcktest'
}), 'https://www.instagram.com/p/BZ7KMM0A-Qc/', 'Instagram URL should be normalized');
t.equal(_lib.profileServices.instagram.normalizeInstagramUrl({
service: 'instagram',
proof_url: 'https://instagram.com/p/BZ7KMM0A-Qc/',
identifier: 'blckstcktest'
}), 'https://www.instagram.com/p/BZ7KMM0A-Qc/', 'Instagram URL should be normalized');
t.equal(_lib.profileServices.instagram.normalizeInstagramUrl({
service: 'instagram',
proof_url: 'http://www.instagram.com/p/BZ7KMM0A-Qc/',
identifier: 'blckstcktest'
}), 'https://www.instagram.com/p/BZ7KMM0A-Qc/', 'Instagram URL should be normalized');
t.equal(_lib.profileServices.instagram.normalizeInstagramUrl({
service: 'instagram',
proof_url: 'http://instagram.com/p/BZ7KMM0A-Qc/',
identifier: 'blckstcktest'
}), 'https://www.instagram.com/p/BZ7KMM0A-Qc/', 'Instagram URL should be normalized');
});
(0, _tape2.default)('get proof url', function (t) {
t.plan(11);
t.equal(_lib.profileServices.facebook.getProofUrl(_sampleData.sampleProofs.naval[1]), 'https://www.facebook.com/navalr/posts/10152190734077261', 'Facebook proof URL should match reference');
t.equal(_lib.profileServices.github.getProofUrl(_sampleData.sampleProofs.naval[2]), 'https://gist.github.com/navalr/f31a74054f859ec0ac6a/raw', 'Github proof URL should match reference');
t.equal(_lib.profileServices.twitter.getProofUrl(_sampleData.sampleProofs.naval[0]), 'https://twitter.com/naval/status/486609266212499456', 'Twitter proof URL should match reference');
t.equal(_lib.profileServices.facebook.getProofUrl(_sampleData.sampleProofs.larry[0]), 'https://www.facebook.com/larry.salibra/posts/10100341028448093', 'Facebook proof URL should match reference');
t.equal(_lib.profileServices.instagram.getProofUrl(_sampleData.sampleProofs.ken[0]), 'https://www.instagram.com/p/BYj6UDwgaX7/', 'Instagram proof URL should match reference');
t.equal(_lib.profileServices.hackerNews.getProofUrl(_sampleData.sampleProofs.ken[1]), 'https://news.ycombinator.com/user?id=yukanl', 'Hacker News proof URL should match reference');
t.equal(_lib.profileServices.linkedIn.getProofUrl(_sampleData.sampleProofs.ken[2]), 'https://www.linkedin.com/feed/update/urn:li:activity:6311587377647222784/', 'LinkedIn proof URL should match reference');
t.equal(_lib.profileServices.hackerNews.getProofUrl(_sampleData.sampleProofs.bruno[0]), 'https://news.ycombinator.com/user?id=BrunoBernardino', 'Hacker News proof URL should match reference');
t.throws(function () {
var notNavalTwitter = Object.assign({}, _sampleData.sampleProofs.naval[0], {
proof_url: 'https://twitter.com/not_naval/status/486609266212499456'
});
_lib.profileServices.twitter.getProofUrl(notNavalTwitter);
}, /Error/, 'Not having claimed account identifier in Twitter proof URL should throw exception');
t.throws(function () {
var notNavalGithub = Object.assign({}, _sampleData.sampleProofs.naval[2], {
proof_url: 'https://gist.github.com/not_naval/f31a74054f859ec0ac6a'
});
_lib.profileServices.github.getProofUrl(notNavalGithub);
}, /Error/, 'Not having claimed account identifier in Github proof URL should throw exception');
t.throws(function () {
var notKenHackerNews = Object.assign({}, _sampleData.sampleProofs.ken[0], {
proof_url: 'https://news.ycombinator.com/user?id=notken'
});
_lib.profileServices.github.getProofUrl(notKenHackerNews);
}, /Error/, 'Not having claimed account identifier in Hacker News proof URL should throw exception');
});
}
function runProofsUnitTests() {
// Proof utils
runProofUtilsUnitTests();
// Proof statements
runProofStatementUnitTests();
// Proof address based
runOwnerAddressBasedProofsUnitTests();
// Proof identity extract from response body
runInBodyIdentityVerificationTests();
// Proof services
runProofServicesUnitTests();
// Proof HTML
// testProofs(sampleProfiles.naval, 'naval.id', 3)
testProofs(_sampleData.sampleProfiles.larry, 'larry.id', 1);
// Broken proofs
brokenProofs();
}