diff --git a/doc/api.md b/doc/api.md index 56adf9ce..8b85091e 100644 --- a/doc/api.md +++ b/doc/api.md @@ -146,6 +146,24 @@ The API is served by the URL } ``` +#### Get all analyzed applications with a specific tracker found + +`GET /api/applications?tracker=` returns `JSON` + +```json +{ + "applications": [ + { + "id": 1, + "handle": "com.johnson.nett", + "app_uid": "C585E0D6274EDA2FA159542E305D2C61963BA8CC", + "source": "google" + }, + [edited] + ] +} +``` + #### Get the number of distinct applications `GET /api/applications/count` returns `JSON` diff --git a/exodus/restful_api/tests.py b/exodus/restful_api/tests.py index b8b421a3..5117a3cc 100644 --- a/exodus/restful_api/tests.py +++ b/exodus/restful_api/tests.py @@ -137,6 +137,58 @@ def test_returns_applications_with_multiple_versions(self): self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), expected_json) + def test_returns_applications_with_specific_tracker_using_option_tracker_and_short(self): + self._force_authentication() + report_with_tracker_1 = Report.objects.create(id=1234) + tracker_1 = Tracker.objects.create(name='Teemo') + tracker_2 = Tracker.objects.create(name='Analytics') + report_with_tracker_1.found_trackers.set([tracker_1.id]) + application_with_tracker_1 = Application.objects.create( + name='app_name1', + handle='handle1', + source='google1', + report=report_with_tracker_1 + ) + + report_with_no_trackers = Report.objects.create(id=1235) + Application.objects.create( + name='app_name2', + handle='handle2', + source='google2', + report=report_with_no_trackers + ) + + report_with_both_trackers = Report.objects.create(id=1236) + report_with_both_trackers.found_trackers.set([tracker_1.id, tracker_2.id]) + application_with_both_trackers = Application.objects.create( + name='app_name3', + handle='handle3', + source='google3', + report=report_with_both_trackers + ) + + expected_json = { + 'applications': [ + { + "id": application_with_tracker_1.id, + "handle": application_with_tracker_1.handle, + "app_uid": "", + "source": application_with_tracker_1.source + }, + { + "id": application_with_both_trackers.id, + "handle": application_with_both_trackers.handle, + "app_uid": "", + "source": application_with_both_trackers.source + } + ] + } + + response = self.client.get(self.PATH + f'?option=short&tracker={tracker_1.id}') + + self.assertEqual(response.status_code, 200) + self.assertEqual(response.json(), expected_json) + class RestfulApiSearchHandleDetailsTests(APITestCase): PATH = '/api/search/{}/details'.format(DUMMY_HANDLE) diff --git a/exodus/restful_api/views.py b/exodus/restful_api/views.py index 94ea4150..7acf3b70 100644 --- a/exodus/restful_api/views.py +++ b/exodus/restful_api/views.py @@ -156,7 +156,11 @@ def get_all_trackers(request): def get_all_applications(request): if request.method == 'GET': try: - applications = Application.objects.order_by('handle', '-source').distinct('handle', 'source') + if request.GET.get('tracker'): + tracker_id = request.GET.get('tracker') + applications = Application.objects.filter(report__found_trackers__id=tracker_id).order_by('handle', '-source').distinct('handle', 'source') + else: + applications = Application.objects.order_by('handle', '-source').distinct('handle', 'source') if request.GET.get('option', 'full') == 'short': serializer = ApplicationShortSerializer(applications, many=True) else: