-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_core.py
138 lines (105 loc) · 3.91 KB
/
test_core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import random
from decimal import Decimal
import pytest
from strong_but_simple_passwords import core
from strong_but_simple_passwords.core import (
estimate_password_strength,
generate_password_from_sentence,
get_first_letters_from_each_word,
get_first_letters_from_word,
get_random_sentence,
get_random_symbol,
put_symbol_between_words,
sentences,
symbols,
)
def test_get_random_sentence():
random_sentence = get_random_sentence()
assert random_sentence in sentences
def test_get_first_letters_from_word():
word = "Candy"
assert get_first_letters_from_word(word, 0) == ""
assert get_first_letters_from_word(word, 1) == "C"
assert get_first_letters_from_word(word, 2) == "Ca"
assert get_first_letters_from_word(word, 3) == "Can"
assert get_first_letters_from_word(word, 10) == "Candy"
word = ""
assert get_first_letters_from_word(word, 0) == ""
assert get_first_letters_from_word(word, 1) == ""
assert get_first_letters_from_word(word, 10) == ""
with pytest.raises(ValueError):
get_first_letters_from_word("oops", -1)
def test_get_first_letters_from_each_word():
sentence = "I like cute dogs"
assert get_first_letters_from_each_word(sentence, 1) == ["I", "l", "c", "d"]
assert get_first_letters_from_each_word(sentence, 2) == ["I", "li", "cu", "do"]
assert get_first_letters_from_each_word(sentence, 3) == ["I", "lik", "cut", "dog"]
assert get_first_letters_from_each_word(sentence, 0) == ["", "", "", ""]
sentence = "Hello"
assert get_first_letters_from_each_word(sentence, 1) == ["H"]
assert get_first_letters_from_each_word(sentence, 2) == ["He"]
assert get_first_letters_from_each_word(sentence, 0) == [""]
with pytest.raises(ValueError):
get_first_letters_from_each_word(sentence, -1)
def test_get_random_symbol():
random_symbol = get_random_symbol()
assert random_symbol in symbols
def test_put_symbol_between_words():
words = ["I", "like", "cute", "dogs"]
symbol = "!"
assert put_symbol_between_words(words, symbol, 0) == [
"!",
"I",
"like",
"cute",
"dogs",
]
assert put_symbol_between_words(words, symbol, 1) == [
"I",
"!",
"like",
"cute",
"dogs",
]
assert put_symbol_between_words(words, symbol, 3) == [
"I",
"like",
"cute",
"!",
"dogs",
]
assert put_symbol_between_words(words, symbol, 4) == [
"I",
"like",
"cute",
"dogs",
"!",
]
with pytest.raises(ValueError):
put_symbol_between_words(words, symbol, 10)
with pytest.raises(ValueError):
put_symbol_between_words(words, symbol, -1)
def test_generate_password_from_sentence(monkeypatch):
monkeypatch.setattr(core, "get_random_symbol", lambda: "!")
monkeypatch.setattr(random, "randint", lambda x, y: 1)
sentence = "I like cute dogs and cats"
assert generate_password_from_sentence(sentence) == "I!lcdac"
monkeypatch.setattr(random, "randint", lambda x, y: 0)
assert generate_password_from_sentence(sentence) == "!Ilcdac"
monkeypatch.setattr(random, "randint", lambda x, y: 6)
assert generate_password_from_sentence(sentence) == "Ilcdac!"
monkeypatch.setattr(random, "randint", lambda x, y: 3)
assert (
generate_password_from_sentence(sentence, letters_per_word=3)
== "Ilikcut!dogandcat"
)
def test_estimate_password_strength():
"""
This example is taken from the zxcvbn documentation.
"""
password = "JohnSmith123"
response = estimate_password_strength(password)
assert response.get_fast_cracking_time_string() == "less than a second"
assert response.get_fast_cracking_time_seconds() == Decimal("0.00025678")
assert response.get_slow_cracking_time_string() == "4 minutes"
assert response.get_slow_cracking_time_seconds() == Decimal("256.78")