From 7808ccf009937c868820a4e81a4cedd621049b57 Mon Sep 17 00:00:00 2001 From: zneix <44851575+zneix@users.noreply.github.com> Date: Wed, 23 Sep 2020 01:07:32 +0200 Subject: [PATCH] Rewrote key generator tests, fixed vowels -Tests were not brought up to date with some updates that were done to key generator modules. -Apparently original creator had no idea that 'y' is a vowel; fixed this issue and reflected changes in actual files. --- lib/key_generators/dictionary.js | 13 ++++---- lib/key_generators/phonetic.js | 8 +++-- test/document_handler_spec.js | 13 +++----- test/key_generators/dictionary.js | 28 ++++++++++++++++ test/key_generators/dictionary_spec.js | 33 ------------------- .../{phonetic_spec.js => phonetic.js} | 16 ++++----- .../{random_spec.js => random.js} | 9 +++-- 7 files changed, 55 insertions(+), 65 deletions(-) create mode 100644 test/key_generators/dictionary.js delete mode 100644 test/key_generators/dictionary_spec.js rename test/key_generators/{phonetic_spec.js => phonetic.js} (53%) rename test/key_generators/{random_spec.js => random.js} (67%) diff --git a/lib/key_generators/dictionary.js b/lib/key_generators/dictionary.js index 37afe82..5743e25 100644 --- a/lib/key_generators/dictionary.js +++ b/lib/key_generators/dictionary.js @@ -3,21 +3,22 @@ const fs = require('fs'); module.exports = class DictionaryGenerator { - constructor({ path } = {}, readyCallback){ + constructor({ path } = {}){ //check for dictionary path if (!path){ - winston.error('No dictionary path specified in options'); - process.exit(1); + let error = 'No dictionary path specified in options'; + winston.error(error); + throw error; } //load dictionary if (!fs.existsSync(path)){ - winston.error(`Dictionary file "${path}" doesn't exist`); - process.exit(1); + let error = `Dictionary file "${path}" doesn't exist`; + winston.error(error); + throw error; } this.dictionary = fs.readFileSync(path).toString().split(/\s+/gm); - if (readyCallback) readyCallback(); } // Generates a dictionary-based key, of keyLength words diff --git a/lib/key_generators/phonetic.js b/lib/key_generators/phonetic.js index bfc402c..485f67a 100644 --- a/lib/key_generators/phonetic.js +++ b/lib/key_generators/phonetic.js @@ -1,18 +1,20 @@ //inspiration from pwgen and https://tools.arantius.com/password -//helper function to get a random consonant / vowel +//helper function to get a random array element function randArray(collection){ return collection[ Math.floor(Math.random() * collection.length) ]; } -const vovels = 'aeiou'; -const consonants = 'bcdfghjklmnpqrstvwxyz'; +const vovels = 'aeiouy'; +const consonants = 'bcdfghjklmnpqrstvwxz'; module.exports = class PhoneticKeyGenerator { //generate a phonetic key consisting of random consonant & vowel createKey(keyLength){ let text = ''; const start = Math.floor(Math.random() * 2); + //start == 0 - starts with consonant + //start == 1 - starts with vovel for (let i = 0; i < keyLength; i++){ text += randArray(i % 2 == start ? consonants : vovels); diff --git a/test/document_handler_spec.js b/test/document_handler_spec.js index 39b1ff7..9cd2961 100644 --- a/test/document_handler_spec.js +++ b/test/document_handler_spec.js @@ -1,26 +1,23 @@ /* global describe, it */ -const assert = require('assert'); - +const { strictEqual } = require('assert'); const DocumentHandler = require('../lib/document_handler'); const Generator = require('../lib/key_generators/random'); -describe('document_handler', function(){ - - describe('randomKey', function(){ +describe('DocumentHandler', function(){ + describe('random', function(){ it('should choose a key of the proper length', function(){ let gen = new Generator(); let dh = new DocumentHandler({ keyLength: 6, keyGenerator: gen }); - assert.equal(6, dh.acceptableKey().length); + strictEqual(6, dh.acceptableKey().length); }); it('should choose a default key length', function(){ let gen = new Generator(); let dh = new DocumentHandler({ keyGenerator: gen }); - assert.equal(dh.keyLength, DocumentHandler.defaultKeyLength); + strictEqual(dh.keyLength, DocumentHandler.defaultKeyLength); }); - }); }); diff --git a/test/key_generators/dictionary.js b/test/key_generators/dictionary.js new file mode 100644 index 0000000..2e206c3 --- /dev/null +++ b/test/key_generators/dictionary.js @@ -0,0 +1,28 @@ +/* global describe, it */ + +const fs = require('fs'); +const assert = require('assert'); +const Generator = require('../../lib/key_generators/dictionary'); + +const tmpPath = '/tmp/haste-server-test-dictionary'; + +describe('KeyGenerator', () => { + describe('dictionary', () => { + it('should throw an error if given no path', () => { + assert.throws(() => { new Generator(); }); + }); + + it('should throw an error if given invalid path', () => { + if (fs.existsSync(tmpPath)) fs.unlinkSync(tmpPath); + assert.throws(() => { new Generator({path: tmpPath}); }); + }); + + it('should return a key of the proper number of words from the given dictionary', () => { + const words = ['xd']; + fs.writeFileSync(tmpPath, words.join('\n')); + + const gen = new Generator({path: tmpPath}); + assert.strictEqual('xdxdxd', gen.createKey(3)); + }); + }); +}); diff --git a/test/key_generators/dictionary_spec.js b/test/key_generators/dictionary_spec.js deleted file mode 100644 index 9abcf26..0000000 --- a/test/key_generators/dictionary_spec.js +++ /dev/null @@ -1,33 +0,0 @@ -/* global describe, it */ - -const assert = require('assert'); - -const fs = require('fs'); - -const Generator = require('../../lib/key_generators/dictionary'); - -describe('RandomKeyGenerator', function(){ - describe('randomKey', function(){ - it('should throw an error if given no options', () => { - assert.throws(() => { - new Generator(); - }, Error); - }); - - it('should throw an error if given no path', () => { - assert.throws(() => { - new Generator({}); - }, Error); - }); - - it('should return a key of the proper number of words from the given dictionary', () => { - const path = '/tmp/haste-server-test-dictionary'; - const words = ['cat']; - fs.writeFileSync(path, words.join('\n')); - - const gen = new Generator({path}, () => { - assert.equal('catcatcat', gen.createKey(3)); - }); - }); - }); -}); diff --git a/test/key_generators/phonetic_spec.js b/test/key_generators/phonetic.js similarity index 53% rename from test/key_generators/phonetic_spec.js rename to test/key_generators/phonetic.js index cf8acef..299f1c8 100644 --- a/test/key_generators/phonetic_spec.js +++ b/test/key_generators/phonetic.js @@ -1,27 +1,23 @@ /* global describe, it */ const assert = require('assert'); - const Generator = require('../../lib/key_generators/phonetic'); -const vowels = 'aeiou'; -const consonants = 'bcdfghjklmnpqrstvwxyz'; - -describe('RandomKeyGenerator', () => { - describe('randomKey', () => { +describe('KeyGenerator', () => { + describe('phonetic', () => { it('should return a key of the proper length', () => { const gen = new Generator(); - assert.equal(6, gen.createKey(6).length); + assert.strictEqual(6, gen.createKey(6).length); }); it('should alternate consonants and vowels', () => { const gen = new Generator(); + const vowels = 'aeiouy'; const key = gen.createKey(3); - assert.ok(consonants.includes(key[0])); - assert.ok(consonants.includes(key[2])); - assert.ok(vowels.includes(key[1])); + if (vowels.includes(key[0])) assert.ok(vowels.includes(key[2])); + else assert.ok(vowels.includes(key[1])); }); }); }); diff --git a/test/key_generators/random_spec.js b/test/key_generators/random.js similarity index 67% rename from test/key_generators/random_spec.js rename to test/key_generators/random.js index aace497..2e47cd8 100644 --- a/test/key_generators/random_spec.js +++ b/test/key_generators/random.js @@ -1,19 +1,18 @@ /* global describe, it */ const assert = require('assert'); - const Generator = require('../../lib/key_generators/random'); -describe('RandomKeyGenerator', () => { - describe('randomKey', () => { +describe('KeyGenerator', () => { + describe('random', () => { it('should return a key of the proper length', () => { const gen = new Generator(); - assert.equal(6, gen.createKey(6).length); + assert.strictEqual(6, gen.createKey(6).length); }); it('should use a key from the given keyset if given', () => { const gen = new Generator({keyspace: 'A'}); - assert.equal('AAAAAA', gen.createKey(6)); + assert.strictEqual('AAAAAA', gen.createKey(6)); }); }); });