Path: blob/master/tests/discovery/test_githubcode_additions.py
609 views
from unittest.mock import MagicMock, AsyncMock1import asyncio2import pytest3from _pytest.mark.structures import MarkDecorator4from theHarvester.discovery import githubcode5from theHarvester.lib.core import Core67pytestmark: MarkDecorator = pytest.mark.asyncio8910class TestSearchGithubCodeProcess:11async def test_process_stops_after_max_retries(self, monkeypatch):12Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]13inst = githubcode.SearchGithubCode(word="test", limit=10)1415# Speed up by avoiding actual sleeps16monkeypatch.setattr(githubcode, "get_delay", lambda: 0, raising=False)17monkeypatch.setattr(asyncio, "sleep", AsyncMock(return_value=None))1819# Force RetryResult every time20monkeypatch.setattr(21inst,22"handle_response",23AsyncMock(return_value=githubcode.RetryResult(0)),24)25monkeypatch.setattr(26inst,27"do_search",28AsyncMock(return_value=("", {}, 403, {})),29)3031inst.max_retries = 232await inst.process()33assert inst.page == 0, "Process should stop after exceeding max retries"34assert inst.retry_count == 3, "Retry count should exceed max_retries before stopping"3536async def test_process_stops_on_error_result(self, monkeypatch):37Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]38inst = githubcode.SearchGithubCode(word="test", limit=10)3940monkeypatch.setattr(githubcode, "get_delay", lambda: 0, raising=False)41monkeypatch.setattr(asyncio, "sleep", AsyncMock(return_value=None))4243# Force ErrorResult44monkeypatch.setattr(45inst,46"handle_response",47AsyncMock(return_value=githubcode.ErrorResult(500, "err")),48)49monkeypatch.setattr(50inst,51"do_search",52AsyncMock(return_value=("", {}, 500, {})),53)5455await inst.process()56assert inst.page == 0, "Process should stop on error result to avoid infinite loop"5758async def test_process_breaks_on_same_page_pagination(self, monkeypatch):59Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]60inst = githubcode.SearchGithubCode(word="test", limit=10)6162monkeypatch.setattr(githubcode, "get_delay", lambda: 0, raising=False)63monkeypatch.setattr(asyncio, "sleep", AsyncMock(return_value=None))6465# Force SuccessResult that does not advance the page66monkeypatch.setattr(67inst,68"handle_response",69AsyncMock(return_value=githubcode.SuccessResult([], next_page=1, last_page=0)),70)71monkeypatch.setattr(72inst,73"do_search",74AsyncMock(return_value=("", {"items": []}, 200, {})),75)7677await inst.process()78assert inst.page == 0, "Process should stop when pagination does not advance"798081