lots of refactoring around the menues and building out test cases

This commit is contained in:
Justin Bollinger
2026-02-05 13:52:06 -05:00
parent bbaa40958a
commit 9756f83b0c
32 changed files with 2458 additions and 1236 deletions
+21 -8
View File
@@ -1,21 +1,22 @@
import os
import sys
import types
import pytest
from unittest import mock
import hate_crack.api as api
@pytest.fixture
def fake_file(tmp_path):
return tmp_path / "testfile.txt"
@pytest.fixture
def patch_dependencies(tmp_path):
# Patch sanitize_filename to just return the filename
patch1 = mock.patch("hate_crack.api.sanitize_filename", side_effect=lambda x: x)
# Patch get_hcat_wordlists_dir to return tmp_path
patch2 = mock.patch("hate_crack.api.get_hcat_wordlists_dir", return_value=str(tmp_path))
patch2 = mock.patch(
"hate_crack.api.get_hcat_wordlists_dir", return_value=str(tmp_path)
)
# Patch extract_with_7z to just return True
patch3 = mock.patch("hate_crack.api.extract_with_7z", return_value=True)
# Patch os.replace to do nothing
@@ -29,16 +30,18 @@ def patch_dependencies(tmp_path):
for p in patches:
p.stop()
def make_mock_response(content=b"abc", total=3, status_code=200, endswith='.txt'):
def make_mock_response(content=b"abc", total=3, status_code=200, endswith=".txt"):
mock_resp = mock.MagicMock()
mock_resp.__enter__.return_value = mock_resp
mock_resp.__exit__.return_value = False
mock_resp.iter_content = lambda chunk_size: [content]
mock_resp.headers = {'content-length': str(total)}
mock_resp.headers = {"content-length": str(total)}
mock_resp.status_code = status_code
mock_resp.raise_for_status = mock.Mock()
return mock_resp
def test_download_success(tmp_path, patch_dependencies):
file_name = "wordlist.txt"
out_path = str(tmp_path / file_name)
@@ -49,6 +52,7 @@ def test_download_success(tmp_path, patch_dependencies):
assert result is True
m_open.assert_called() # File was opened for writing
def test_download_7z_triggers_extract(tmp_path, patch_dependencies):
file_name = "archive.7z"
out_path = str(tmp_path / file_name)
@@ -61,26 +65,35 @@ def test_download_7z_triggers_extract(tmp_path, patch_dependencies):
assert result is True
m_extract.assert_called_once()
def test_download_keyboard_interrupt(tmp_path, patch_dependencies):
file_name = "wordlist.txt"
out_path = str(tmp_path / file_name)
# Simulate KeyboardInterrupt in requests.get context manager
def raise_keyboard_interrupt(*a, **kw):
raise KeyboardInterrupt()
with mock.patch("hate_crack.api.requests.get", side_effect=raise_keyboard_interrupt):
with mock.patch(
"hate_crack.api.requests.get", side_effect=raise_keyboard_interrupt
):
result = api.download_official_wordlist(file_name, out_path)
assert result is False
def test_download_exception(tmp_path, patch_dependencies):
file_name = "wordlist.txt"
out_path = str(tmp_path / file_name)
# Simulate generic Exception in requests.get context manager
def raise_exception(*a, **kw):
raise Exception("fail")
with mock.patch("hate_crack.api.requests.get", side_effect=raise_exception):
result = api.download_official_wordlist(file_name, out_path)
assert result is False
def test_progress_bar_prints(tmp_path, patch_dependencies, capsys):
file_name = "wordlist.txt"
out_path = str(tmp_path / file_name)
@@ -92,4 +105,4 @@ def test_progress_bar_prints(tmp_path, patch_dependencies, capsys):
result = api.download_official_wordlist(file_name, out_path)
assert result is True
captured = capsys.readouterr()
assert "Downloaded" in captured.out or "Downloaded" in captured.err
assert "Downloaded" in captured.out or "Downloaded" in captured.err