Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # --------------- check_password.py ------------------
- def check_password(word: str) -> bool:
- """
- Перевірка паролю
- Довжина від 8 до 30 символів
- Хоча б 1 велика буква
- Хоча б 1 маленька буква
- Хоча б 1 цифра
- Хоча б 1 спец символ
- """
- if not isinstance(word, str):
- raise TypeError
- # приберемо пробіли на початку і в кінці
- word = word.strip(" ")
- no_space = " " not in word
- valid_length = len(word) in range(8, 31)
- has_upper = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ").intersection(word)
- has_lower = set("abcdefghijklmnopqrstuvwxyz").intersection(word)
- has_digit = set("1234567890").intersection(word)
- has_punctuation = set("?/.,<>:\"'-=`|~;'[]{})(_+!\\@#$%^&*").intersection(word)
- return all([no_space, valid_length, has_upper, has_lower, has_digit, has_punctuation])
- # ----------------------- tests/test_password.py -------------------------
- import string
- import pytest
- from check_password import check_password
- @pytest.mark.parametrize(["word", "expected"], [
- ["SomePass123!@#", True],
- ["So1!", False],
- ["SomePass123!@#++++++++++++++++++++++++++++++++++++++++", False],
- ["SomePass!@#", False],
- ["somepass123!@#", False],
- ["SOMEPASS123!@#", False],
- ["SomePass123", False],
- # граничні значення
- ["Aa1!!!!", False], # 7
- ["Aa1!!!!!", True], # 8
- ["Aa1!!!!!" + "1" * 22, True], # 30
- ["Aa1!!!!!" + "1" * 23, False], # 31
- # пробіл в паролі
- ["Some Pass123!@#", False],
- [" " * 30 + "SomePass123!@#", True],
- ["SomePass123!@#" + " " * 30, True],
- ])
- def test_password(word, expected):
- assert check_password(word) is expected
- def test_type_error():
- with pytest.raises(TypeError):
- check_password(1234)
- @pytest.mark.parametrize("val", string.digits)
- def test_any_digit(val):
- assert check_password("Aaaaaaaaa!" + val) is True
- @pytest.mark.parametrize("val", string.ascii_uppercase)
- def test_any_upper_char(val):
- assert check_password("1aaaaaaaa!" + val) is True
- @pytest.mark.parametrize("val", string.ascii_lowercase)
- def test_any_lower_char(val):
- assert check_password("1AAAAAAA!" + val) is True
- @pytest.mark.parametrize("val", string.punctuation)
- def test_any_punc(val):
- assert check_password("1AAAAAAAa" + val) is True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement