mirror of
https://github.com/trustedsec/hate_crack.git
synced 2026-07-28 14:47:22 -07:00
updated pytests for hashview
This commit is contained in:
+112
-60
@@ -53,80 +53,123 @@ class TestHashviewAPI:
|
||||
# Cleanup
|
||||
|
||||
def test_list_hashfiles_success(self, api):
|
||||
"""Test successful hashfile listing with mocked API call"""
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {
|
||||
'hashfiles': json.dumps([
|
||||
{'id': 1, 'customer_id': 1, 'name': 'hashfile1.txt'},
|
||||
{'id': 2, 'customer_id': 2, 'name': 'hashfile2.txt'}
|
||||
])
|
||||
}
|
||||
mock_response.raise_for_status = Mock()
|
||||
api.session.get.return_value = mock_response
|
||||
|
||||
result = api.list_hashfiles()
|
||||
assert isinstance(result, list)
|
||||
assert len(result) == 2
|
||||
assert result[0]['name'] == 'hashfile1.txt'
|
||||
"""Test successful hashfile listing with real API if possible, else mock."""
|
||||
hashview_url = os.environ.get('HASHVIEW_URL')
|
||||
hashview_api_key = os.environ.get('HASHVIEW_API_KEY')
|
||||
if hashview_url and hashview_api_key:
|
||||
real_api = HashviewAPI(hashview_url, hashview_api_key)
|
||||
result = real_api.list_hashfiles()
|
||||
assert isinstance(result, list)
|
||||
# If there are no hashfiles, that's valid, but if present, check structure
|
||||
if result:
|
||||
assert 'name' in result[0]
|
||||
else:
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {
|
||||
'hashfiles': json.dumps([
|
||||
{'id': 1, 'customer_id': 1, 'name': 'hashfile1.txt'},
|
||||
{'id': 2, 'customer_id': 2, 'name': 'hashfile2.txt'}
|
||||
])
|
||||
}
|
||||
mock_response.raise_for_status = Mock()
|
||||
api.session.get.return_value = mock_response
|
||||
result = api.list_hashfiles()
|
||||
assert isinstance(result, list)
|
||||
assert len(result) == 2
|
||||
assert result[0]['name'] == 'hashfile1.txt'
|
||||
|
||||
def test_list_hashfiles_empty(self, api):
|
||||
"""Test hashfile listing returns empty list if no hashfiles"""
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {}
|
||||
mock_response.raise_for_status = Mock()
|
||||
api.session.get.return_value = mock_response
|
||||
|
||||
result = api.list_hashfiles()
|
||||
assert result == []
|
||||
"""Test hashfile listing returns empty list if no hashfiles (real API if possible)."""
|
||||
hashview_url = os.environ.get('HASHVIEW_URL')
|
||||
hashview_api_key = os.environ.get('HASHVIEW_API_KEY')
|
||||
if hashview_url and hashview_api_key:
|
||||
real_api = HashviewAPI(hashview_url, hashview_api_key)
|
||||
result = real_api.list_hashfiles()
|
||||
# If there are no hashfiles, result should be []
|
||||
if not result:
|
||||
assert result == []
|
||||
else:
|
||||
assert isinstance(result, list)
|
||||
else:
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {}
|
||||
mock_response.raise_for_status = Mock()
|
||||
api.session.get.return_value = mock_response
|
||||
result = api.list_hashfiles()
|
||||
assert result == []
|
||||
|
||||
def test_get_customer_hashfiles(self, api):
|
||||
"""Test filtering hashfiles by customer_id"""
|
||||
api.list_hashfiles = Mock(return_value=[
|
||||
{'id': 1, 'customer_id': 1, 'name': 'hashfile1.txt'},
|
||||
{'id': 2, 'customer_id': 2, 'name': 'hashfile2.txt'},
|
||||
{'id': 3, 'customer_id': 1, 'name': 'hashfile3.txt'}
|
||||
])
|
||||
result = api.get_customer_hashfiles(1)
|
||||
assert len(result) == 2
|
||||
assert all(hf['customer_id'] == 1 for hf in result)
|
||||
"""Test filtering hashfiles by customer_id (real API if possible)."""
|
||||
hashview_url = os.environ.get('HASHVIEW_URL')
|
||||
hashview_api_key = os.environ.get('HASHVIEW_API_KEY')
|
||||
customer_id = os.environ.get('HASHVIEW_CUSTOMER_ID')
|
||||
if hashview_url and hashview_api_key and customer_id:
|
||||
real_api = HashviewAPI(hashview_url, hashview_api_key)
|
||||
result = real_api.get_customer_hashfiles(int(customer_id))
|
||||
assert isinstance(result, list)
|
||||
# If there are hashfiles, all should match customer_id
|
||||
if result:
|
||||
assert all(hf['customer_id'] == int(customer_id) for hf in result)
|
||||
else:
|
||||
api.list_hashfiles = Mock(return_value=[
|
||||
{'id': 1, 'customer_id': 1, 'name': 'hashfile1.txt'},
|
||||
{'id': 2, 'customer_id': 2, 'name': 'hashfile2.txt'},
|
||||
{'id': 3, 'customer_id': 1, 'name': 'hashfile3.txt'}
|
||||
])
|
||||
result = api.get_customer_hashfiles(1)
|
||||
assert len(result) == 2
|
||||
assert all(hf['customer_id'] == 1 for hf in result)
|
||||
|
||||
def test_display_customers_multicolumn_empty(self, api, capsys):
|
||||
"""Test display_customers_multicolumn with no customers"""
|
||||
"""Test display_customers_multicolumn with no customers (mock only, as real API not needed)."""
|
||||
api.display_customers_multicolumn([])
|
||||
captured = capsys.readouterr()
|
||||
assert "No customers found" in captured.out
|
||||
|
||||
def test_upload_cracked_hashes_success(self, api, tmp_path):
|
||||
"""Test uploading cracked hashes with valid lines"""
|
||||
cracked_file = tmp_path / "cracked.txt"
|
||||
cracked_file.write_text("8846f7eaee8fb117ad06bdd830b7586c:password\n"
|
||||
"e19ccf75ee54e06b06a5907af13cef42:123456\n"
|
||||
"31d6cfe0d16ae931b73c59d7e0c089c0:should_skip\n"
|
||||
"invalidline\n")
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {'imported': 2}
|
||||
mock_response.raise_for_status = Mock()
|
||||
api.session.post.return_value = mock_response
|
||||
|
||||
result = api.upload_cracked_hashes(str(cracked_file), hash_type='1000')
|
||||
assert 'imported' in result
|
||||
assert result['imported'] == 2
|
||||
"""Test uploading cracked hashes with valid lines (real API if possible)."""
|
||||
hashview_url = os.environ.get('HASHVIEW_URL')
|
||||
hashview_api_key = os.environ.get('HASHVIEW_API_KEY')
|
||||
hash_type = os.environ.get('HASHVIEW_HASH_TYPE', '1000')
|
||||
if hashview_url and hashview_api_key:
|
||||
real_api = HashviewAPI(hashview_url, hashview_api_key)
|
||||
cracked_file = tmp_path / "cracked.txt"
|
||||
cracked_file.write_text("8846f7eaee8fb117ad06bdd830b7586c:password\n"
|
||||
"e19ccf75ee54e06b06a5907af13cef42:123456\n")
|
||||
try:
|
||||
result = real_api.upload_cracked_hashes(str(cracked_file), hash_type=hash_type)
|
||||
assert 'imported' in result
|
||||
except Exception as e:
|
||||
# If the API does not allow upload, skip
|
||||
pytest.skip(f"Real API upload_cracked_hashes not allowed: {e}")
|
||||
else:
|
||||
cracked_file = tmp_path / "cracked.txt"
|
||||
cracked_file.write_text("8846f7eaee8fb117ad06bdd830b7586c:password\n"
|
||||
"e19ccf75ee54e06b06a5907af13cef42:123456\n"
|
||||
"31d6cfe0d16ae931b73c59d7e0c089c0:should_skip\n"
|
||||
"invalidline\n")
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {'imported': 2}
|
||||
mock_response.raise_for_status = Mock()
|
||||
api.session.post.return_value = mock_response
|
||||
result = api.upload_cracked_hashes(str(cracked_file), hash_type='1000')
|
||||
assert 'imported' in result
|
||||
assert result['imported'] == 2
|
||||
|
||||
def test_upload_cracked_hashes_api_error(self, api, tmp_path):
|
||||
"""Test uploading cracked hashes with API error response"""
|
||||
"""Test uploading cracked hashes with API error response (mock only)."""
|
||||
cracked_file = tmp_path / "cracked.txt"
|
||||
cracked_file.write_text("8846f7eaee8fb117ad06bdd830b7586c:password\n")
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {'type': 'Error', 'msg': 'Some error'}
|
||||
mock_response.raise_for_status = Mock()
|
||||
api.session.post.return_value = mock_response
|
||||
|
||||
with pytest.raises(Exception) as excinfo:
|
||||
api.upload_cracked_hashes(str(cracked_file), hash_type='1000')
|
||||
assert "Hashview API Error" in str(excinfo.value)
|
||||
|
||||
def test_upload_cracked_hashes_invalid_json(self, api, tmp_path):
|
||||
"""Test uploading cracked hashes with invalid JSON response"""
|
||||
"""Test uploading cracked hashes with invalid JSON response (mock only)."""
|
||||
cracked_file = tmp_path / "cracked.txt"
|
||||
cracked_file.write_text("8846f7eaee8fb117ad06bdd830b7586c:password\n")
|
||||
mock_response = Mock()
|
||||
@@ -134,21 +177,30 @@ class TestHashviewAPI:
|
||||
mock_response.text = "not a json"
|
||||
mock_response.raise_for_status = Mock()
|
||||
api.session.post.return_value = mock_response
|
||||
|
||||
with pytest.raises(Exception) as excinfo:
|
||||
api.upload_cracked_hashes(str(cracked_file), hash_type='1000')
|
||||
assert "Invalid API response" in str(excinfo.value)
|
||||
|
||||
def test_create_customer_success(self, api):
|
||||
"""Test creating a customer"""
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {'id': 10, 'name': 'New Customer'}
|
||||
mock_response.raise_for_status = Mock()
|
||||
api.session.post.return_value = mock_response
|
||||
|
||||
result = api.create_customer("New Customer")
|
||||
assert result['id'] == 10
|
||||
assert result['name'] == "New Customer"
|
||||
"""Test creating a customer (real API if possible)."""
|
||||
hashview_url = os.environ.get('HASHVIEW_URL')
|
||||
hashview_api_key = os.environ.get('HASHVIEW_API_KEY')
|
||||
if hashview_url and hashview_api_key:
|
||||
real_api = HashviewAPI(hashview_url, hashview_api_key)
|
||||
try:
|
||||
result = real_api.create_customer("New Customer Test")
|
||||
assert 'id' in result
|
||||
assert 'name' in result
|
||||
except Exception as e:
|
||||
pytest.skip(f"Real API create_customer not allowed: {e}")
|
||||
else:
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {'id': 10, 'name': 'New Customer'}
|
||||
mock_response.raise_for_status = Mock()
|
||||
api.session.post.return_value = mock_response
|
||||
result = api.create_customer("New Customer")
|
||||
assert result['id'] == 10
|
||||
assert result['name'] == "New Customer"
|
||||
|
||||
def test_download_left_hashes(self, api, tmp_path):
|
||||
"""Test downloading left hashes: real API if possible, else mock."""
|
||||
|
||||
Reference in New Issue
Block a user