Browse Source

feat(api): Add support for `query` parameter to recent searches list endpoint (SEN-405)

The query parameter will just filter to any recent searches containing that value.
Dan Fuller 6 years ago
parent
commit
3aad664e7b

+ 10 - 3
src/sentry/api/endpoints/organization_recent_searches.py

@@ -59,10 +59,17 @@ class OrganizationRecentSearchesEndpoint(OrganizationEndpoint):
                 status=400,
             )
 
+        query_kwargs = {
+            'organization': organization,
+            'user': request.user,
+            'type': search_type,
+        }
+
+        if 'query' in request.GET:
+            query_kwargs['query__icontains'] = request.GET['query']
+
         recent_searches = list(RecentSearch.objects.filter(
-            organization=organization,
-            user=request.user,
-            type=search_type,
+            **query_kwargs
         ).order_by('-last_seen')[:limit])
 
         return Response(serialize(recent_searches, request.user))

+ 38 - 2
tests/sentry/api/endpoints/test_organization_recent_searches.py

@@ -22,9 +22,12 @@ class RecentSearchesListTest(APITestCase):
     def user(self):
         return self.create_user('test@test.com')
 
-    def check_results(self, expected, search_type):
+    def check_results(self, expected, search_type, query=None):
         self.login_as(user=self.user)
-        response = self.get_valid_response(self.organization.slug, type=search_type.value)
+        kwargs = {}
+        if query:
+            kwargs['query'] = query
+        response = self.get_valid_response(self.organization.slug, type=search_type.value, **kwargs)
         assert response.data == serialize(expected)
 
     def test_simple(self):
@@ -93,6 +96,39 @@ class RecentSearchesListTest(APITestCase):
             assert response.status_code == 400
             assert response.data['detail'].startswith(expected_error)
 
+    def test_query(self):
+        issue_recent_searches = [
+            RecentSearch.objects.create(
+                organization=self.organization,
+                user=self.user,
+                type=SearchType.ISSUE.value,
+                query='some test',
+                last_seen=timezone.now().replace(microsecond=0),
+                date_added=timezone.now().replace(microsecond=0),
+            ),
+            RecentSearch.objects.create(
+                organization=self.organization,
+                user=self.user,
+                type=SearchType.ISSUE.value,
+                query='older query',
+                last_seen=timezone.now().replace(microsecond=0) - timedelta(minutes=30),
+                date_added=timezone.now().replace(microsecond=0) - timedelta(minutes=30),
+            ),
+            RecentSearch.objects.create(
+                organization=self.organization,
+                user=self.user,
+                type=SearchType.ISSUE.value,
+                query='oldest query',
+                last_seen=timezone.now().replace(microsecond=0) - timedelta(hours=1),
+                date_added=timezone.now().replace(microsecond=0) - timedelta(hours=1),
+            ),
+        ]
+        self.check_results(
+            issue_recent_searches[1:],
+            search_type=SearchType.ISSUE,
+            query='lde'
+        )
+
 
 class RecentSearchesCreateTest(APITestCase):
     endpoint = 'sentry-api-0-organization-recent-searches'