Skip to content
Open
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
4 changes: 1 addition & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ on:
pull_request: # Trigger on pull requests
branches:
- '*' # Matches any branch
create: # Trigger when a new branch is created
branches:
- '*' # Matches any branch


jobs:
test:
Expand Down
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"python.testing.unittestArgs": ["-v", "-s", "./tests", "-p", "*test*.py"],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
}
35 changes: 32 additions & 3 deletions Tools/helper_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pwinput
from termcolor import colored
# from termcolor import colored

# try and accept accordingly to make a program that will not crash
# learn what the type hints are and how to use them e.g. -> str, -> bool, -> int
Expand All @@ -8,11 +8,13 @@ def get_password(prompt: str) -> str:
"""
Get a password from the user.
"""
return pwinput.pwinput(prompt, mask='*')

def get_username(prompt: str) -> str:
"""
Get a username from the user.
"""
return input(prompt)

def validate_password(password: str) -> bool:
"""
Expand All @@ -24,14 +26,37 @@ def validate_password(password: str) -> bool:
- at least one digit
- at least one special character
"""

upper = False
lower = False
digit = False
special = False
if len(password) >= 8:
for x in password:
if x.isupper():
upper = True
if x.isdigit():
digit = True
if x.islower():
lower = True
if not any([x.isalpha(), x.isdigit(), x == ' ']):
special = True
return upper and lower and digit and special
return False
def validate_username(username: str) -> bool:
"""
Validate a username.
a valid username should contain:
- at least 3 characters
- no alphanumeric characters
"""
if len(username) < 3:
return False
for x in username:
if x.isalnum():
continue
else:
return False
return True
def save_user_info(username: str, password: str) -> None:
database_path = "./Database/users.csv"
"""
Expand All @@ -42,8 +67,12 @@ def save_user_info(username: str, password: str) -> None:
- append the username and password to the file
- ensure theres no duplicates
"""
with open(database_path, 'a') as f:

f.write(f'{username},{password}\n')



if __name__ == "__main__":
#use to test the functions
pass
pass
12 changes: 10 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# import the tools
from Tools.helper_functions import *


def main():
#implement logic for the program to work correctly use given functions you implemented
pass
username = get_username('Enter your user name: ')
password = get_password('Enter your password: ')
if validate_username(username) and validate_password(password):
save_user_info(username,password)
else:
return "Invalid password"




if __name__ == "__main__":
main()
main()

6 changes: 3 additions & 3 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class TestUserFunctions(unittest.TestCase):

@patch("helper_functions.pwinput.pwinput", return_value="securePassword123!")
@patch("Tools.helper_functions.pwinput.pwinput", return_value="securePassword123!")
def test_get_password(self, mock_input):
result = get_password("Enter your password: ")
self.assertEqual(result, "securePassword123!")
Expand Down Expand Up @@ -38,9 +38,9 @@ def test_save_user_info(self):
def mock_save_user_info(username, password):
save_user_info(username, password)

with patch("helper_functions.open", unittest.mock.mock_open()) as mock_file:
with patch("Tools.helper_functions.open", unittest.mock.mock_open()) as mock_file:
mock_save_user_info("user123", "Password123!")
mock_file.assert_called_once_with("user_data.csv", "a")
mock_file.assert_called_once_with("./Database/users.csv", "a")
handle = mock_file()
handle.write.assert_called_once_with("user123,Password123!\n")

Expand Down