Path: blob/master/tests/discovery/test_githubcode_additions.py
883 views
from unittest.mock import MagicMock, AsyncMock1import asyncio2import pytest3from theHarvester.discovery import githubcode4from theHarvester.lib.core import Core567class TestSearchGithubCodeProcess:8@pytest.mark.asyncio9async def test_process_stops_after_max_retries(self, monkeypatch):10Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]11inst = githubcode.SearchGithubCode(word="test", limit=10)1213# Speed up by avoiding actual sleeps14monkeypatch.setattr(githubcode, "get_delay", lambda: 0, raising=False)15monkeypatch.setattr(asyncio, "sleep", AsyncMock(return_value=None))1617# Force RetryResult every time18monkeypatch.setattr(19inst,20"handle_response",21AsyncMock(return_value=githubcode.RetryResult(0)),22)23monkeypatch.setattr(24inst,25"do_search",26AsyncMock(return_value=("", {}, 403, {})),27)2829inst.max_retries = 230await inst.process()31assert inst.page == 0, "Process should stop after exceeding max retries"32assert inst.retry_count == 3, "Retry count should exceed max_retries before stopping"3334@pytest.mark.asyncio35async def test_process_stops_on_error_result(self, monkeypatch):36Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]37inst = githubcode.SearchGithubCode(word="test", limit=10)3839monkeypatch.setattr(githubcode, "get_delay", lambda: 0, raising=False)40monkeypatch.setattr(asyncio, "sleep", AsyncMock(return_value=None))4142# Force ErrorResult43monkeypatch.setattr(44inst,45"handle_response",46AsyncMock(return_value=githubcode.ErrorResult(500, "err")),47)48monkeypatch.setattr(49inst,50"do_search",51AsyncMock(return_value=("", {}, 500, {})),52)5354await inst.process()55assert inst.page == 0, "Process should stop on error result to avoid infinite loop"5657@pytest.mark.asyncio58async 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