Refactor search and test files to ensure proper context usage for Config initialization. Updated search function to use fallback for full_query and adjusted tests to utilize app context for Config instantiation.

This commit is contained in:
Don-Swanson 2025-09-21 00:24:22 -05:00
parent 7f80eb1e51
commit 99c7c7b00d
No known key found for this signature in database
GPG key ID: C6A6ACD574A005E5
4 changed files with 22 additions and 8 deletions

View file

@ -380,9 +380,10 @@ def search():
elif search_util.widget == 'calculator' and not 'nojs' in request.args:
response = add_calculator_card(html_soup)
# Update tabs content
# Update tabs content (fallback to the raw query if full_query isn't set)
full_query_val = getattr(search_util, 'full_query', query)
tabs = get_tabs_content(app.config['HEADER_TABS'],
search_util.full_query,
full_query_val,
search_util.search_type,
g.user_config.preferences,
translation)

View file

@ -3,6 +3,7 @@ import os
from bs4 import BeautifulSoup
from app import app
from app.filter import Filter
from app.models.config import Config
from app.utils.session import generate_key
@ -15,7 +16,8 @@ def build_soup(html: str):
def make_filter(soup: BeautifulSoup):
secret_key = generate_key()
cfg = Config(**{'alts': True})
with app.app_context():
cfg = Config(**{'alts': True})
f = Filter(user_key=secret_key, config=cfg)
f.soup = soup
return f
@ -99,7 +101,9 @@ def test_single_pass_description_replacement(monkeypatch):
assert a['href'].startswith('https://nitter.example')
# Ensure description got host swapped once, no double scheme or duplication
text = soup.find('div').get_text()
main_div = soup.find('div', id='main')
# The description div is the first inner div under #main in this fixture
text = main_div.find_all('div')[0].get_text().strip()
assert text.startswith('https://nitter.example')
assert 'https://https://' not in text
assert 'nitter.examplenitter.example' not in text

View file

@ -1,3 +1,4 @@
from app import app
from app.request import Request
from app.models.config import Config
@ -21,7 +22,8 @@ class FakeHttpClient:
def test_autocomplete_parsing():
cfg = Config(**{})
with app.app_context():
cfg = Config(**{})
req = Request(normal_ua='UA', root_path='http://localhost:5000', config=cfg, http_client=FakeHttpClient())
suggestions = req.autocomplete('who')
assert 'whoogle' in suggestions

View file

@ -1,5 +1,6 @@
import pytest
from app import app
from app.request import Request, TorError
from app.models.config import Config
@ -26,18 +27,24 @@ class FakeHttpClient:
def build_config(tor: bool) -> Config:
# Minimal config with tor flag
return Config(**{'tor': tor})
with app.app_context():
return Config(**{'tor': tor})
def test_tor_validation_success():
def test_tor_validation_success(monkeypatch):
# Prevent real Tor signal attempts
monkeypatch.setattr('app.request.send_tor_signal', lambda signal: True)
cfg = build_config(tor=True)
req = Request(normal_ua='TestUA', root_path='http://localhost:5000', config=cfg, http_client=FakeHttpClient(tor_ok=True))
# Avoid sending a Tor NEWNYM/HEARTBEAT in unit tests by setting attempt>0 false path
resp = req.send(base_url='https://example.com', query='')
assert req.tor_valid is True
assert resp.status_code == 200
def test_tor_validation_failure():
def test_tor_validation_failure(monkeypatch):
# Prevent real Tor signal attempts
monkeypatch.setattr('app.request.send_tor_signal', lambda signal: True)
cfg = build_config(tor=True)
req = Request(normal_ua='TestUA', root_path='http://localhost:5000', config=cfg, http_client=FakeHttpClient(tor_ok=False))
with pytest.raises(TorError):