Fix reports API test case

This commit is contained in:
Jeremy Stretch 2020-07-06 11:15:20 -04:00
parent 4ea4112490
commit e02936a44a
2 changed files with 10 additions and 9 deletions

View File

@ -217,7 +217,7 @@ class ReportViewSet(ViewSet):
""" """
# Check that the user has permission to run reports. # Check that the user has permission to run reports.
if not request.user.has_perm('extras.add_reportresult'): if not request.user.has_perm('extras.run_script'):
raise PermissionDenied("This user does not have permission to run reports.") raise PermissionDenied("This user does not have permission to run reports.")
# Retrieve and run the Report. This will create a new JobResult. # Retrieve and run the Report. This will create a new JobResult.
@ -231,7 +231,7 @@ class ReportViewSet(ViewSet):
) )
report.result = job_result report.result = job_result
serializer = serializers.ReportDetailSerializer(report) serializer = serializers.ReportDetailSerializer(report, context={'request': request})
return Response(serializer.data) return Response(serializer.data)

View File

@ -6,7 +6,7 @@ from django.utils import timezone
from rest_framework import status from rest_framework import status
from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Rack, RackGroup, RackRole, Site from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Rack, RackGroup, RackRole, Site
from extras.api.views import ScriptViewSet from extras.api.views import ReportViewSet, ScriptViewSet
from extras.models import ConfigContext, Graph, ExportTemplate, Tag from extras.models import ConfigContext, Graph, ExportTemplate, Tag
from extras.reports import Report from extras.reports import Report
from extras.scripts import BooleanVar, IntegerVar, Script, StringVar from extras.scripts import BooleanVar, IntegerVar, Script, StringVar
@ -211,28 +211,29 @@ class ConfigContextTest(APIViewTestCases.APIViewTestCase):
class ReportTest(APITestCase): class ReportTest(APITestCase):
class TestReport(Report): class TestReport(Report):
pass # The report is not actually run, we are testing that the view enqueues the job
def test_foo(self):
self.log_success(None, "Report completed")
def get_test_report(self, *args): def get_test_report(self, *args):
return self.TestReport return self.TestReport()
def setUp(self): def setUp(self):
super().setUp() super().setUp()
# Monkey-patch the API viewset's _get_script method to return our test script above # Monkey-patch the API viewset's _get_script method to return our test script above
ReportViewSet._get_report = self.get_test_report ReportViewSet._retrieve_report = self.get_test_report
def test_get_report(self): def test_get_report(self):
url = reverse('extras-api:report-detail', kwargs={'pk': None}) url = reverse('extras-api:report-detail', kwargs={'pk': None})
response = self.client.get(url, **self.header) response = self.client.get(url, **self.header)
self.assertEqual(response.data['name'], self.TestReport.__name__) self.assertEqual(response.data['name'], self.TestReport.__name__)
def test_run_report(self): def test_run_report(self):
self.add_permissions('extras.run_script')
url = reverse('extras-api:report-detail', kwargs={'pk': None}) url = reverse('extras-api:report-run', kwargs={'pk': None})
response = self.client.post(url, {}, format='json', **self.header) response = self.client.post(url, {}, format='json', **self.header)
self.assertHttpStatus(response, status.HTTP_200_OK) self.assertHttpStatus(response, status.HTTP_200_OK)