mirror of
https://github.com/SunRed/haste-server.git
synced 2024-11-01 01:30:21 +01:00
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:
parent
d2c5641886
commit
7808ccf009
7 changed files with 55 additions and 65 deletions
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
|
28
test/key_generators/dictionary.js
Normal file
28
test/key_generators/dictionary.js
Normal 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));
|
||||
});
|
||||
});
|
||||
});
|
|
@ -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));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
|
@ -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]));
|
||||
});
|
||||
});
|
||||
});
|
|
@ -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));
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue