Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applied black as code formatter #176

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions change_pswd_func/change_pswd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@

def change_pswd(old_pswd: str, new_pswd: str) -> bool:
if not find_pswd(old_pswd):
'''
"""
[Change password requirement] 1. Old password should match with system
'''
"""
print(LogMsgChangePaswd.INVALID_OLD_PSWD)
elif not verify_pswd(new_pswd):
'''
"""
[Change password requirement] 2. New password should be a valid password
'''
"""
print(LogMsgChangePaswd.INVALID_NEW_PSWD)
elif similar(old_pswd, new_pswd):
'''
"""
[Change password requirement] 3. password is not similar to old password < 80% match.
'''
"""
print(LogMsgChangePaswd.SIMILAR_TO_OLD_ONE)
else:
print(LogMsgChangePaswd.SUCCESS)
Expand Down
10 changes: 5 additions & 5 deletions change_pswd_func/check_similarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def similar(string1: str, string2: str) -> bool:
'''Check two strings similarity
"""Check two strings similarity

Calculate distance between two strings and compare distance with threshold

Expand All @@ -16,12 +16,12 @@ def similar(string1: str, string2: str) -> bool:
Returns:
bool: True if two strings are similar otherwise False

'''
"""

if type(string1) is not str or type(string2) is not str:
raise TypeError('Args need to be str type.')
raise TypeError("Args need to be str type.")

print('\n[string1]: {}'.format(string1)) # '\n' is for pytest output
print('[string2]: {}'.format(string2))
print("\n[string1]: {}".format(string1)) # '\n' is for pytest output
print("[string2]: {}".format(string2))
normalized_distance = distance(string1, string2) / max(len(string1), len(string2))
return 1 - normalized_distance > SIMILARITY_THRESHOLD
2 changes: 1 addition & 1 deletion change_pswd_func/constants.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

SP_CHARS = ['!', '@', '#', '$', '&', '*']
SP_CHARS = ["!", "@", "#", "$", "&", "*"]

MIN_VALID_LENGTH = 18

Expand Down
4 changes: 2 additions & 2 deletions change_pswd_func/find_pswd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def find_pswd(pswd: str) -> bool:
return pswd in load_system_pswd()


def load_system_pswd(filepath='change_pswd_func/file/pswds_on_system') -> List[str]:
def load_system_pswd(filepath="change_pswd_func/file/pswds_on_system") -> List[str]:
pswds = []
with open(filepath, 'r') as fr:
with open(filepath, "r") as fr:
for line in fr.readlines():
pswds.append(line.strip())
return pswds
32 changes: 20 additions & 12 deletions change_pswd_func/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,45 @@
MAX_CHAR_CONTINUOUS_NUM,
MAX_SP_CHAR_NUM,
MAX_VALID_LENGTH,
MIN_VALID_LENGTH
MIN_VALID_LENGTH,
)

T = TypeVar('T')
T = TypeVar("T")


class LogMsgVerifyPswd(Generic[T]):
# TODO Can be used these msg to check which message is shown when validation is failed on unit test

INVALID_LENGTH = '[NG] The length needs to be from {} to {}.'.format(MIN_VALID_LENGTH, MAX_VALID_LENGTH)
INVALID_LENGTH = "[NG] The length needs to be from {} to {}.".format(
MIN_VALID_LENGTH, MAX_VALID_LENGTH
)

INVALID_CHAR = '[NG] Included invalid char'
INVALID_CHAR = "[NG] Included invalid char"

NOT_ALL_PATTERNS = "[NG] All necessary patterns aren't included"

OVER_CONTINUOUS_SAME_CHARS = '[NG] Included continous more than {} same chars'.format(MAX_CHAR_CONTINUOUS_NUM)
OVER_CONTINUOUS_SAME_CHARS = (
"[NG] Included continous more than {} same chars".format(
MAX_CHAR_CONTINUOUS_NUM
)
)

OVER_SP_CHAR_NUM = '[NG] Included more than {} special characters'.format(MAX_SP_CHAR_NUM)
OVER_SP_CHAR_NUM = "[NG] Included more than {} special characters".format(
MAX_SP_CHAR_NUM
)

MORE_THAN_HALF_OF_LENGTH = '[NG] 50 % of password should not be a number'
MORE_THAN_HALF_OF_LENGTH = "[NG] 50 % of password should not be a number"

VALID = '[OK] Valid password'
VALID = "[OK] Valid password"


class LogMsgChangePaswd(Generic[T]):
# TODO Can be used these msg to check which message is shown when validation is failed on unit test

INVALID_OLD_PSWD = '[NG] Could not find old password in the system'
INVALID_OLD_PSWD = "[NG] Could not find old password in the system"

INVALID_NEW_PSWD = '[NG] Could not change password due to invalid new password'
INVALID_NEW_PSWD = "[NG] Could not change password due to invalid new password"

SIMILAR_TO_OLD_ONE = '[NG] Could not change password due to similar to previous one'
SIMILAR_TO_OLD_ONE = "[NG] Could not change password due to similar to previous one"

SUCCESS = '[OK] Changed password successfully'
SUCCESS = "[OK] Changed password successfully"
32 changes: 16 additions & 16 deletions change_pswd_func/verify_pswd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
MAX_SP_CHAR_NUM,
MAX_VALID_LENGTH,
MIN_VALID_LENGTH,
SP_CHARS
SP_CHARS,
)
from .log import LogMsgVerifyPswd


def verify_pswd(pswd: str) -> bool:
print('\n[pswd]: {}'.format(pswd)) # '\n' is for pytest output
print("\n[pswd]: {}".format(pswd)) # '\n' is for pytest output
if _check_length(pswd):
print('Password length is {}.'.format(len(pswd)))
print("Password length is {}.".format(len(pswd)))
print(LogMsgVerifyPswd.INVALID_LENGTH)
elif _include_invalid_char(pswd):
print(LogMsgVerifyPswd.INVALID_CHAR)
Expand All @@ -34,27 +34,27 @@ def verify_pswd(pswd: str) -> bool:


def _check_length(pswd: str) -> bool:
'''
"""
[Password requirement] 1. At least 18 alphanumeric characters and list of special chars !@#$&*
'''
"""
return len(pswd) < MIN_VALID_LENGTH or len(pswd) > MAX_VALID_LENGTH


def _include_invalid_char(pswd: str) -> bool:
'''
"""
[Password requirement] 1. At least 18 alphanumeric characters and list of special chars !@#$&*
'''
"""
for x in pswd:
# x.isalnum() unavailable for Hiragana, Kanji, etc
if not bool(re.search('[0-9a-zA-Z]', x)) and x not in SP_CHARS:
if not bool(re.search("[0-9a-zA-Z]", x)) and x not in SP_CHARS:
return True
return False


def _include_not_all_patterns(pswd: str) -> bool:
'''
"""
[Password requirement] 2. At least 1 Upper case, 1 lower case ,least 1 numeric, 1 special character
'''
"""
upper_flg = False
lower_flg = False
num_flg = False
Expand All @@ -74,9 +74,9 @@ def _include_not_all_patterns(pswd: str) -> bool:


def _include_over_continuous_same_chars(pswd: str) -> bool:
'''
"""
[Password requirement] 3. No duplicate repeat characters more than 4
'''
"""
count = 1
prev_char = pswd[0]
for x in list(pswd)[1:]:
Expand All @@ -92,19 +92,19 @@ def _include_over_continuous_same_chars(pswd: str) -> bool:


def _include_over_sp_char_num(pswd: str) -> bool:
'''
"""
[Password requirement] 4. No more than 4 special characters
'''
"""
count = 0
for c in SP_CHARS:
count += pswd.count(c)
return count > MAX_SP_CHAR_NUM


def _include_num_more_than_half_of_length(pswd: str) -> bool:
'''
"""
[Password requirement] 5. 50 % of password should not be a number
'''
"""
count = 0
for c in pswd:
if c.isnumeric():
Expand Down
10 changes: 6 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

from change_pswd_func.change_pswd import change_pswd

if __name__ == '__main__':
if __name__ == "__main__":

parser = argparse.ArgumentParser(description='Intent test')
parser.add_argument('old_pswd', help="Old password to be replaced with new one", type=str)
parser.add_argument('new_pswd', help='New password', type=str)
parser = argparse.ArgumentParser(description="Intent test")
parser.add_argument(
"old_pswd", help="Old password to be replaced with new one", type=str
)
parser.add_argument("new_pswd", help="New password", type=str)

args = parser.parse_args()

Expand Down
Loading