Skip to content

AI powered OSINT engine to discover and map 840+ social media identities across the web

test: replace aioresponses with real aiohttp test server

arxhr0075efdbce5594d
diff --git a/pyproject.toml b/pyproject.toml
index ec0ef1e..54bba79 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -38,7 +38,6 @@ train = ["scikit-learn>=1.3", "numpy>=1.24"]
dev = [
"pytest>=8",
"pytest-asyncio>=0.23",
- "aioresponses>=0.7",
"ruff>=0.4",
"build",
"twine",
diff --git a/tests/test_scanner.py b/tests/test_scanner.py
index dbfbe84..93396b6 100644
--- a/tests/test_scanner.py
+++ b/tests/test_scanner.py
@@ -1,6 +1,9 @@
+import socket
+
import aiohttp
import pytest
-from aioresponses import aioresponses
+from aiohttp import web
+from aiohttp.test_utils import TestServer
from aliens_eye.core.analyzer import FeatureExtractor
from aliens_eye.core.config import ScannerConfig
@@ -8,14 +11,32 @@ from aliens_eye.core.detector import Detector
from aliens_eye.core.fingerprints import FingerprintStore
from aliens_eye.core.scanner import UsernameScanner, build_connector
-SITES = {
- "alpha": "https://alpha.example/{}",
- "beta": "https://beta.example/users/{}",
-}
+
+def free_port() -> int:
+ with socket.socket() as sock:
+ sock.bind(("127.0.0.1", 0))
+ return sock.getsockname()[1]
@pytest.fixture
-def scanner(tmp_path, logger):
+async def site_server(found_html, not_found_html):
+ app = web.Application()
+
+ async def alpha(request):
+ return web.Response(text=found_html, content_type="text/html")
+
+ async def beta(request):
+ return web.Response(text=not_found_html, status=404, content_type="text/html")
+
+ app.router.add_get("/alpha/{username}", alpha)
+ app.router.add_get("/beta/users/{username}", beta)
+ server = TestServer(app)
+ await server.start_server()
+ yield server
+ await server.close()
+
+
+def make_scanner(sites, tmp_path, logger):
config = ScannerConfig(
retries=0,
rate_limit_delay=0.0,
@@ -24,7 +45,7 @@ def scanner(tmp_path, logger):
plain_output=True,
)
return UsernameScanner(
- sites_data=dict(SITES),
+ sites_data=sites,
config=config,
extractor=FeatureExtractor(),
detector=Detector(),
@@ -33,11 +54,17 @@ def scanner(tmp_path, logger):
)
-async def test_scan_all_sites_mocked(scanner, found_html, not_found_html):
- with aioresponses() as mocked:
- mocked.get("https://alpha.example/torvalds", status=200, body=found_html)
- mocked.get("https://beta.example/users/torvalds", status=404, body=not_found_html)
- results = await scanner.scan_all_sites("torvalds")
+def server_sites(server):
+ base = f"http://{server.host}:{server.port}"
+ return {
+ "alpha": base + "/alpha/{}",
+ "beta": base + "/beta/users/{}",
+ }
+
+
+async def test_scan_all_sites_end_to_end(site_server, tmp_path, logger):
+ scanner = make_scanner(server_sites(site_server), tmp_path, logger)
+ results = await scanner.scan_all_sites("torvalds")
assert len(results) == 2
by_site = {r["site"]: r for r in results}
@@ -49,24 +76,20 @@ async def test_scan_all_sites_mocked(scanner, found_html, not_found_html):
assert set(r) >= {"site", "url", "status", "code", "confidence", "ai_analysis"}
-async def test_scan_handles_connection_error(scanner):
- with aioresponses() as mocked:
- mocked.get(
- "https://alpha.example/ghost",
- exception=aiohttp.ClientConnectionError("refused"),
- )
- mocked.get("https://beta.example/users/ghost", status=200, body="<html></html>")
- results = await scanner.scan_all_sites("ghost")
+async def test_scan_handles_connection_error(site_server, tmp_path, logger):
+ sites = server_sites(site_server)
+ sites["dead"] = f"http://127.0.0.1:{free_port()}/{{}}"
+ scanner = make_scanner(sites, tmp_path, logger)
+ results = await scanner.scan_all_sites("ghost")
by_site = {r["site"]: r for r in results}
- assert by_site["alpha"]["status"] == "Error"
+ assert by_site["dead"]["status"] in {"Error", "Timeout"}
+ assert by_site["alpha"]["status"] == "Found"
-async def test_scan_with_variations_basic(scanner, found_html):
- with aioresponses() as mocked:
- mocked.get("https://alpha.example/torvalds", status=200, body=found_html)
- mocked.get("https://beta.example/users/torvalds", status=200, body=found_html)
- all_results = await scanner.scan_with_variations("torvalds", "basic")
+async def test_scan_with_variations_basic(site_server, tmp_path, logger):
+ scanner = make_scanner(server_sites(site_server), tmp_path, logger)
+ all_results = await scanner.scan_with_variations("torvalds", "basic")
assert list(all_results) == ["torvalds"]
assert len(all_results["torvalds"]) == 2