mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-17 04:32:51 -06:00
Refactor & expand search view tests
This commit is contained in:
parent
a20ccfee7e
commit
e6ccea0168
@ -1,10 +1,10 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
import django_tables2 as tables
|
import django_tables2 as tables
|
||||||
from django.conf import settings
|
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from extras.models import *
|
from extras.models import *
|
||||||
|
from netbox.constants import EMPTY_TABLE_TEXT
|
||||||
from netbox.tables import BaseTable, NetBoxTable, columns
|
from netbox.tables import BaseTable, NetBoxTable, columns
|
||||||
from .template_code import *
|
from .template_code import *
|
||||||
|
|
||||||
@ -550,7 +550,7 @@ class ScriptResultsTable(BaseTable):
|
|||||||
)
|
)
|
||||||
|
|
||||||
class Meta(BaseTable.Meta):
|
class Meta(BaseTable.Meta):
|
||||||
empty_text = _('No results found')
|
empty_text = _(EMPTY_TABLE_TEXT)
|
||||||
fields = (
|
fields = (
|
||||||
'index', 'time', 'status', 'message',
|
'index', 'time', 'status', 'message',
|
||||||
)
|
)
|
||||||
@ -581,7 +581,7 @@ class ReportResultsTable(BaseTable):
|
|||||||
)
|
)
|
||||||
|
|
||||||
class Meta(BaseTable.Meta):
|
class Meta(BaseTable.Meta):
|
||||||
empty_text = _('No results found')
|
empty_text = _(EMPTY_TABLE_TEXT)
|
||||||
fields = (
|
fields = (
|
||||||
'index', 'method', 'time', 'status', 'object', 'url', 'message',
|
'index', 'method', 'time', 'status', 'object', 'url', 'message',
|
||||||
)
|
)
|
||||||
|
@ -41,3 +41,6 @@ DEFAULT_ACTION_PERMISSIONS = {
|
|||||||
# General-purpose tokens
|
# General-purpose tokens
|
||||||
CENSOR_TOKEN = '********'
|
CENSOR_TOKEN = '********'
|
||||||
CENSOR_TOKEN_CHANGED = '***CHANGED***'
|
CENSOR_TOKEN_CHANGED = '***CHANGED***'
|
||||||
|
|
||||||
|
# Placeholder text for empty tables
|
||||||
|
EMPTY_TABLE_TEXT = 'No results found'
|
||||||
|
@ -14,6 +14,7 @@ from django_tables2.data import TableQuerysetData
|
|||||||
from core.models import ObjectType
|
from core.models import ObjectType
|
||||||
from extras.choices import *
|
from extras.choices import *
|
||||||
from extras.models import CustomField, CustomLink
|
from extras.models import CustomField, CustomLink
|
||||||
|
from netbox.constants import EMPTY_TABLE_TEXT
|
||||||
from netbox.registry import registry
|
from netbox.registry import registry
|
||||||
from netbox.tables import columns
|
from netbox.tables import columns
|
||||||
from utilities.paginator import EnhancedPaginator, get_paginate_count
|
from utilities.paginator import EnhancedPaginator, get_paginate_count
|
||||||
@ -258,7 +259,7 @@ class SearchTable(tables.Table):
|
|||||||
attrs = {
|
attrs = {
|
||||||
'class': 'table table-hover object-list',
|
'class': 'table table-hover object-list',
|
||||||
}
|
}
|
||||||
empty_text = _('No results found')
|
empty_text = _(EMPTY_TABLE_TEXT)
|
||||||
|
|
||||||
def __init__(self, data, highlight=None, **kwargs):
|
def __init__(self, data, highlight=None, **kwargs):
|
||||||
self.highlight = highlight
|
self.highlight = highlight
|
||||||
|
@ -1,24 +1,76 @@
|
|||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
|
||||||
from utilities.testing import TestCase
|
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
from django.test import override_settings
|
||||||
|
|
||||||
|
from dcim.models import Site
|
||||||
|
from netbox.constants import EMPTY_TABLE_TEXT
|
||||||
|
from netbox.search.backends import search_backend
|
||||||
|
from utilities.testing import TestCase
|
||||||
|
|
||||||
|
|
||||||
class HomeViewTestCase(TestCase):
|
class HomeViewTestCase(TestCase):
|
||||||
|
|
||||||
def test_home(self):
|
def test_home(self):
|
||||||
|
|
||||||
url = reverse('home')
|
url = reverse('home')
|
||||||
|
|
||||||
response = self.client.get(url)
|
response = self.client.get(url)
|
||||||
self.assertHttpStatus(response, 200)
|
self.assertHttpStatus(response, 200)
|
||||||
|
|
||||||
def test_search(self):
|
|
||||||
|
|
||||||
|
class SearchViewTestCase(TestCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpTestData(cls):
|
||||||
|
sites = (
|
||||||
|
Site(name='Site Alpha', slug='alpha', description='Red'),
|
||||||
|
Site(name='Site Bravo', slug='bravo', description='Red'),
|
||||||
|
Site(name='Site Charlie', slug='charlie', description='Green'),
|
||||||
|
Site(name='Site Delta', slug='delta', description='Green'),
|
||||||
|
Site(name='Site Echo', slug='echo', description='Blue'),
|
||||||
|
Site(name='Site Foxtrot', slug='foxtrot', description='Blue'),
|
||||||
|
)
|
||||||
|
Site.objects.bulk_create(sites)
|
||||||
|
search_backend.cache(sites)
|
||||||
|
|
||||||
|
def test_search(self):
|
||||||
|
url = reverse('search')
|
||||||
|
response = self.client.get(url)
|
||||||
|
self.assertHttpStatus(response, 200)
|
||||||
|
|
||||||
|
def test_search_query(self):
|
||||||
url = reverse('search')
|
url = reverse('search')
|
||||||
params = {
|
params = {
|
||||||
'q': 'foo',
|
'q': 'red',
|
||||||
}
|
}
|
||||||
|
query = urllib.parse.urlencode(params)
|
||||||
|
|
||||||
response = self.client.get('{}?{}'.format(url, urllib.parse.urlencode(params)))
|
# Test without view permission
|
||||||
|
response = self.client.get(f'{url}?{query}')
|
||||||
self.assertHttpStatus(response, 200)
|
self.assertHttpStatus(response, 200)
|
||||||
|
content = str(response.content)
|
||||||
|
self.assertIn(EMPTY_TABLE_TEXT, content)
|
||||||
|
|
||||||
|
# Add view permissions & query again. Only matching objects should be listed
|
||||||
|
self.add_permissions('dcim.view_site')
|
||||||
|
response = self.client.get(f'{url}?{query}')
|
||||||
|
self.assertHttpStatus(response, 200)
|
||||||
|
content = str(response.content)
|
||||||
|
self.assertIn('Site Alpha', content)
|
||||||
|
self.assertIn('Site Bravo', content)
|
||||||
|
self.assertNotIn('Site Charlie', content)
|
||||||
|
self.assertNotIn('Site Delta', content)
|
||||||
|
self.assertNotIn('Site Echo', content)
|
||||||
|
self.assertNotIn('Site Foxtrot', content)
|
||||||
|
|
||||||
|
@override_settings(EXEMPT_VIEW_PERMISSIONS=['*'])
|
||||||
|
def test_search_no_results(self):
|
||||||
|
url = reverse('search')
|
||||||
|
params = {
|
||||||
|
'q': 'xxxxxxxxx', # Matches nothing
|
||||||
|
}
|
||||||
|
query = urllib.parse.urlencode(params)
|
||||||
|
|
||||||
|
response = self.client.get(f'{url}?{query}')
|
||||||
|
self.assertHttpStatus(response, 200)
|
||||||
|
content = str(response.content)
|
||||||
|
self.assertIn(EMPTY_TABLE_TEXT, content)
|
||||||
|
Loading…
Reference in New Issue
Block a user