Path: blob/master/tests/discovery/test_githubcode.py
884 views
from unittest.mock import MagicMock1import pytest2from httpx import Response3from theHarvester.discovery import githubcode4from theHarvester.discovery.constants import MissingKey5from theHarvester.lib.core import Core678class TestSearchGithubCode:9class OkResponse:10response = Response(status_code=200)1112# Mocking the json method properly13def __init__(self):14self.response = Response(status_code=200)15object.__setattr__(16self.response,17"json",18MagicMock(19return_value={20"items": [21{"text_matches": [{"fragment": "test1"}]},22{"text_matches": [{"fragment": "test2"}]},23]24}25),26)2728class FailureResponse:29def __init__(self):30self.response = Response(status_code=401)31object.__setattr__(self.response, "json", MagicMock(return_value={}))3233class RetryResponse:34def __init__(self):35self.response = Response(status_code=403)36object.__setattr__(self.response, "json", MagicMock(return_value={}))3738class MalformedResponse:39def __init__(self):40self.response = Response(status_code=200)41object.__setattr__(42self.response,43"json",44MagicMock(45return_value={46"items": [47{"fail": True},48{"text_matches": []},49{"text_matches": [{"weird": "result"}]},50]51}52),53)5455@pytest.mark.asyncio56async def test_missing_key(self):57with pytest.raises(MissingKey):58Core.github_key = MagicMock(return_value=None) # type: ignore[method-assign]59githubcode.SearchGithubCode(word="test", limit=500)6061@pytest.mark.asyncio62async def test_fragments_from_response(self):63Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]64test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)65test_result = await test_class_instance.fragments_from_response(66self.OkResponse().response.json()67)68print("test_result: ", test_result)69assert test_result == ["test1", "test2"]7071@pytest.mark.asyncio72async def test_invalid_fragments_from_response(self):73Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]74test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)75test_result = await test_class_instance.fragments_from_response(76self.MalformedResponse().response.json()77)78assert test_result == []7980@pytest.mark.asyncio81async def test_next_page(self):82Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]83test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)84test_result = githubcode.SuccessResult(list(), next_page=2, last_page=4)85assert 2 == await test_class_instance.next_page_or_end(test_result)8687@pytest.mark.asyncio88async def test_last_page(self):89Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]90test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)91test_result = githubcode.SuccessResult(list(), 0, 0)92assert await test_class_instance.next_page_or_end(test_result) == 09394@pytest.mark.asyncio95async def test_infinite_loop_fix_page_zero(self):96"""Test that the loop condition properly exits when page becomes 0"""97Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]98test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)99100# Test the fixed condition: page != 0101page = 0102counter = 0103limit = 10104105# The condition should be False when page is 0, preventing infinite loop106condition_result = counter <= limit and page != 0107assert condition_result is False, "Loop should exit when page is 0"108109@pytest.mark.asyncio110async def test_infinite_loop_fix_page_nonzero(self):111"""Test that the loop condition continues when page is non-zero"""112Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]113test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)114115# Test with non-zero page values116for page in [1, 2, 3, 10]:117counter = 0118limit = 10119120# The condition should be True when page is non-zero121condition_result = counter <= limit and page != 0122assert condition_result is True, f"Loop should continue when page is {page}"123124@pytest.mark.asyncio125async def test_infinite_loop_fix_old_vs_new_condition(self):126"""Test that demonstrates the difference between old and new conditions"""127Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]128test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)129130page = 0131counter = 0132limit = 10133134# Old problematic condition (would cause infinite loop)135old_condition = counter <= limit and page is not None136137# New fixed condition (properly exits)138new_condition = counter <= limit and page != 0139140# Old condition would be True (causing infinite loop)141assert old_condition is True, "Old condition would cause infinite loop when page=0"142143# New condition is False (properly exits)144assert new_condition is False, "New condition properly exits when page=0"145146147if __name__ == "__main__":148pytest.main()149150151