From 481747347a2d8a3f89958920db12cdd85032b154 Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Wed, 23 Oct 2019 18:41:43 -0500 Subject: [PATCH] Don't error on non existing version Return an empty response instead. Related sentry issue https://sentry.io/organizations/read-the-docs/issues/902771096/?project=148442&query=is%3Aunresolved++search --- readthedocs/search/api.py | 9 ++++--- readthedocs/search/tests/test_api.py | 37 ++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/readthedocs/search/api.py b/readthedocs/search/api.py index b167ff61cc7..ea27caa49a2 100644 --- a/readthedocs/search/api.py +++ b/readthedocs/search/api.py @@ -6,8 +6,9 @@ from rest_framework.exceptions import ValidationError from rest_framework.pagination import PageNumberPagination +from readthedocs.projects.models import HTMLFile +from readthedocs.search import tasks, utils from readthedocs.search.faceted_search import PageSearch -from readthedocs.search import utils, tasks log = logging.getLogger(__name__) @@ -79,9 +80,11 @@ def get_queryset(self): kwargs['filters']['project'] = [p.slug for p in self.get_all_projects()] kwargs['filters']['version'] = self.request.query_params.get('version') if not kwargs['filters']['project']: - raise ValidationError("Unable to find a project to search") + log.info("Unable to find a project to search") + return HTMLFile.objects.none() if not kwargs['filters']['version']: - raise ValidationError("Unable to find a version to search") + log.info("Unable to find a version to search") + return HTMLFile.objects.none() user = self.request.user queryset = PageSearch( query=query, user=user, **kwargs diff --git a/readthedocs/search/tests/test_api.py b/readthedocs/search/tests/test_api.py index dd57dd3e992..f0cb4633671 100644 --- a/readthedocs/search/tests/test_api.py +++ b/readthedocs/search/tests/test_api.py @@ -1,17 +1,17 @@ import re -import pytest +import pytest from django.core.urlresolvers import reverse from django_dynamic_fixture import G from readthedocs.builds.models import Version -from readthedocs.projects.models import HTMLFile +from readthedocs.projects.models import HTMLFile, Project +from readthedocs.search.documents import PageDocument from readthedocs.search.tests.utils import ( - get_search_query_from_project_file, - SECTION_FIELDS, DOMAIN_FIELDS, + SECTION_FIELDS, + get_search_query_from_project_file, ) -from readthedocs.search.documents import PageDocument @pytest.mark.django_db @@ -224,3 +224,30 @@ def test_doc_search_subprojects(self, api_client, all_projects): # Check the link is the subproject document link document_link = subproject.get_docs_url(version_slug=version.slug) assert document_link in first_result['link'] + + def test_doc_search_unexisting_project(self, api_client): + project = 'notfound' + assert not Project.objects.filter(slug=project).exists() + + search_params = { + 'q': 'documentation', + 'project': project, + 'version': 'latest', + } + resp = api_client.get(self.url, search_params) + assert resp.status_code == 404 + + def test_doc_search_unexisting_version(self, api_client, project): + version = 'notfound' + assert not project.versions.filter(slug=version).exists() + + search_params = { + 'q': 'documentation', + 'project': project.slug, + 'version': version, + } + resp = api_client.get(self.url, search_params) + assert resp.status_code == 200 + + data = resp.data['results'] + assert len(data) == 0