Skip to content

Commit

Permalink
Add data masking for the remainder CRUD functions. Closes #8.
Browse files Browse the repository at this point in the history
  • Loading branch information
chambridge committed Sep 12, 2017
1 parent 066732d commit b0a50f6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
14 changes: 14 additions & 0 deletions quipucords/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from django.test import TestCase
from django.core.urlresolvers import reverse
from rest_framework import status
from . import models
from . import vault

Expand Down Expand Up @@ -46,6 +47,19 @@ def test_hostcred_creation(self):
host_cred = self.create_hostcredential()
self.assertTrue(isinstance(host_cred, models.HostCredential))

def test_hostcred_create(self):
"""
Ensure we can create a new host credential object via API.
"""
url = reverse("hostcred-list")
data = {'name': 'cred1',
'username': 'user1',
'password': 'pass1'}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(models.HostCredential.objects.count(), 1)
self.assertEqual(models.HostCredential.objects.get().name, 'cred1')

def test_hostcred_list_view(self):
"""Tests the list view set of the HostCredential API"""
url = reverse("hostcred-list")
Expand Down
21 changes: 21 additions & 0 deletions quipucords/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""Describes the views associatd with the API models"""

from django.shortcuts import get_object_or_404
from rest_framework import status
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from api.serializers import CredentialSerializer, HostCredentialSerializer
Expand Down Expand Up @@ -52,8 +53,28 @@ def list(self, request): # pylint: disable=unused-argument
cred = mask_credential(cred)
return Response(serializer.data)

def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
cred = mask_credential(serializer.data)
return Response(cred, status=status.HTTP_201_CREATED,
headers=headers)

def retrieve(self, request, pk=None): # pylint: disable=unused-argument
host_cred = get_object_or_404(self.queryset, pk=pk)
serializer = HostCredentialSerializer(host_cred)
cred = mask_credential(serializer.data)
return Response(cred)

# pylint: disable=unused-argument
def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data,
partial=partial)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
cred = mask_credential(serializer.data)
return Response(cred)

0 comments on commit b0a50f6

Please sign in to comment.