Skip to content

Commit

Permalink
feat: add enterprise allocation cancellation (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnagro authored Jul 10, 2023
2 parents af75f40 + b343ec1 commit e5fe0da
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Unreleased
~~~~~~~~~~
* Nothing unreleased

[0.6.1]
~~~~~~~
* Adds an enterprise allocation cancellation method

[0.6.0]
~~~~~~~
* Adds optional arg to create_enterprise_allocation() to either raise (current/default behavior),
Expand Down
2 changes: 1 addition & 1 deletion getsmarter_api_clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Clients to interact with GetSmarter APIs.
"""

__version__ = '0.6.0'
__version__ = '0.6.1'
32 changes: 32 additions & 0 deletions getsmarter_api_clients/geag.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,35 @@ def create_enterprise_allocation(
if should_raise:
raise
return response

def cancel_enterprise_allocation(
self,
order_uuid,
should_raise=True,
):
"""
Cancel an enterprise_allocation (enrollment) through GEAG.
:Parameters:
- `order_uuid` (str): The order UUID of the allocation
- `should_raise` (boolean): Should exceptions be re-raised
"""
url = f'{self.api_url}/enterprise_allocations/cancel'

payload = {
'orderUuid': str(order_uuid),
}

response = self.post(url, json=payload)
try:
response.raise_for_status()
except HTTPError:
message = (
f'Allocation cancelation failed for {order_uuid} '
f'with reasons: {response.text}, '
f'with payload: {payload}'
)
logger.error(message)
if should_raise:
raise
return response
48 changes: 48 additions & 0 deletions tests/getsmarter_api_clients/test_geag.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
from datetime import datetime
from unittest import mock
from uuid import uuid4

import ddt
import pytz
Expand Down Expand Up @@ -53,13 +54,15 @@ class GetSmarterEnterpriseApiClientTests(BaseOAuthApiClientTests):
'work_experience': 'None',
'org_id': '12KJ2j9js0',
}
ENTERPRISE_ALLOCATION_ORDER_UUID = str(uuid4())

def setUp(self):
super().setUp()

self.terms_url = f'{self.api_url}/terms'
self.allocations_url = f'{self.api_url}/allocations'
self.enterprise_allocations_url = f'{self.api_url}/enterprise_allocations'
self.enterprise_allocations_cancellation_url = f'{self.api_url}/enterprise_allocations/cancel'

self.tiered_cache_patcher = mock.patch('getsmarter_api_clients.oauth.TieredCache')
self.mock_tiered_cache = self.tiered_cache_patcher.start()
Expand Down Expand Up @@ -220,3 +223,48 @@ def test_create_enterprise_allocation_error_response(self, should_raise):
)
self.assertEqual(error_payload, response.json())
self.assertEqual(400, response.status_code)

@responses.activate
def test_cancel_enterprise_allocation(self):
responses.add(
responses.POST,
self.enterprise_allocations_cancellation_url,
status=204,
)
client = GetSmarterEnterpriseApiClient(**self.mock_constructor_args)

client.cancel_enterprise_allocation(self.ENTERPRISE_ALLOCATION_ORDER_UUID)

expected_payload = {
'orderUuid': self.ENTERPRISE_ALLOCATION_ORDER_UUID,
}

self.assertEqual(len(responses.calls), 1)
self.assertEqual(responses.calls[0].request.url, self.enterprise_allocations_cancellation_url)
self.assertDictEqual(ast.literal_eval(responses.calls[0].request.body.decode('utf-8')), expected_payload)

@responses.activate
@ddt.data(True, False)
def test_cancel_enterprise_allocation_error_response(self, should_raise):
error_payload = {'error': 'the workers are going home'}
responses.add(
responses.POST,
self.enterprise_allocations_cancellation_url,
status=400,
body=json.dumps(error_payload),
)
client = GetSmarterEnterpriseApiClient(**self.mock_constructor_args)

if should_raise:
with self.assertRaises(HTTPError):
response = client.cancel_enterprise_allocation(
self.ENTERPRISE_ALLOCATION_ORDER_UUID,
should_raise=should_raise,
)
else:
response = client.cancel_enterprise_allocation(
self.ENTERPRISE_ALLOCATION_ORDER_UUID,
should_raise=should_raise,
)
self.assertEqual(error_payload, response.json())
self.assertEqual(400, response.status_code)

0 comments on commit e5fe0da

Please sign in to comment.