110 lines
3.9 KiB
Python
110 lines
3.9 KiB
Python
import json
|
|
import pytest
|
|
import mock
|
|
|
|
from django.conf import settings
|
|
from django.test import Client
|
|
from django.test.client import RequestFactory
|
|
from django.template import Context
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from combo.apps.search.models import SearchCell
|
|
from combo.data.models import Page, JsonCell
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
client = Client()
|
|
|
|
SEARCH_SERVICES = {
|
|
'search1': {
|
|
'label': 'Search 1',
|
|
'url': 'http://www.example.net/search/?q=%(q)s',
|
|
}
|
|
}
|
|
|
|
|
|
class SearchServices(object):
|
|
def __init__(self, search_services):
|
|
self.search_services = search_services
|
|
|
|
def __enter__(self):
|
|
settings.COMBO_SEARCH_SERVICES = self.search_services
|
|
|
|
def __exit__(self, *args, **kwargs):
|
|
delattr(settings, 'COMBO_SEARCH_SERVICES')
|
|
|
|
def test_enabled(app):
|
|
assert SearchCell.is_enabled() == False
|
|
with SearchServices(SEARCH_SERVICES):
|
|
assert SearchCell.is_enabled() == True
|
|
with SearchServices({}):
|
|
assert SearchCell.is_enabled() == False
|
|
|
|
def test_search_cell(app):
|
|
with SearchServices(SEARCH_SERVICES):
|
|
page = Page(title='Search', slug='search_page', template_name='standard')
|
|
page.save()
|
|
|
|
cell = SearchCell(page=page, placeholder='content', order=0)
|
|
cell._search_service = 'search1'
|
|
cell.save()
|
|
|
|
context = Context({})
|
|
resp = cell.render(context)
|
|
assert 'input' in resp
|
|
assert 'id="combo-search-input-%s"' % cell.pk in resp
|
|
|
|
cell.slug = 'var-name'
|
|
context = Context({'request': RequestFactory().get('/?q_var_name=searchme')})
|
|
resp = cell.render(context)
|
|
assert "combo_search_input_%s.val('searchme');" % cell.pk in resp
|
|
|
|
with mock.patch('combo.apps.search.models.requests.get') as requests_get:
|
|
|
|
response = {'err': 0, 'data': []}
|
|
mock_json = mock.Mock()
|
|
mock_json.json.return_value = response
|
|
requests_get.return_value = mock_json
|
|
resp = client.get('/ajax/search/%s/?q=foo' % cell.pk, status=200)
|
|
assert requests_get.call_args[0][0] == 'http://www.example.net/search/?q=foo'
|
|
assert '<li>' not in resp.content
|
|
|
|
response['data'] = [{'url': 'http://test', 'text': 'barbarbar'}]
|
|
resp = client.get('/ajax/search/%s/?q=foo' % cell.pk, status=200)
|
|
assert resp.content.count('<li>') == 1
|
|
assert '<li><a href="http://test">barbarbar</a></li>' in resp.content
|
|
|
|
response['data'] = [{'url': 'http://test', 'text': 'barbarbar',
|
|
'description': 'this is <b>html</b>'}]
|
|
resp = client.get('/ajax/search/%s/?q=foo' % cell.pk, status=200)
|
|
assert resp.content.count('<li>') == 1
|
|
assert '<li><a href="http://test">barbarbar</a>' in resp.content
|
|
assert 'this is <b>html</b>' in resp.content
|
|
|
|
|
|
def test_search_global_context(app):
|
|
with SearchServices(SEARCH_SERVICES):
|
|
page = Page(title='Search', slug='search_page', template_name='standard')
|
|
page.save()
|
|
cell = SearchCell(page=page, placeholder='content', order=0)
|
|
cell._search_service = 'search1'
|
|
cell.save()
|
|
assert cell.varname == ''
|
|
|
|
cell.slug = 'search-item'
|
|
cell.save()
|
|
assert cell.varname == 'search_item'
|
|
|
|
jsoncell = JsonCell(page=page, placeholder='content', order=0)
|
|
jsoncell.url = 'http://www.example.net/search/[search_item]/'
|
|
jsoncell.save()
|
|
url = reverse('combo-public-ajax-page-cell',
|
|
kwargs={'page_pk': page.id, 'cell_reference': jsoncell.get_reference()}) + \
|
|
'?search_item=foo'
|
|
|
|
with mock.patch('combo.utils.requests.get') as requests_get:
|
|
data = {'data': []}
|
|
requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200)
|
|
resp = app.get(url)
|
|
assert requests_get.call_args[0][0] == 'http://www.example.net/search/foo/'
|