Raise error if no search term provided for search endpoint (#5441)

- Also adjust unit testing
This commit is contained in:
Oliver 2023-08-14 15:39:20 +10:00 committed by GitHub
parent 93e4dadb49
commit 5731b88499
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -287,6 +287,11 @@ class APISearchView(APIView):
'offset': 0,
}
if 'search' not in data:
raise ValidationError({
'search': 'Search term must be provided',
})
for key, cls in self.get_result_types().items():
# Only return results which are specifically requested
if key in data:

View File

@ -316,6 +316,19 @@ class SearchTests(InvenTreeAPITestCase):
'sales_order',
]
def test_empty(self):
"""Test empty request"""
data = [
'',
None,
{},
]
for d in data:
response = self.post(reverse('api-search'), d, expected_code=400)
self.assertIn('Search term must be provided', str(response.data))
def test_results(self):
"""Test individual result types"""