Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
Refactor DeviceCertView
Browse files Browse the repository at this point in the history
Closes #267
  • Loading branch information
pythonpro committed Jul 11, 2019
1 parent 785847b commit c2990db
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 29 deletions.
17 changes: 4 additions & 13 deletions backend/device_registry/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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):
Expand Down
7 changes: 0 additions & 7 deletions backend/device_registry/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions backend/device_registry/templates/device_info_security.html
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ <h4 class="tab-title">Security</h4>
{% endif %}
</td>
</tr>
{% if not is_expired %}
{% if not is_expired %}
<tr>
<th scope="row">Download Certificate</th>
<td>
<a href="{{ object.get_cert_url }}">{{ object.device_id }}.crt</a>
<a href="{% url 'download_device_cert' object.device_id %}">{{ object.device_id }}.crt</a>
</td>
</tr>
<tr>
Expand Down
7 changes: 0 additions & 7 deletions backend/device_registry/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions backend/device_registry/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,6 @@
api_views.autocomplete_tags,
name='ajax-tags-autocomplete',
),
path('devices/device-cert/<str:device_id>/'.format(api_version), api_views.DeviceCertView.as_view(),
name='download_device_cert'),
]

0 comments on commit c2990db

Please sign in to comment.