This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import os
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from app.sources import GoogleBrowserSearchBlocked, GoogleBrowserSearchSource
|
||||
|
||||
|
||||
class GoogleBrowserSearchSourceTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.old_enabled = os.environ.pop("GOOGLE_BROWSER_SEARCH_ENABLED", None)
|
||||
|
||||
def tearDown(self):
|
||||
GoogleBrowserSearchSource._last_request_at = None
|
||||
if self.old_enabled is None:
|
||||
os.environ.pop("GOOGLE_BROWSER_SEARCH_ENABLED", None)
|
||||
else:
|
||||
os.environ["GOOGLE_BROWSER_SEARCH_ENABLED"] = self.old_enabled
|
||||
|
||||
def test_disabled_feature_blocks_without_network_io(self):
|
||||
source = GoogleBrowserSearchSource()
|
||||
with patch("app.sources.urlopen") as network:
|
||||
with self.assertRaises(GoogleBrowserSearchBlocked) as raised:
|
||||
source.discover(
|
||||
{"approved": True, "public_access": True, "terms_accepted": True, "rate_limit": 6},
|
||||
criteria={"keywords": ["solar installers"], "city": "Cape Town"},
|
||||
limits={"max_records": 5},
|
||||
)
|
||||
self.assertEqual(raised.exception.code, "GOOGLE_BROWSER_BLOCKED")
|
||||
self.assertEqual(raised.exception.reason, "feature_disabled")
|
||||
network.assert_not_called()
|
||||
|
||||
def test_enabled_fetch_builds_query_from_criteria_and_parses_visible_result_links(self):
|
||||
os.environ["GOOGLE_BROWSER_SEARCH_ENABLED"] = "true"
|
||||
|
||||
class Response:
|
||||
def read(self, _limit):
|
||||
return b'<html><body><a href="https://acme.example/about"><h3>Acme Solar</h3></a><a href="https://www.google.com/preferences"><h3>Settings</h3></a></body></html>'
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *_):
|
||||
return False
|
||||
|
||||
with patch("app.sources.urlopen", return_value=Response()) as network:
|
||||
page = GoogleBrowserSearchSource().discover(
|
||||
{"approved": True, "public_access": True, "terms_accepted": True, "rate_limit": 6},
|
||||
criteria={"keywords": ["solar"], "city": "Cape Town"},
|
||||
limits={"max_records": 4},
|
||||
)
|
||||
self.assertEqual(page.records, [{"name": "Acme Solar", "website": "https://acme.example/about", "email": "", "phone": "", "description": "Public Google search result", "location": ""}])
|
||||
request = network.call_args.args[0]
|
||||
self.assertIn("q=solar+Cape+Town", request.full_url)
|
||||
self.assertNotIn("query", request.full_url)
|
||||
self.assertTrue(page.metadata["public_html_only"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user