From d965a2dfc3dacf942bc5cd72429dde36dd668072 Mon Sep 17 00:00:00 2001 From: Roman P Date: Thu, 11 Jul 2019 16:55:21 +0400 Subject: [PATCH] Refactor DeviceCertView Closes #267 --- backend/device_registry/api_views.py | 17 ++++------------- backend/device_registry/models.py | 7 ------- .../templates/device_info_security.html | 4 ++-- backend/device_registry/tests/test_api.py | 7 ------- backend/device_registry/urls.py | 1 + 5 files changed, 7 insertions(+), 29 deletions(-) diff --git a/backend/device_registry/api_views.py b/backend/device_registry/api_views.py index cb21a7577..1b67a8ff3 100644 --- a/backend/device_registry/api_views.py +++ b/backend/device_registry/api_views.py @@ -136,7 +136,7 @@ def post(self, request, *args, **kwargs): class DeviceCertView(APIView): """ - Returns a device certificate from the database. + Return a device certificate from the database. """ permission_classes = [AllowAny] @@ -146,18 +146,9 @@ def get(self, request, *args, **kwargs): except ObjectDoesNotExist: return Response('Device not found', status=status.HTTP_404_NOT_FOUND) - if 'format' in request.GET: - return Response({ - 'certificate': device.certificate, - 'certificate_expires': device.certificate_expires, - 'is_expired': - device.certificate_expires < timezone.now() if device.certificate_expires is not None else False, - 'device_id': device.device_id, - }) - else: - response = HttpResponse(device.certificate, content_type='application/x-pem-file') - response['Content-Disposition'] = 'attachment; filename={}.crt'.format(device.device_id) - return response + response = HttpResponse(device.certificate, content_type='application/x-pem-file') + response['Content-Disposition'] = 'attachment; filename={}.crt'.format(device.device_id) + return response class DeviceIDView(APIView): diff --git a/backend/device_registry/models.py b/backend/device_registry/models.py index aa5397363..c7745a861 100644 --- a/backend/device_registry/models.py +++ b/backend/device_registry/models.py @@ -112,13 +112,6 @@ def get_cert_expiration_date(self): except ValueError: pass - def get_cert_url(self): - if settings.IS_DEV: - cert_url = f'http://localhost:8001/api/v0.2/device-cert/{self.device_id}' - else: - cert_url = f'https://api.wott.io/v0.2/device-cert/{self.device_id}' - return cert_url - @property def actions_count(self): if self.firewallstate.policy == FirewallState.POLICY_ENABLED_ALLOW: diff --git a/backend/device_registry/templates/device_info_security.html b/backend/device_registry/templates/device_info_security.html index 1717bb339..704aa64cc 100644 --- a/backend/device_registry/templates/device_info_security.html +++ b/backend/device_registry/templates/device_info_security.html @@ -207,11 +207,11 @@

Security

{% endif %} - {% if not is_expired %} + {% if not is_expired %} Download Certificate - {{ object.device_id }}.crt + {{ object.device_id }}.crt diff --git a/backend/device_registry/tests/test_api.py b/backend/device_registry/tests/test_api.py index 3e0385e4f..50622affe 100644 --- a/backend/device_registry/tests/test_api.py +++ b/backend/device_registry/tests/test_api.py @@ -59,13 +59,6 @@ def test_simple_get(self): self.assertIsInstance(response.content, bytes) self.assertEqual(response.content, TEST_CERT.encode('utf8')) - def test_get_with_format(self): - response = self.client.get(self.url + '?format') - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertIsInstance(response.data, dict) - self.assertDictEqual(response.data, {'certificate': TEST_CERT, 'certificate_expires': self.expires, - 'is_expired': False, 'device_id': self.device.device_id}) - class DeviceIDViewTest(APITestCase): def setUp(self): diff --git a/backend/device_registry/urls.py b/backend/device_registry/urls.py index 1e01871e9..e1b7e2a85 100644 --- a/backend/device_registry/urls.py +++ b/backend/device_registry/urls.py @@ -104,4 +104,5 @@ api_views.autocomplete_tags, name='ajax-tags-autocomplete', ), + path('devices/device-cert//', api_views.DeviceCertView.as_view(), name='download_device_cert') ]