Skip to content

Commit f30eb28

Browse files
authored
Merge pull request readthedocs#6325 from stsewd/dont-error-on-wrong-input
Don't error on non existing version
2 parents dde41e8 + 4817473 commit f30eb28

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

readthedocs/search/api.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
from rest_framework.exceptions import ValidationError
77
from rest_framework.pagination import PageNumberPagination
88

9+
from readthedocs.projects.models import HTMLFile
10+
from readthedocs.search import tasks, utils
911
from readthedocs.search.faceted_search import PageSearch
10-
from readthedocs.search import utils, tasks
1112

1213

1314
log = logging.getLogger(__name__)
@@ -79,9 +80,11 @@ def get_queryset(self):
7980
kwargs['filters']['project'] = [p.slug for p in self.get_all_projects()]
8081
kwargs['filters']['version'] = self.request.query_params.get('version')
8182
if not kwargs['filters']['project']:
82-
raise ValidationError("Unable to find a project to search")
83+
log.info("Unable to find a project to search")
84+
return HTMLFile.objects.none()
8385
if not kwargs['filters']['version']:
84-
raise ValidationError("Unable to find a version to search")
86+
log.info("Unable to find a version to search")
87+
return HTMLFile.objects.none()
8588
user = self.request.user
8689
queryset = PageSearch(
8790
query=query, user=user, **kwargs

readthedocs/search/tests/test_api.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import re
2-
import pytest
32

3+
import pytest
44
from django.core.urlresolvers import reverse
55
from django_dynamic_fixture import G
66

77
from readthedocs.builds.models import Version
8-
from readthedocs.projects.models import HTMLFile
8+
from readthedocs.projects.models import HTMLFile, Project
9+
from readthedocs.search.documents import PageDocument
910
from readthedocs.search.tests.utils import (
10-
get_search_query_from_project_file,
11-
SECTION_FIELDS,
1211
DOMAIN_FIELDS,
12+
SECTION_FIELDS,
13+
get_search_query_from_project_file,
1314
)
14-
from readthedocs.search.documents import PageDocument
1515

1616

1717
@pytest.mark.django_db
@@ -224,3 +224,30 @@ def test_doc_search_subprojects(self, api_client, all_projects):
224224
# Check the link is the subproject document link
225225
document_link = subproject.get_docs_url(version_slug=version.slug)
226226
assert document_link in first_result['link']
227+
228+
def test_doc_search_unexisting_project(self, api_client):
229+
project = 'notfound'
230+
assert not Project.objects.filter(slug=project).exists()
231+
232+
search_params = {
233+
'q': 'documentation',
234+
'project': project,
235+
'version': 'latest',
236+
}
237+
resp = api_client.get(self.url, search_params)
238+
assert resp.status_code == 404
239+
240+
def test_doc_search_unexisting_version(self, api_client, project):
241+
version = 'notfound'
242+
assert not project.versions.filter(slug=version).exists()
243+
244+
search_params = {
245+
'q': 'documentation',
246+
'project': project.slug,
247+
'version': version,
248+
}
249+
resp = api_client.get(self.url, search_params)
250+
assert resp.status_code == 200
251+
252+
data = resp.data['results']
253+
assert len(data) == 0

0 commit comments

Comments
 (0)