Commit 8ca284fa by Vedran Karacic Committed by Vedran Karačić

SDN check exception handling.

parent 3e158fb0
...@@ -3,6 +3,7 @@ import mock ...@@ -3,6 +3,7 @@ import mock
import ddt import ddt
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from requests.exceptions import HTTPError, Timeout
from rest_framework import status from rest_framework import status
from ecommerce.core.models import User from ecommerce.core.models import User
...@@ -38,12 +39,21 @@ class SDNCheckViewSetTests(TestCase): ...@@ -38,12 +39,21 @@ class SDNCheckViewSetTests(TestCase):
self.client.logout() self.client.logout()
self.assertEqual(self.make_request().status_code, status.HTTP_401_UNAUTHORIZED) self.assertEqual(self.make_request().status_code, status.HTTP_401_UNAUTHORIZED)
def test_sdn_check_match(self): @ddt.data(0, 1)
def test_sdn_check_match(self, hits):
"""Verify the endpoint returns the number of hits SDN check made.""" """Verify the endpoint returns the number of hits SDN check made."""
with mock.patch.object(SDNClient, 'search') as sdn_validator_mock: with mock.patch.object(SDNClient, 'search') as sdn_validator_mock:
with mock.patch.object(User, 'deactivate_account') as deactivate_account_mock: with mock.patch.object(User, 'deactivate_account') as deactivate_account_mock:
sdn_validator_mock.return_value = {'total': 1} sdn_validator_mock.return_value = {'total': hits}
deactivate_account_mock.return_value = True deactivate_account_mock.return_value = True
response = self.make_request() response = self.make_request()
self.assertEqual(json.loads(response.content)['hits'], 1) self.assertEqual(json.loads(response.content)['hits'], hits)
self.assertTrue(sdn_validator_mock.called) self.assertTrue(sdn_validator_mock.called)
@ddt.data(HTTPError, Timeout)
def test_sdn_check_error(self, side_effect):
"""Verify zero hits are returned when an exception happens."""
with mock.patch.object(SDNClient, 'search') as sdn_validator_mock:
sdn_validator_mock.side_effect = side_effect
response = self.make_request()
self.assertEqual(json.loads(response.content)['hits'], 0)
"""API endpoint for performing an SDN check on users.""" """API endpoint for performing an SDN check on users."""
from requests.exceptions import HTTPError, Timeout
from rest_framework.views import APIView from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response from rest_framework.response import Response
...@@ -27,15 +28,20 @@ class SDNCheckViewSet(APIView): ...@@ -27,15 +28,20 @@ class SDNCheckViewSet(APIView):
api_key=site_configuration.sdn_api_key, api_key=site_configuration.sdn_api_key,
sdn_list=site_configuration.sdn_api_list sdn_list=site_configuration.sdn_api_list
) )
response = sdn_check.search(name, country) try:
hits = response['total'] response = sdn_check.search(name, country)
if hits > 0: hits = response['total']
sdn_check.deactivate_user( if hits > 0:
request.user, sdn_check.deactivate_user(
request.site.siteconfiguration, request.user,
name, request.site.siteconfiguration,
country, name,
response country,
) response
)
except (HTTPError, Timeout):
# If the SDN API endpoint is down or times out
# the user is allowed to make the purchase.
pass
return Response({'hits': hits}) return Response({'hits': hits})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment