Write unit tests for util.js

This commit is contained in:
Tankred Hase 2016-05-31 13:48:59 +02:00
parent 23ee139857
commit c8db7388c3
2 changed files with 151 additions and 3 deletions

View File

@ -88,11 +88,11 @@ exports.parseUserIds = function(userIds) {
*/ */
exports.deDup = function(list) { exports.deDup = function(list) {
var result = []; var result = [];
(list || []).forEach(function(i) { for (let i of list) {
if (result.indexOf(i) === -1) { if (result.indexOf(i) === -1) {
result.push(i); result.push(i);
} }
}); }
return result; return result;
}; };

148
test/unit/util-test.js Normal file
View File

@ -0,0 +1,148 @@
'use strict';
const expect = require('chai').expect;
const util = require('../../src/service/util');
describe('Util Unit Tests', () => {
describe('isString', () => {
it('should be true for string', () => {
expect(util.isString('asdf')).to.be.true;
});
it('should be true for String object', () => {
expect(util.isString(String('asdf'))).to.be.true;
});
it('should be true for empty String', () => {
expect(util.isString('')).to.be.true;
});
it('should be false for undefined', () => {
expect(util.isString(undefined)).to.be.false;
});
it('should be false for Object', () => {
expect(util.isString({})).to.be.false;
});
});
describe('validateKeyId', () => {
it('should be true for 40 byte hex', () => {
expect(util.validateKeyId('0123456789ABCDEF0123456789ABCDEF01234567')).to.be.true;
});
it('should be true for 16 byte hex', () => {
expect(util.validateKeyId('0123456789ABCDEF')).to.be.true;
});
it('should be true for 8 byte hex', () => {
expect(util.validateKeyId('01234567')).to.be.true;
});
it('should be false for 8 byte non-hex', () => {
expect(util.validateKeyId('0123456Z')).to.be.false;
});
it('should be false for 7 byte hex', () => {
expect(util.validateKeyId('0123456')).to.be.false;
});
it('should be false for 41 byte hex', () => {
expect(util.validateKeyId('0123456789ABCDEF0123456789ABCDEF012345678')).to.be.false;
});
it('should be false for undefined', () => {
expect(util.validateKeyId(undefined)).to.be.false;
});
it('should be false for Object', () => {
expect(util.validateKeyId({})).to.be.false;
});
});
describe('validateAddress', () => {
it('should be true valid email', () => {
expect(util.validateAddress('a@b.co')).to.be.true;
});
it('should be false for too short TLD', () => {
expect(util.validateAddress('a@b.c')).to.be.false;
});
it('should be false for no .', () => {
expect(util.validateAddress('a@bco')).to.be.false;
});
it('should be false for no @', () => {
expect(util.validateAddress('ab.co')).to.be.false;
});
it('should be false invalid cahr', () => {
expect(util.validateAddress('a<@b.co')).to.be.false;
});
it('should be false for undefined', () => {
expect(util.validateAddress(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/);
});
});
describe('throw', () => {
it('should throw error with status and expose', () => {
try {
util.throw(500, 'boom');
expect(true).to.be.false;
} catch(e) {
expect(e.message).to.equal('boom');
expect(e.status).to.equal(500);
expect(e.expose).to.be.true;
}
});
});
describe('getOrigin', () => {
it('should work', () => {
expect(util.getOrigin({host:'h', protocol:'p'})).to.exist;
});
});
});