Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
ecommerce
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
ecommerce
Commits
8ca284fa
Commit
8ca284fa
authored
Feb 13, 2017
by
Vedran Karacic
Committed by
Vedran Karačić
Feb 14, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SDN check exception handling.
parent
3e158fb0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
13 deletions
+29
-13
ecommerce/extensions/api/v2/tests/views/test_sdn.py
+13
-3
ecommerce/extensions/api/v2/views/sdn.py
+16
-10
No files found.
ecommerce/extensions/api/v2/tests/views/test_sdn.py
View file @
8ca284fa
...
@@ -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
)
ecommerce/extensions/api/v2/views/sdn.py
View file @
8ca284fa
"""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
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment