Add tests for b64_to_str

This commit is contained in:
Wout Bouckaert
2025-05-10 20:19:48 -06:00
parent 202766b066
commit 8131be7711
2 changed files with 52 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
import base64
import binascii
from hashlib import blake2b
from pathlib import Path
@@ -118,5 +119,5 @@ class CryptoHelper:
"""
try:
return base64.b64decode(input_b64).decode("UTF-8")
except (RuntimeError, UnicodeError) as why:
except (RuntimeError, UnicodeError, binascii.Error) as why:
raise RuntimeError(f"Unable to decode {input_b64} to b64.") from why

View File

@@ -1,3 +1,5 @@
import pytest
from app.classes.helpers.cryptography_helper import CryptoHelper
@@ -71,6 +73,11 @@ def test_bytes_to_hex_known_value() -> None:
def test_str_to_b64() -> None:
"""
Returns:
"""
# Test 1
known_value_1: str = "Hello World"
known_output_1: str = "SGVsbG8gV29ybGQ="
@@ -79,3 +86,46 @@ def test_str_to_b64() -> None:
known_value_2: str = "I love Crafty! Yee haw!"
known_output_2: str = "SSBsb3ZlIENyYWZ0eSEgWWVlIGhhdyE="
assert CryptoHelper.str_to_b64(known_value_2) == known_output_2
def test_b64_to_str() -> None:
"""
Test known input with b64_to_str
Returns:
"""
# Test 1
known_value_1: str = "SGVsbG8gV29ybGQ="
known_output_1: str = "Hello World"
assert CryptoHelper.b64_to_str(known_value_1) == known_output_1
# Test 2
known_value_2: str = "SSBsb3ZlIENyYWZ0eSEgWWVlIGhhdyE="
known_output_2: str = "I love Crafty! Yee haw!"
assert CryptoHelper.b64_to_str(known_value_2) == known_output_2
def test_b64_to_str_not_b64() -> None:
"""
Test b64_to_str function when give a value that is not b64 encoded. Should return
RuntimeError.
Returns:
"""
test_error_value: str = "!This is not B64 encoded text!"
with pytest.raises(RuntimeError):
_ = CryptoHelper.b64_to_str(test_error_value)
def test_b64_to_str_not_unicode() -> None:
"""
Test b64_to_str with data that is not Unicode. Should return RuntimeError.
Returns:
"""
random_data: str = "gQ=="
with pytest.raises(RuntimeError):
_ = CryptoHelper.b64_to_str(random_data)