def test_user_already_exists(): username = '******' password = '******' salt = auth.get_salt() pwdb = {'old_name': (auth.pwhash('old_password', salt), salt)} pwdb_path = tempfile.gettempdir() / PWDB_FLNAME pwdb_file = open(pwdb_path, 'wb') pickle.dump(pwdb, pwdb_file) salt = auth.get_salt() try: auth.add_user(username, password, salt, pwdb, pwdb_file) assert False except Exception as _: assert True
def test_empty_username(): username = '' password = '******' salt = auth.get_salt() pwdb = {'old_name': (auth.pwhash('old_password', salt), salt)} pwdb_path = tempfile.gettempdir() / PWDB_FLNAME with open(pwdb_path, 'wb+') as pwdb_file: pickle.dump(pwdb, pwdb_file) salt = auth.get_salt() try: with open(pwdb_path, 'wb+') as pwdb_file: auth.add_user(username, password, salt, pwdb, pwdb_file) assert False except auth.UsernameError as _: assert True
def test_wrong_name_wrong_password(): salt = auth.get_salt() password = '******' pwdb = {'real_name': (auth.pwhash(password, salt), salt)} username = '******' pass_text = 'wrong_password' assert not auth.authenticate(username, pass_text, pwdb)
def test_user_not_exists(): username = '******' password = '******' salt = auth.get_salt() pwdb = {'old_name': (auth.pwhash('old_password', salt), salt)} pwdb_path = tempfile.gettempdir() / PWDB_FLNAME with open(pwdb_path, 'wb+') as pwdb_file: pickle.dump(pwdb, pwdb_file) salt = auth.get_salt() try: with open(pwdb_path, 'wb+') as pwdb_file: auth.add_user(username, password, salt, pwdb, pwdb_file) with open(pwdb_path, 'rb+') as pwdb_file: pwdb = pickle.load(pwdb_file) print(pwdb) assert pwdb[username] == (auth.pwhash(password, salt), salt) except: assert False
def test_add_user(pwdb_path): username = '******' password = '******' try: pwdb_file = open(pwdb_path, 'rb+') except FileNotFoundError: pwdb_file = open(pwdb_path, 'wb+') pwdb = read_pwdb(pwdb_file) TEST_PWDB_FLNAME_COPY = tempfile.gettempdir() / pathlib.Path('test_pwdb_copy.pkl') pwdb_file_copy = open(TEST_PWDB_FLNAME_COPY, 'wb+') # creating a copy of the original test database # so that changes don't affect the original test database add_user(username, password, get_salt(), pwdb, pwdb_file_copy) assert username in pwdb
def test_get_salt(): salts = [auth.get_salt() for i in range(100)] assert all([len(salt) == 10 for salt in salts]), 'Some are not 10 characters long' assert len(set(salts)) == len(salts), 'Tested 100 salts are not unique'
def test_empy_database(): salt = auth.get_salt() pwdb = {} username = '******' pass_text = 'some_password' assert not auth.authenticate(username, pass_text, pwdb)