Skip to content

Commit fa31d26

Browse files
committed
Check if filters.project is empty
1 parent c8b1ef8 commit fa31d26

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

readthedocs/search/api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ def get_queryset(self):
120120
kwargs = {'filter_by_user': False, 'filters': {}}
121121
kwargs['filters']['project'] = [p.slug for p in self.get_all_projects()]
122122
kwargs['filters']['version'] = self._get_version().slug
123+
# Check to avoid searching all projects in case project is empty.
124+
if not kwargs['filters']['project']:
125+
log.info("Unable to find a project to search")
126+
return HTMLFile.objects.none()
123127
user = self.request.user
124128
queryset = PageSearch(
125129
query=query, user=user, **kwargs

readthedocs/search/tests/test_api.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
from unittest import mock
23

34
import pytest
45
from django.urls import reverse
@@ -7,6 +8,7 @@
78
from readthedocs.builds.models import Version
89
from readthedocs.projects.constants import PUBLIC
910
from readthedocs.projects.models import HTMLFile, Project
11+
from readthedocs.search.api import PageSearchAPIView
1012
from readthedocs.search.documents import PageDocument
1113
from readthedocs.search.tests.utils import (
1214
DOMAIN_FIELDS,
@@ -263,3 +265,20 @@ def test_doc_search_unexisting_version(self, api_client, project):
263265
}
264266
resp = self.get_search(api_client, search_params)
265267
assert resp.status_code == 404
268+
269+
@mock.patch.object(PageSearchAPIView, 'get_all_projects', list)
270+
def test_get_all_projects_returns_empty_results(self, api_client, project):
271+
"""If there is a case where `get_all_projects` returns empty, we could be querying all projects."""
272+
273+
# `documentation` word is present both in `kuma` and `docs` files
274+
# and not in `pipeline`, so search with this phrase but filter through project
275+
search_params = {
276+
'q': 'documentation',
277+
'project': 'docs',
278+
'version': 'latest'
279+
}
280+
resp = self.get_search(api_client, search_params)
281+
assert resp.status_code == 200
282+
283+
data = resp.data['results']
284+
assert len(data) == 0

0 commit comments

Comments
 (0)