Commit 343f35b3 by Jim Abramson

Merge pull request #126 from edx/jsa/xcom-396

Recover from 400 on enrollment revocation when due to mode mismatch.
parents 5a6509c5 3c20e945
......@@ -216,9 +216,16 @@ class EnrollmentFulfillmentModule(BaseFulfillmentModule):
logger.info('Successfully revoked line [%d].', line.id)
return True
else:
# check if the error / message are something we can recover from.
data = response.json()
detail = data.get('message', '(No details provided.)')
logger.error('Failed to revoke fulfillment of Line [%d]: %s', line.id, detail)
if response.status_code == 400 and "Enrollment mode mismatch" in detail:
# The user is currently enrolled in different mode than the one
# we are refunding an order for. Don't revoke that enrollment.
logger.info('Skipping revocation for line [%d]: %s', line.id, detail)
return True
else:
logger.error('Failed to revoke fulfillment of Line [%d]: %s', line.id, detail)
except Exception: # pylint: disable=broad-except
logger.exception('Failed to revoke fulfillment of Line [%d].', line.id)
......
......@@ -111,7 +111,26 @@ class EnrollmentFulfillmentModuleTests(CourseCatalogTestMixin, FulfillmentTestMi
self.assertEqual(actual, expected)
@httpretty.activate
def test_revoke_product_api_error(self):
def test_revoke_product_expected_error(self):
"""
If the Enrollment API responds with an expected error, the method should log that revocation was
bypassed, and return True.
"""
message = 'Enrollment mode mismatch: active mode=x, requested mode=y. Won\'t deactivate.'
body = '{{"message": "{}"}}'.format(message)
httpretty.register_uri(httpretty.POST, settings.ENROLLMENT_API_URL, status=400, body=body, content_type=JSON)
line = self.order.lines.first()
logger_name = 'ecommerce.extensions.fulfillment.modules'
with LogCapture(logger_name) as l:
self.assertTrue(EnrollmentFulfillmentModule().revoke_line(line))
l.check(
(logger_name, 'INFO', 'Attempting to revoke fulfillment of Line [{}]...'.format(line.id)),
(logger_name, 'INFO', 'Skipping revocation for line [%d]: %s' % (line.id, message))
)
@httpretty.activate
def test_revoke_product_unexpected_error(self):
""" If the Enrollment API responds with a non-200 status, the method should log an error and return False. """
message = 'Meh.'
body = '{{"message": "{}"}}'.format(message)
......
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