Cleanup util
This commit is contained in:
parent
67e1b3d247
commit
7bc4f8a9d9
@ -17,8 +17,6 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
const addressparser = require('addressparser');
|
||||
|
||||
/**
|
||||
* Checks for a valid string
|
||||
* @param {} data The input to be checked
|
||||
@ -42,15 +40,27 @@ exports.isTrue = function(data) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks for a valid key id which is between 16 and 40 hex chars.
|
||||
* Checks for a valid long key id which is 16 hex chars long.
|
||||
* @param {string} data The key id
|
||||
* @return {boolean} If the key id if valid
|
||||
* @return {boolean} If the key id is valid
|
||||
*/
|
||||
exports.validateKeyId = function(data) {
|
||||
exports.isKeyId = function(data) {
|
||||
if (!this.isString(data)) {
|
||||
return false;
|
||||
}
|
||||
return /^[a-fA-F0-9]{16,40}$/.test(data);
|
||||
return /^[a-fA-F0-9]{16}$/.test(data);
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks for a valid version 4 fingerprint which is 40 hex chars long.
|
||||
* @param {string} data The key id
|
||||
* @return {boolean} If the fingerprint is valid
|
||||
*/
|
||||
exports.isFingerPrint = function(data) {
|
||||
if (!this.isString(data)) {
|
||||
return false;
|
||||
}
|
||||
return /^[a-fA-F0-9]{40}$/.test(data);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -58,7 +68,7 @@ exports.validateKeyId = function(data) {
|
||||
* @param {string} data The email address
|
||||
* @return {boolean} If the email address if valid
|
||||
*/
|
||||
exports.validateAddress = function(data) {
|
||||
exports.isEmail = function(data) {
|
||||
if (!this.isString(data)) {
|
||||
return false;
|
||||
}
|
||||
@ -66,49 +76,6 @@ exports.validateAddress = function(data) {
|
||||
return re.test(data);
|
||||
};
|
||||
|
||||
/**
|
||||
* Validate an ascii armored public PGP key block.
|
||||
* @param {string} data The armored key block
|
||||
* @return {boolean} If the key is valid
|
||||
*/
|
||||
exports.validatePublicKey = function(data) {
|
||||
if (!this.isString(data)) {
|
||||
return false;
|
||||
}
|
||||
const begin = /-----BEGIN PGP PUBLIC KEY BLOCK-----/;
|
||||
const end = /-----END PGP PUBLIC KEY BLOCK-----/;
|
||||
return begin.test(data) && end.test(data);
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse an array of user id string to objects
|
||||
* @param {Array} userIds A list of user ids strings
|
||||
* @return {Array} An array of user id objects
|
||||
*/
|
||||
exports.parseUserIds = function(userIds) {
|
||||
let result = [];
|
||||
userIds.forEach(uid => result = result.concat(addressparser(uid)));
|
||||
return result.map(u => ({
|
||||
email: u.address ? u.address.toLowerCase() : undefined,
|
||||
name: u.name
|
||||
}));
|
||||
};
|
||||
|
||||
/**
|
||||
* Deduplicates items in an array
|
||||
* @param {Array} list The list of items with duplicates
|
||||
* @return {Array} The list of items without duplicates
|
||||
*/
|
||||
exports.deDup = function(list) {
|
||||
var result = [];
|
||||
for (let i of list) {
|
||||
if (result.indexOf(i) === -1) {
|
||||
result.push(i);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* Create an error with a custom status attribute e.g. for http codes.
|
||||
* @param {number} status The error's http status code
|
||||
|
@ -46,104 +46,69 @@ describe('Util Unit Tests', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('validateKeyId', () => {
|
||||
it('should be true for 40 byte hex', () => {
|
||||
expect(util.validateKeyId('0123456789ABCDEF0123456789ABCDEF01234567')).to.be.true;
|
||||
});
|
||||
describe('isKeyId', () => {
|
||||
it('should be true for 16 byte hex', () => {
|
||||
expect(util.validateKeyId('0123456789ABCDEF')).to.be.true;
|
||||
});
|
||||
it('should be false for 15 byte hex', () => {
|
||||
expect(util.validateKeyId('0123456789ABCDE')).to.be.false;
|
||||
expect(util.isKeyId('0123456789ABCDEF')).to.be.true;
|
||||
});
|
||||
it('should be false for 16 byte non-hex', () => {
|
||||
expect(util.validateKeyId('0123456789ABCDEZ')).to.be.false;
|
||||
expect(util.isKeyId('0123456789ABCDEZ')).to.be.false;
|
||||
});
|
||||
it('should be false for 15 byte hex', () => {
|
||||
expect(util.isKeyId('0123456789ABCDE')).to.be.false;
|
||||
});
|
||||
it('should be false for 17 byte hex', () => {
|
||||
expect(util.isKeyId('0123456789ABCDEF0')).to.be.false;
|
||||
});
|
||||
it('should be false for undefined', () => {
|
||||
expect(util.isKeyId(undefined)).to.be.false;
|
||||
});
|
||||
it('should be false for Object', () => {
|
||||
expect(util.isKeyId({})).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('isFingerPrint', () => {
|
||||
it('should be true for 40 byte hex', () => {
|
||||
expect(util.isFingerPrint('0123456789ABCDEF0123456789ABCDEF01234567')).to.be.true;
|
||||
});
|
||||
it('should be false for 40 byte non-hex', () => {
|
||||
expect(util.isKeyId('0123456789ABCDEF0123456789ABCDEF0123456Z')).to.be.false;
|
||||
});
|
||||
it('should be false for 39 byte hex', () => {
|
||||
expect(util.isFingerPrint('0123456789ABCDEF0123456789ABCDEF0123456')).to.be.false;
|
||||
});
|
||||
it('should be false for 41 byte hex', () => {
|
||||
expect(util.validateKeyId('0123456789ABCDEF0123456789ABCDEF012345678')).to.be.false;
|
||||
expect(util.isFingerPrint('0123456789ABCDEF0123456789ABCDEF012345678')).to.be.false;
|
||||
});
|
||||
it('should be false for undefined', () => {
|
||||
expect(util.validateKeyId(undefined)).to.be.false;
|
||||
expect(util.isFingerPrint(undefined)).to.be.false;
|
||||
});
|
||||
it('should be false for Object', () => {
|
||||
expect(util.validateKeyId({})).to.be.false;
|
||||
expect(util.isFingerPrint({})).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('validateAddress', () => {
|
||||
describe('isEmail', () => {
|
||||
it('should be true valid email', () => {
|
||||
expect(util.validateAddress('a@b.co')).to.be.true;
|
||||
expect(util.isEmail('a@b.co')).to.be.true;
|
||||
});
|
||||
it('should be false for too short TLD', () => {
|
||||
expect(util.validateAddress('a@b.c')).to.be.false;
|
||||
expect(util.isEmail('a@b.c')).to.be.false;
|
||||
});
|
||||
it('should be false for no .', () => {
|
||||
expect(util.validateAddress('a@bco')).to.be.false;
|
||||
expect(util.isEmail('a@bco')).to.be.false;
|
||||
});
|
||||
it('should be false for no @', () => {
|
||||
expect(util.validateAddress('ab.co')).to.be.false;
|
||||
expect(util.isEmail('ab.co')).to.be.false;
|
||||
});
|
||||
it('should be false invalid cahr', () => {
|
||||
expect(util.validateAddress('a<@b.co')).to.be.false;
|
||||
expect(util.isEmail('a<@b.co')).to.be.false;
|
||||
});
|
||||
it('should be false for undefined', () => {
|
||||
expect(util.validateAddress(undefined)).to.be.false;
|
||||
expect(util.isEmail(undefined)).to.be.false;
|
||||
});
|
||||
it('should be false for Object', () => {
|
||||
expect(util.validateAddress({})).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('validatePublicKey', () => {
|
||||
let key;
|
||||
before(() => {
|
||||
key = require('fs').readFileSync(__dirname + '/../key1.asc', 'utf8');
|
||||
});
|
||||
it('should be true valid key', () => {
|
||||
expect(util.validatePublicKey(key)).to.be.true;
|
||||
});
|
||||
it('should be false invalid prefix', () => {
|
||||
expect(util.validatePublicKey(key.replace(/BEGIN PGP/, 'BEGIN PP'))).to.be.false;
|
||||
});
|
||||
it('should be false missing suffix', () => {
|
||||
expect(util.validatePublicKey(key.replace(/-----END PGP PUBLIC KEY BLOCK-----/, ''))).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('validatePublicKey', () => {
|
||||
let key;
|
||||
before(() => {
|
||||
key = require('fs').readFileSync(__dirname + '/../key1.asc', 'utf8');
|
||||
});
|
||||
it('should be true valid key', () => {
|
||||
expect(util.validatePublicKey(key)).to.be.true;
|
||||
});
|
||||
it('should be false invalid prefix', () => {
|
||||
expect(util.validatePublicKey(key.replace(/BEGIN PGP/, 'BEGIN PP'))).to.be.false;
|
||||
});
|
||||
it('should be false missing suffix', () => {
|
||||
expect(util.validatePublicKey(key.replace(/-----END PGP PUBLIC KEY BLOCK-----/, ''))).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseUserIds', () => {
|
||||
it('should parse string', () => {
|
||||
expect(util.parseUserIds(['A <A@b.co>'])).to.deep.equal([{name:'A', email:'a@b.co'}]);
|
||||
});
|
||||
it('should work for empty array', () => {
|
||||
expect(util.parseUserIds([])).to.deep.equal([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('deDup', () => {
|
||||
it('should work for empty array', () => {
|
||||
expect(util.deDup([])).to.deep.equal([]);
|
||||
});
|
||||
it('should work for empty array', () => {
|
||||
expect(util.deDup(['a','b','a'])).to.deep.equal(['a','b']);
|
||||
});
|
||||
it('should throw for undefined', () => {
|
||||
expect(util.deDup.bind(null, undefined)).to.throw(/Cannot read property/);
|
||||
expect(util.isEmail({})).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user