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.
This commit is contained in:
zneix 2020-09-23 01:07:32 +02:00
parent d2c5641886
commit 7808ccf009
No known key found for this signature in database
GPG Key ID: 911916E0523B22F6
7 changed files with 55 additions and 65 deletions

View File

@ -3,21 +3,22 @@ const fs = require('fs');
module.exports = class DictionaryGenerator { module.exports = class DictionaryGenerator {
constructor({ path } = {}, readyCallback){ constructor({ path } = {}){
//check for dictionary path //check for dictionary path
if (!path){ if (!path){
winston.error('No dictionary path specified in options'); let error = 'No dictionary path specified in options';
process.exit(1); winston.error(error);
throw error;
} }
//load dictionary //load dictionary
if (!fs.existsSync(path)){ if (!fs.existsSync(path)){
winston.error(`Dictionary file "${path}" doesn't exist`); let error = `Dictionary file "${path}" doesn't exist`;
process.exit(1); winston.error(error);
throw error;
} }
this.dictionary = fs.readFileSync(path).toString().split(/\s+/gm); this.dictionary = fs.readFileSync(path).toString().split(/\s+/gm);
if (readyCallback) readyCallback();
} }
// Generates a dictionary-based key, of keyLength words // Generates a dictionary-based key, of keyLength words

View File

@ -1,18 +1,20 @@
//inspiration from pwgen and https://tools.arantius.com/password //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){ function randArray(collection){
return collection[ Math.floor(Math.random() * collection.length) ]; return collection[ Math.floor(Math.random() * collection.length) ];
} }
const vovels = 'aeiou'; const vovels = 'aeiouy';
const consonants = 'bcdfghjklmnpqrstvwxyz'; const consonants = 'bcdfghjklmnpqrstvwxz';
module.exports = class PhoneticKeyGenerator { module.exports = class PhoneticKeyGenerator {
//generate a phonetic key consisting of random consonant & vowel //generate a phonetic key consisting of random consonant & vowel
createKey(keyLength){ createKey(keyLength){
let text = ''; let text = '';
const start = Math.floor(Math.random() * 2); const start = Math.floor(Math.random() * 2);
//start == 0 - starts with consonant
//start == 1 - starts with vovel
for (let i = 0; i < keyLength; i++){ for (let i = 0; i < keyLength; i++){
text += randArray(i % 2 == start ? consonants : vovels); text += randArray(i % 2 == start ? consonants : vovels);

View File

@ -1,26 +1,23 @@
/* global describe, it */ /* global describe, it */
const assert = require('assert'); const { strictEqual } = require('assert');
const DocumentHandler = require('../lib/document_handler'); const DocumentHandler = require('../lib/document_handler');
const Generator = require('../lib/key_generators/random'); const Generator = require('../lib/key_generators/random');
describe('document_handler', function(){ describe('DocumentHandler', function(){
describe('randomKey', function(){
describe('random', function(){
it('should choose a key of the proper length', function(){ it('should choose a key of the proper length', function(){
let gen = new Generator(); let gen = new Generator();
let dh = new DocumentHandler({ keyLength: 6, keyGenerator: gen }); 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(){ it('should choose a default key length', function(){
let gen = new Generator(); let gen = new Generator();
let dh = new DocumentHandler({ keyGenerator: gen }); let dh = new DocumentHandler({ keyGenerator: gen });
assert.equal(dh.keyLength, DocumentHandler.defaultKeyLength); strictEqual(dh.keyLength, DocumentHandler.defaultKeyLength);
}); });
}); });
}); });

View File

@ -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));
});
});
});

View File

@ -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));
});
});
});
});

View File

@ -1,27 +1,23 @@
/* global describe, it */ /* global describe, it */
const assert = require('assert'); const assert = require('assert');
const Generator = require('../../lib/key_generators/phonetic'); const Generator = require('../../lib/key_generators/phonetic');
const vowels = 'aeiou'; describe('KeyGenerator', () => {
const consonants = 'bcdfghjklmnpqrstvwxyz'; describe('phonetic', () => {
describe('RandomKeyGenerator', () => {
describe('randomKey', () => {
it('should return a key of the proper length', () => { it('should return a key of the proper length', () => {
const gen = new Generator(); const gen = new Generator();
assert.equal(6, gen.createKey(6).length); assert.strictEqual(6, gen.createKey(6).length);
}); });
it('should alternate consonants and vowels', () => { it('should alternate consonants and vowels', () => {
const gen = new Generator(); const gen = new Generator();
const vowels = 'aeiouy';
const key = gen.createKey(3); const key = gen.createKey(3);
assert.ok(consonants.includes(key[0])); if (vowels.includes(key[0])) assert.ok(vowels.includes(key[2]));
assert.ok(consonants.includes(key[2])); else assert.ok(vowels.includes(key[1]));
assert.ok(vowels.includes(key[1]));
}); });
}); });
}); });

View File

@ -1,19 +1,18 @@
/* global describe, it */ /* global describe, it */
const assert = require('assert'); const assert = require('assert');
const Generator = require('../../lib/key_generators/random'); const Generator = require('../../lib/key_generators/random');
describe('RandomKeyGenerator', () => { describe('KeyGenerator', () => {
describe('randomKey', () => { describe('random', () => {
it('should return a key of the proper length', () => { it('should return a key of the proper length', () => {
const gen = new Generator(); 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', () => { it('should use a key from the given keyset if given', () => {
const gen = new Generator({keyspace: 'A'}); const gen = new Generator({keyspace: 'A'});
assert.equal('AAAAAA', gen.createKey(6)); assert.strictEqual('AAAAAA', gen.createKey(6));
}); });
}); });
}); });