2020-04-16 02:41:53 +03:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
from app.filter import Filter
|
2021-04-01 07:23:30 +03:00
|
|
|
from app.utils.session import generate_user_key
|
2020-04-16 02:41:53 +03:00
|
|
|
from datetime import datetime
|
|
|
|
from dateutil.parser import *
|
2021-05-07 18:45:53 +03:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
from test.conftest import demo_config
|
2020-04-16 02:41:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
def get_search_results(data):
|
2021-04-01 07:23:30 +03:00
|
|
|
secret_key = generate_user_key()
|
|
|
|
soup = Filter(user_key=secret_key).clean(
|
2020-12-18 00:06:47 +03:00
|
|
|
BeautifulSoup(data, 'html.parser'))
|
2020-04-16 02:41:53 +03:00
|
|
|
|
|
|
|
main_divs = soup.find('div', {'id': 'main'})
|
|
|
|
assert len(main_divs) > 1
|
|
|
|
|
|
|
|
result_divs = []
|
|
|
|
for div in main_divs:
|
|
|
|
# Result divs should only have 1 inner div
|
2020-12-18 00:06:47 +03:00
|
|
|
if (len(list(div.children)) != 1
|
|
|
|
or not div.findChild()
|
|
|
|
or 'div' not in div.findChild().name):
|
2020-04-16 02:41:53 +03:00
|
|
|
continue
|
|
|
|
|
|
|
|
result_divs.append(div)
|
|
|
|
|
|
|
|
return result_divs
|
|
|
|
|
|
|
|
|
2020-04-29 03:59:33 +03:00
|
|
|
def test_get_results(client):
|
2020-04-16 02:41:53 +03:00
|
|
|
rv = client.get('/search?q=test')
|
|
|
|
assert rv._status_code == 200
|
|
|
|
|
2020-04-16 02:54:38 +03:00
|
|
|
# Depending on the search, there can be more
|
|
|
|
# than 10 result divs
|
2021-06-15 17:14:42 +03:00
|
|
|
results = get_search_results(rv.data)
|
|
|
|
assert len(results) >= 10
|
|
|
|
assert len(results) <= 15
|
2020-04-16 02:41:53 +03:00
|
|
|
|
|
|
|
|
2020-04-29 03:59:33 +03:00
|
|
|
def test_post_results(client):
|
|
|
|
rv = client.post('/search', data=dict(q='test'))
|
|
|
|
assert rv._status_code == 200
|
|
|
|
|
|
|
|
# Depending on the search, there can be more
|
|
|
|
# than 10 result divs
|
2021-06-15 17:14:42 +03:00
|
|
|
results = get_search_results(rv.data)
|
|
|
|
assert len(results) >= 10
|
|
|
|
assert len(results) <= 15
|
|
|
|
|
|
|
|
|
|
|
|
def test_translate_search(client):
|
|
|
|
rv = client.post('/search', data=dict(q='translate hola'))
|
|
|
|
assert rv._status_code == 200
|
|
|
|
|
|
|
|
# Pretty weak test, but better than nothing
|
|
|
|
str_data = str(rv.data)
|
|
|
|
assert 'iframe' in str_data
|
|
|
|
assert 'lingva.ml/auto/en/ hola' in str_data
|
2020-04-29 03:59:33 +03:00
|
|
|
|
|
|
|
|
2021-05-07 18:45:53 +03:00
|
|
|
def test_block_results(client):
|
|
|
|
rv = client.post('/search', data=dict(q='pinterest'))
|
|
|
|
assert rv._status_code == 200
|
|
|
|
|
|
|
|
has_pinterest = False
|
|
|
|
for link in BeautifulSoup(rv.data, 'html.parser').find_all('a', href=True):
|
|
|
|
if 'pinterest.com' in urlparse(link['href']).netloc:
|
|
|
|
has_pinterest = True
|
|
|
|
break
|
|
|
|
|
|
|
|
assert has_pinterest
|
|
|
|
|
|
|
|
demo_config['block'] = 'pinterest.com'
|
|
|
|
rv = client.post('/config', data=demo_config)
|
|
|
|
assert rv._status_code == 302
|
|
|
|
|
|
|
|
rv = client.post('/search', data=dict(q='pinterest'))
|
|
|
|
assert rv._status_code == 200
|
|
|
|
|
|
|
|
for link in BeautifulSoup(rv.data, 'html.parser').find_all('a', href=True):
|
|
|
|
assert 'pinterest.com' not in urlparse(link['href']).netloc
|
|
|
|
|
|
|
|
|
2020-12-12 03:21:32 +03:00
|
|
|
# TODO: Unit test the site alt method instead -- the results returned
|
|
|
|
# are too unreliable for this test in particular.
|
|
|
|
# def test_site_alts(client):
|
|
|
|
# rv = client.post('/search', data=dict(q='twitter official account'))
|
|
|
|
# assert b'twitter.com/Twitter' in rv.data
|
|
|
|
|
|
|
|
# client.post('/config', data=dict(alts=True))
|
|
|
|
# assert json.loads(client.get('/config').data)['alts']
|
|
|
|
|
|
|
|
# rv = client.post('/search', data=dict(q='twitter official account'))
|
|
|
|
# assert b'twitter.com/Twitter' not in rv.data
|
|
|
|
# assert b'nitter.net/Twitter' in rv.data
|
2020-12-06 01:01:21 +03:00
|
|
|
|
|
|
|
|
2020-04-16 02:41:53 +03:00
|
|
|
def test_recent_results(client):
|
|
|
|
times = {
|
2020-04-29 03:59:33 +03:00
|
|
|
'past year': 365,
|
|
|
|
'past month': 31,
|
|
|
|
'past week': 7
|
2020-04-16 02:41:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for time, num_days in times.items():
|
2020-04-29 03:59:33 +03:00
|
|
|
rv = client.post('/search', data=dict(q='test :' + time))
|
2020-04-16 02:41:53 +03:00
|
|
|
result_divs = get_search_results(rv.data)
|
|
|
|
|
|
|
|
current_date = datetime.now()
|
2020-06-28 19:52:53 +03:00
|
|
|
for div in [_ for _ in result_divs if _.find('span')]:
|
2020-04-16 02:41:53 +03:00
|
|
|
date_span = div.find('span').decode_contents()
|
2020-04-27 03:11:02 +03:00
|
|
|
if not date_span or len(date_span) > 15 or len(date_span) < 7:
|
2020-04-16 02:41:53 +03:00
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
date = parse(date_span)
|
2020-12-18 00:06:47 +03:00
|
|
|
# Date can have a little bit of wiggle room
|
|
|
|
assert (current_date - date).days <= (num_days + 5)
|
2020-04-16 02:41:53 +03:00
|
|
|
except ParserError:
|
2020-05-23 23:27:23 +03:00
|
|
|
pass
|