Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
laramies
GitHub Repository: laramies/theHarvester
Path: blob/master/tests/test_hackertarget_apikey.py
899 views
1
import pytest
2
from theHarvester.discovery import hackertarget as ht_mod
3
from theHarvester.lib.core import Core
4
5
6
class TestHackerTargetApiKey:
7
8
@pytest.mark.asyncio
9
async def test_do_search_with_apikey(self, monkeypatch):
10
# make Core.hackertarget_key return a known key
11
monkeypatch.setattr(Core, "hackertarget_key", lambda: "TESTKEY")
12
13
# monkeypatch AsyncFetcher.fetch_all to capture requested URLs
14
async def fake_fetch_all(urls, headers=None, proxy=False):
15
# ensure apikey present in each URL
16
assert all("apikey=TESTKEY" in u for u in urls)
17
return ["1.2.3.4,host.example.com\n", "No PTR records found\n"]
18
19
monkeypatch.setattr(ht_mod.AsyncFetcher, "fetch_all", fake_fetch_all)
20
21
s = ht_mod.SearchHackerTarget("example.com")
22
await s.do_search()
23
24
# after do_search, total_results should include our fake response (commas replaced by colons)
25
assert "1.2.3.4:host.example.com" in s.total_results
26
27
@pytest.mark.asyncio
28
async def test_do_search_without_apikey(self, monkeypatch):
29
monkeypatch.setattr(Core, "hackertarget_key", lambda: None)
30
31
async def fake_fetch_all(urls, headers=None, proxy=False):
32
assert all("apikey=" not in u for u in urls)
33
return ["1.2.3.4,host.example.com\n"]
34
35
monkeypatch.setattr(ht_mod.AsyncFetcher, "fetch_all", fake_fetch_all)
36
37
s = ht_mod.SearchHackerTarget("example.com")
38
await s.do_search()
39
assert "1.2.3.4:host.example.com" in s.total_results
40
41