Skip to content

Commit

Permalink
Merge pull request #43 from UBC-MDS/week2_decfun_body
Browse files Browse the repository at this point in the history
separated decryption tests
  • Loading branch information
rorywhite200 authored Jan 18, 2024
2 parents e5cca57 + 9fcdda0 commit f395808
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 65 deletions.
20 changes: 7 additions & 13 deletions src/passwordler/decrypt_password.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
from ._internals import original, getKeyMap

def decrypt_password(encrypted_message, random_seed = 123):
"""
Expand Down Expand Up @@ -42,23 +43,16 @@ def decrypt_password(encrypted_message, random_seed = 123):
'encrypted_message cannot be empty string')

random.seed(random_seed)

original = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q',
'r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y',
'Z','é','è','à','ü','ö','ä','0','1','2','3','4','5','6','7','8','9',
'.',',',';',':','!','?','+','@','#','%','&','$','£','=','-','_',' ',]

encryption = original.copy()
random.shuffle(encryption)

decryption = original.copy()
random.shuffle(decryption)

keyMap = getKeyMap(decryption, isDecryption=True)
decrypted_msg = []

for character in encrypted_message:
if character in original:
index = encryption.index(character)
new_char = original[index]
decrypted_msg.append(new_char)
if character in keyMap:
decrypted_msg.append(keyMap[character])
else:
decrypted_msg.append(character)

Expand Down
57 changes: 57 additions & 0 deletions tests/test_decrypt_password.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import pytest
import random
from passwordler.decrypt_password import decrypt_password


def test_decrypt_password_type_error():
"""
Test if the function returns a TypeError if the type of the input is not correct.
"""
with pytest.raises(TypeError, match="string expected as encrypted message"):
decrypt_password(123)

with pytest.raises(TypeError, match="integer expected as random_seed"):
decrypt_password("abc", random_seed="123")


def test_decrypt_password_value_error():
"""
Test if the function returns a ValueError if the input for the encrypted_message is an emptry string.
"""
with pytest.raises(ValueError, match="encrypted_message cannot be empty string"):
decrypt_password("")


def test_decrypt_password_no_seed():
"""
Test if the function returns a string as output if the seed is left as default.
"""
result = decrypt_password("Camelot")
assert isinstance(result, str)


def test_decrypt_password_upper_vs_lower_case():
"""
Test that the function handles case sensitivity correctly.
"""
result_lower = decrypt_password("spam", random_seed=42)
result_upper = decrypt_password("SPAM", random_seed=42)
assert result_lower != result_upper


def test_decrypt_password_special_characters_input():
"""
Test if the function handles special characters correctly.
"""
result = decrypt_password("@#$%^&*()_+=-]|;:,.<>?/`~", random_seed=42)
assert isinstance(result, str)


def test_decrypt_password_long_input_message():
"""
Test if the function handles longer messages correctly.
"""
result = decrypt_password(
"I say you are, and I should know. I followed a few!", random_seed=42
)
assert isinstance(result, str)
58 changes: 6 additions & 52 deletions tests/test_encrypt_decrypt_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,13 @@
from passwordler.decrypt_password import decrypt_password
from passwordler.encrypt_password import encrypt_password

def test_decrypt_password_type_error():
"""
Test if the function returns a TypeError if the type of the input is not correct.
"""
with pytest.raises(TypeError, match="string expected as encrypted message"):
decrypt_password(123)

with pytest.raises(TypeError, match="integer expected as random_seed"):
decrypt_password("abc", random_seed="123")

def test_decrypt_password_value_error():
"""
Test if the function returns a ValueError if the input for the encrypted_message is an emptry string.
"""
with pytest.raises(ValueError, match="encrypted_message cannot be empty string"):
decrypt_password('')

def test_decrypt_password_no_seed():
"""
Test if the function returns a string as output if the seed is left as default.
"""
result = decrypt_password('Camelot')
assert isinstance(result, str)

def test_decrypt_password_upper_vs_lower_case():
"""
Test that the function handles case sensitivity correctly.
"""
result_lower = decrypt_password('spam', random_seed=42)
result_upper = decrypt_password('SPAM', random_seed=42)
assert result_lower != result_upper

def test_decrypt_password_special_characters_input():
def test_encryption_decryption():
"""
Test if the function handles special characters correctly.
Test if the encryption and decryption function match each other.
"""
result = decrypt_password('@#$%^&*()_+=-]|;:,.<>?/`~', random_seed=42)
assert isinstance(result, str)

def test_decrypt_password_long_input_message():
"""
Test if the function handles longer messages correctly.
"""
result = decrypt_password('I say you are, and I should know. I followed a few!', random_seed=42)
assert isinstance(result, str)

encrypted_message = encrypt_password("Monty Python", random_seed=123)

#def test_encryption_decryption():
# """
# Test if the encryption and decryption function match each other.
# """
# encrypted_message = encrypt_password('Monty Python', random_seed=123)
#
# assert (
# decrypt_password(encrypted_message, random_seed=123) == "Monty Python"
# ), "The encrypted message is not the same as the initial message"
assert (
decrypt_password(encrypted_message, random_seed=123) == "Monty Python"
), "The encrypted message is not the same as the initial message"

0 comments on commit f395808

Please sign in to comment.