101 lines
2.5 KiB
Python
101 lines
2.5 KiB
Python
import pytest
|
|
|
|
from seed_playground import WORDS, MnemonicWord, MnemonicPhrase
|
|
|
|
|
|
def is_a_bin_num_with_0b(possible_bin_num):
|
|
allowed_chars = {"0", "1", "b"}
|
|
if possible_bin_num[:2] == "0b" and set(possible_bin_num) <= allowed_chars:
|
|
return True
|
|
return False
|
|
|
|
|
|
@pytest.fixture()
|
|
def twelve_valid_words():
|
|
raw_words = [
|
|
"primary",
|
|
"find",
|
|
"roof",
|
|
"forget",
|
|
"subject",
|
|
"present",
|
|
"pipe",
|
|
"primary",
|
|
"flavor",
|
|
"grant",
|
|
"remain",
|
|
"present",
|
|
]
|
|
mnemonic_words = [MnemonicWord(a_word) for a_word in raw_words]
|
|
return mnemonic_words
|
|
|
|
|
|
@pytest.fixture()
|
|
def a_valid_mnemonic_phrase(twelve_valid_words):
|
|
return MnemonicPhrase(twelve_valid_words)
|
|
|
|
|
|
def test_reading_the_file_works():
|
|
|
|
there_are_2048_words = len(WORDS) == 2048
|
|
|
|
first_word_is_abandon = WORDS[0] == "abandon"
|
|
last_word_is_zoo = WORDS[2047] == "zoo"
|
|
word_1957_is_virus = WORDS[1956] == "virus"
|
|
|
|
assert (
|
|
there_are_2048_words
|
|
and first_word_is_abandon
|
|
and last_word_is_zoo
|
|
and word_1957_is_virus
|
|
)
|
|
|
|
|
|
def test_passing_bip39_valid_word_instantiates_mnemonic_word_correctly():
|
|
word = MnemonicWord("abandon")
|
|
assert isinstance(word, MnemonicWord)
|
|
|
|
|
|
def test_passing_random_word_to_mnemonic_word_raises_exception():
|
|
with pytest.raises(ValueError):
|
|
word = MnemonicWord("Machiavelli")
|
|
|
|
|
|
def test_mnemonic_word_can_be_represented_as_binary():
|
|
word = MnemonicWord("abandon")
|
|
word_as_bin = word.as_binary()
|
|
assert is_a_bin_num_with_0b(word_as_bin)
|
|
|
|
|
|
def test_mnemonic_word_can_be_represented_as_int():
|
|
word = MnemonicWord("abandon")
|
|
|
|
word_as_int = word.as_int()
|
|
|
|
assert isinstance(word_as_int, int)
|
|
|
|
|
|
def test_mnemonic_phrase_instantiates_properly_with_12_words(
|
|
twelve_valid_words
|
|
):
|
|
phrase = MnemonicPhrase(twelve_valid_words)
|
|
assert isinstance(phrase, MnemonicPhrase)
|
|
|
|
|
|
def test_mnemonic_phrase_fails_with_13_words(twelve_valid_words):
|
|
twelve_valid_words.append(MnemonicWord("abandon"))
|
|
thirteen_valid_words = twelve_valid_words
|
|
|
|
with pytest.raises(ValueError):
|
|
phrase = MnemonicPhrase(thirteen_valid_words)
|
|
|
|
|
|
def test_mnemonic_phrase_is_iterable(a_valid_mnemonic_phrase):
|
|
for word in a_valid_mnemonic_phrase:
|
|
word
|
|
|
|
|
|
def test_mnemonic_phrase_as_bin_returns_a_valid_bin(a_valid_mnemonic_phrase):
|
|
bin_representation = a_valid_mnemonic_phrase.as_bin()
|
|
|
|
assert is_a_bin_num_with_0b(bin_representation)
|