Path: blob/master/tests/test_hackertarget_apikey.py
899 views
import pytest1from theHarvester.discovery import hackertarget as ht_mod2from theHarvester.lib.core import Core345class TestHackerTargetApiKey:67@pytest.mark.asyncio8async def test_do_search_with_apikey(self, monkeypatch):9# make Core.hackertarget_key return a known key10monkeypatch.setattr(Core, "hackertarget_key", lambda: "TESTKEY")1112# monkeypatch AsyncFetcher.fetch_all to capture requested URLs13async def fake_fetch_all(urls, headers=None, proxy=False):14# ensure apikey present in each URL15assert all("apikey=TESTKEY" in u for u in urls)16return ["1.2.3.4,host.example.com\n", "No PTR records found\n"]1718monkeypatch.setattr(ht_mod.AsyncFetcher, "fetch_all", fake_fetch_all)1920s = ht_mod.SearchHackerTarget("example.com")21await s.do_search()2223# after do_search, total_results should include our fake response (commas replaced by colons)24assert "1.2.3.4:host.example.com" in s.total_results2526@pytest.mark.asyncio27async def test_do_search_without_apikey(self, monkeypatch):28monkeypatch.setattr(Core, "hackertarget_key", lambda: None)2930async def fake_fetch_all(urls, headers=None, proxy=False):31assert all("apikey=" not in u for u in urls)32return ["1.2.3.4,host.example.com\n"]3334monkeypatch.setattr(ht_mod.AsyncFetcher, "fetch_all", fake_fetch_all)3536s = ht_mod.SearchHackerTarget("example.com")37await s.do_search()38assert "1.2.3.4:host.example.com" in s.total_results394041