Add missing @pytest.mark.asyncio decorators to async tests and improve error handling in otxsearch.py

This commit is contained in:
L1ghtn1ng
2025-10-12 00:03:13 +01:00
parent 0382650e61
commit be106ad779
6 changed files with 61 additions and 24 deletions
+8 -3
View File
@@ -1,13 +1,10 @@
from unittest.mock import MagicMock
import pytest
from _pytest.mark.structures import MarkDecorator
from httpx import Response
from theHarvester.discovery import githubcode
from theHarvester.discovery.constants import MissingKey
from theHarvester.lib.core import Core
pytestmark: MarkDecorator = pytest.mark.asyncio
class TestSearchGithubCode:
class OkResponse:
@@ -56,11 +53,13 @@ class TestSearchGithubCode:
),
)
@pytest.mark.asyncio
async def test_missing_key(self):
with pytest.raises(MissingKey):
Core.github_key = MagicMock(return_value=None) # type: ignore[method-assign]
githubcode.SearchGithubCode(word="test", limit=500)
@pytest.mark.asyncio
async def test_fragments_from_response(self):
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]
test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)
@@ -70,6 +69,7 @@ class TestSearchGithubCode:
print("test_result: ", test_result)
assert test_result == ["test1", "test2"]
@pytest.mark.asyncio
async def test_invalid_fragments_from_response(self):
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]
test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)
@@ -78,18 +78,21 @@ class TestSearchGithubCode:
)
assert test_result == []
@pytest.mark.asyncio
async def test_next_page(self):
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]
test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)
test_result = githubcode.SuccessResult(list(), next_page=2, last_page=4)
assert 2 == await test_class_instance.next_page_or_end(test_result)
@pytest.mark.asyncio
async def test_last_page(self):
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]
test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)
test_result = githubcode.SuccessResult(list(), 0, 0)
assert await test_class_instance.next_page_or_end(test_result) is 0
@pytest.mark.asyncio
async def test_infinite_loop_fix_page_zero(self):
"""Test that the loop condition properly exits when page becomes 0"""
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]
@@ -104,6 +107,7 @@ class TestSearchGithubCode:
condition_result = counter <= limit and page != 0
assert condition_result is False, "Loop should exit when page is 0"
@pytest.mark.asyncio
async def test_infinite_loop_fix_page_nonzero(self):
"""Test that the loop condition continues when page is non-zero"""
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]
@@ -118,6 +122,7 @@ class TestSearchGithubCode:
condition_result = counter <= limit and page != 0
assert condition_result is True, f"Loop should continue when page is {page}"
@pytest.mark.asyncio
async def test_infinite_loop_fix_old_vs_new_condition(self):
"""Test that demonstrates the difference between old and new conditions"""
Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]