Skip to content
Snippets Groups Projects
Commit bdb35931 authored by Bastien Abadie's avatar Bastien Abadie
Browse files

Merge branch 'no-corpora-no-results' into 'master'

Return no search results when there are no readable corpora

See merge request !650
parents ff4a1647 a5a95cf1
No related branches found
No related tags found
1 merge request!650Return no search results when there are no readable corpora
from unittest.mock import patch
from django.urls import reverse
from django.contrib.auth.models import AnonymousUser
from rest_framework import status
from arkindex.project.tests import FixtureAPITestCase
from arkindex.documents.models import Corpus
......@@ -60,3 +61,39 @@ class TestSearchApi(FixtureAPITestCase):
for params in self.forbidden_params:
response = self.client.get(reverse('api:element-search'), params)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
@patch('arkindex.project.mixins.ESQuerySet')
def test_element_search_no_corpora(self, esqs_mock):
"""
Test search as a user without access to any corpora returns nothing without asking ES
"""
self.corpus.public = False
self.corpus.save()
self.assertFalse(Corpus.objects.readable(AnonymousUser()).exists())
response = self.client.get(reverse('api:element-search'), {'q': 'abc'})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertDictEqual(response.json(), {
'count': 0,
'next': None,
'previous': None,
'number': 1,
'results': []
})
@patch('arkindex.project.mixins.ESQuerySet')
def test_entity_search_no_corpora(self, esqs_mock):
"""
Test search as a user without access to any corpora returns nothing without asking ES
"""
self.corpus.public = False
self.corpus.save()
self.assertFalse(Corpus.objects.readable(AnonymousUser()).exists())
response = self.client.get(reverse('api:entity-search'), {'q': 'abc'})
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertDictEqual(response.json(), {
'count': 0,
'next': None,
'previous': None,
'number': 1,
'results': []
})
......@@ -54,6 +54,9 @@ class SearchAPIMixin(CorpusACLMixin):
Corpus.objects.readable(self.request.user).values_list('id', flat=True),
))
if not query['corpora_ids']:
return []
query.pop('corpus_id', None)
search = self.get_search(**query)
return ESQuerySet(self.post_process, search)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment