From c8db7388c368ca938b47eb7d52095fe0b32202a6 Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Tue, 31 May 2016 13:48:59 +0200 Subject: [PATCH] Write unit tests for util.js --- src/service/util.js | 6 +- test/unit/util-test.js | 148 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+), 3 deletions(-) create mode 100644 test/unit/util-test.js diff --git a/src/service/util.js b/src/service/util.js index 9a2ec23..4514685 100644 --- a/src/service/util.js +++ b/src/service/util.js @@ -56,7 +56,7 @@ exports.validateAddress = function(data) { /** * Validate an ascii armored public PGP key block. * @param {string} data The armored key block - * @return {boolean} If the key is valid + * @return {boolean} If the key is valid */ exports.validatePublicKey = function(data) { if (!this.isString(data)) { @@ -88,11 +88,11 @@ exports.parseUserIds = function(userIds) { */ exports.deDup = function(list) { var result = []; - (list || []).forEach(function(i) { + for (let i of list) { if (result.indexOf(i) === -1) { result.push(i); } - }); + } return result; }; diff --git a/test/unit/util-test.js b/test/unit/util-test.js new file mode 100644 index 0000000..903a48b --- /dev/null +++ b/test/unit/util-test.js @@ -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 '])).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; + }); + }); + +}); \ No newline at end of file