Skip to content

Commit

Permalink
Merge pull request input-output-hk#1379 from input-output-hk/chore/dd…
Browse files Browse the repository at this point in the history
…w-614-add-unit-tests-for-mnemonics

[DDW-614] Add unit tests for mnemonics
  • Loading branch information
nikolaglumac authored Apr 9, 2019
2 parents 5ea7ee3 + ac38deb commit 8897248
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Changelog

### Chores

- Added unit tests for mnemonic generation and validation ([PR 1379](https://github.com/input-output-hk/daedalus/pull/1379))
- Simplified the test setup ([PR 1378](https://github.com/input-output-hk/daedalus/pull/1378))
- Updated PR template ([PR 1376](https://github.com/input-output-hk/daedalus/pull/1376))
- Implemented new About Us dialog design with a close button ([PR 1369](https://github.com/input-output-hk/daedalus/pull/1369)
Expand Down
10 changes: 10 additions & 0 deletions features/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ Make sure Daedalus is properly installed (see above).
$ yarn test:unit
```

## Unbound tests

Unbound tests run as long as you keep them running
(never end except if an error occurs).

Example:
`yarn test:unit:unbound --tags @mnemonics`
generates and validates mnemonics as long as you keep it
running (the number of executions is updated in the terminal)

# Run UI tests

1. Make sure Daedalus is properly installed (see above).
Expand Down
13 changes: 13 additions & 0 deletions features/mnemonics-generation-and-validation.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Feature: Mnemonics generation and validation

As a developer I want to be sure our mnemonic
(12-word recovery phrase) is correctly generated/validated

@unit @slow
Scenario: All generated wallet recovery mnemonics are valid
Given I generate 10000 wallet recovery mnemonics
Then all generated wallet recovery mnemonics should be valid

@unbound @mnemonics
Scenario: Unbound manual test run gives no invalid mnemeonics
Given I generate and validate an unbound number of wallet recovery mnemonics
42 changes: 42 additions & 0 deletions features/tests/unit/steps/mnemonics-steps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Given, When, Then } from 'cucumber';
import { expect } from 'chai';
import { range } from 'lodash';
import { generateAccountMnemonics } from '../../../../source/renderer/app/api/utils/mnemonics';
import { isValidMnemonic } from '../../../../source/common/crypto/decrypt';
import { WALLET_RECOVERY_PHRASE_WORD_COUNT } from '../../../../source/renderer/app/config/cryptoConfig';

const isValidWalletRecoveryPhrase = mnemonic =>
isValidMnemonic(mnemonic, WALLET_RECOVERY_PHRASE_WORD_COUNT);

Given('I generate {int} wallet recovery mnemonics', function(
numberOfMnemonics
) {
this.context.mnemonics = range(numberOfMnemonics).map(() =>
generateAccountMnemonics().join(' ')
);
});

Then('all generated wallet recovery mnemonics should be valid', function() {
for (const mnemonic of this.context.mnemonics) {
if (!isValidWalletRecoveryPhrase(mnemonic)) {
throw new Error(`"${mnemonic}" is not valid`);
}
}
});

Given(
'I generate and validate an unbound number of wallet recovery mnemonics',
function() {
let numberOfTestsExecuted = 0;
while (true) {
const mnemonic = generateAccountMnemonics().join(' ');
if (!isValidWalletRecoveryPhrase(mnemonic)) {
throw new Error(`"${mnemonic}" is not valid`);
}
numberOfTestsExecuted++;
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(numberOfTestsExecuted + ' mnemonics validated.');
}
}
);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"test": "NODE_ENV=test yarn build && yarn test:unit && yarn test:ui",
"test:unit": "yarn cucumber --require 'features/tests/unit/**/*.js' --tags '@unit and not @skip'",
"test:unit:watch": "nodemon --watch source --watch features --exec \"yarn test:unit --tags '@unit and @watch'\"",
"test:unit:unbound": "yarn cucumber --require 'features/tests/unit/**/*.js' --tags '@unbound and not @skip'",
"test:ui": "yarn cucumber --require 'features/tests/ui/**/*.js' --tags '@ui and not @skip'",
"test:ui:watch": "gulp test:ui:watch",
"cucumber": "cross-env NODE_ENV=test cucumber-js --require-module @babel/register -f node_modules/cucumber-pretty --format-options '{\"snippetInterface\": \"async-await\"}'",
Expand Down

0 comments on commit 8897248

Please sign in to comment.