Path: blob/master/tests/discovery/test_githubcode.py
609 views
from unittest.mock import MagicMock1import pytest2from _pytest.mark.structures import MarkDecorator3from httpx import Response4from theHarvester.discovery import githubcode5from theHarvester.discovery.constants import MissingKey6from theHarvester.lib.core import Core78pytestmark: MarkDecorator = pytest.mark.asyncio91011class TestSearchGithubCode:12class OkResponse:13response = Response(status_code=200)1415# Mocking the json method properly16def __init__(self):17self.response = Response(status_code=200)18object.__setattr__(19self.response,20"json",21MagicMock(22return_value={23"items": [24{"text_matches": [{"fragment": "test1"}]},25{"text_matches": [{"fragment": "test2"}]},26]27}28),29)3031class FailureResponse:32def __init__(self):33self.response = Response(status_code=401)34object.__setattr__(self.response, "json", MagicMock(return_value={}))3536class RetryResponse:37def __init__(self):38self.response = Response(status_code=403)39object.__setattr__(self.response, "json", MagicMock(return_value={}))4041class MalformedResponse:42def __init__(self):43self.response = Response(status_code=200)44object.__setattr__(45self.response,46"json",47MagicMock(48return_value={49"items": [50{"fail": True},51{"text_matches": []},52{"text_matches": [{"weird": "result"}]},53]54}55),56)5758async def test_missing_key(self):59with pytest.raises(MissingKey):60Core.github_key = MagicMock(return_value=None) # type: ignore[method-assign]61githubcode.SearchGithubCode(word="test", limit=500)6263async def test_fragments_from_response(self):64Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]65test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)66test_result = await test_class_instance.fragments_from_response(67self.OkResponse().response.json()68)69print("test_result: ", test_result)70assert test_result == ["test1", "test2"]7172async 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 == []7980async def test_next_page(self):81Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]82test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)83test_result = githubcode.SuccessResult(list(), next_page=2, last_page=4)84assert 2 == await test_class_instance.next_page_or_end(test_result)8586async def test_last_page(self):87Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]88test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)89test_result = githubcode.SuccessResult(list(), 0, 0)90assert await test_class_instance.next_page_or_end(test_result) is 09192async def test_infinite_loop_fix_page_zero(self):93"""Test that the loop condition properly exits when page becomes 0"""94Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]95test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)9697# Test the fixed condition: page != 098page = 099counter = 0100limit = 10101102# The condition should be False when page is 0, preventing infinite loop103condition_result = counter <= limit and page != 0104assert condition_result is False, "Loop should exit when page is 0"105106async def test_infinite_loop_fix_page_nonzero(self):107"""Test that the loop condition continues when page is non-zero"""108Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]109test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)110111# Test with non-zero page values112for page in [1, 2, 3, 10]:113counter = 0114limit = 10115116# The condition should be True when page is non-zero117condition_result = counter <= limit and page != 0118assert condition_result is True, f"Loop should continue when page is {page}"119120async def test_infinite_loop_fix_old_vs_new_condition(self):121"""Test that demonstrates the difference between old and new conditions"""122Core.github_key = MagicMock(return_value="test_key") # type: ignore[method-assign]123test_class_instance = githubcode.SearchGithubCode(word="test", limit=500)124125page = 0126counter = 0127limit = 10128129# Old problematic condition (would cause infinite loop)130old_condition = counter <= limit and page is not None131132# New fixed condition (properly exits)133new_condition = counter <= limit and page != 0134135# Old condition would be True (causing infinite loop)136assert old_condition is True, "Old condition would cause infinite loop when page=0"137138# New condition is False (properly exits)139assert new_condition is False, "New condition properly exits when page=0"140141142if __name__ == "__main__":143pytest.main()144145146