-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basics of business logic for customer provisioning
- Loading branch information
1 parent
b26a256
commit e17a900
Showing
5 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,6 +216,43 @@ def test_create_enterprise_customer_data(self, mock_oauth_client, mock_json): | |
timeout=settings.LMS_CLIENT_TIMEOUT, | ||
) | ||
|
||
@mock.patch('requests.Response.json') | ||
@mock.patch('enterprise_access.apps.api_client.base_oauth.OAuthAPIClient') | ||
def test_get_enterprise_pending_admin_users(self, mock_oauth_client, mock_json): | ||
""" | ||
Test that we can use the LmsApiClient to fetch existing pending admin records. | ||
""" | ||
customer_uuid = str(uuid4()) | ||
|
||
mock_response_payload_results = [{ | ||
'id': 1, | ||
'enterprise_customer': customer_uuid, | ||
'user_email': '[email protected]', | ||
}] | ||
mock_response_payload = { | ||
'count': 1, | ||
'results': mock_response_payload_results, | ||
} | ||
mock_json.return_value = mock_response_payload | ||
|
||
mock_get = mock_oauth_client.return_value.get | ||
|
||
mock_get.return_value = requests.Response() | ||
mock_get.return_value.status_code = 200 | ||
|
||
client = LmsApiClient() | ||
response_payload = client.get_enterprise_pending_admin_users(customer_uuid) | ||
|
||
self.assertEqual(response_payload, mock_response_payload_results) | ||
expected_url = ( | ||
'http://edx-platform.example.com/enterprise/api/v1/pending-enterprise-admin/' | ||
f'?enterprise_customer={customer_uuid}' | ||
) | ||
mock_get.assert_called_once_with( | ||
expected_url, | ||
timeout=settings.LMS_CLIENT_TIMEOUT, | ||
) | ||
|
||
@mock.patch('requests.Response.json') | ||
@mock.patch('enterprise_access.apps.api_client.base_oauth.OAuthAPIClient') | ||
def test_create_enterprise_admin_user(self, mock_oauth_client, mock_json): | ||
|
@@ -316,6 +353,31 @@ def test_create_enterprise_admin_error(self, mock_oauth_client): | |
timeout=settings.LMS_CLIENT_TIMEOUT, | ||
) | ||
|
||
@mock.patch('enterprise_access.apps.api_client.base_oauth.OAuthAPIClient') | ||
def test_get_enterprise_pending_admin_error(self, mock_oauth_client): | ||
""" | ||
Tests that we raise an exception appropriately when listing pending | ||
admin records with the LmsApiClient(). | ||
""" | ||
customer_uuid = str(uuid4()) | ||
mock_get = mock_oauth_client.return_value.get | ||
|
||
mock_get.side_effect = requests.exceptions.HTTPError('whoopsie') | ||
mock_get.return_value.status_code = 400 | ||
|
||
client = LmsApiClient() | ||
with self.assertRaises(requests.exceptions.HTTPError): | ||
client.get_enterprise_pending_admin_users(customer_uuid) | ||
|
||
expected_url = ( | ||
'http://edx-platform.example.com/enterprise/api/v1/pending-enterprise-admin/' | ||
f'?enterprise_customer={customer_uuid}' | ||
) | ||
mock_get.assert_called_once_with( | ||
expected_url, | ||
timeout=settings.LMS_CLIENT_TIMEOUT, | ||
) | ||
|
||
@mock.patch('enterprise_access.apps.api_client.base_oauth.OAuthAPIClient') | ||
def test_unlink_users_from_enterprise(self, mock_oauth_client): | ||
""" | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
""" | ||
Python API for provisioning operations. | ||
""" | ||
import logging | ||
|
||
from ..api_client.lms_client import LmsApiClient | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_or_create_enterprise_customer(*, name, slug, country, **kwargs): | ||
""" | ||
Get or creates an enterprise customer with the provided arguments. | ||
""" | ||
client = LmsApiClient() | ||
existing_customer = client.get_enterprise_customer_data(enterprise_customer_slug=slug) | ||
if existing_customer: | ||
logger.info('Provisioning: enterprise_customer slug %s already exists', slug) | ||
return existing_customer | ||
|
||
created_customer = client.create_enterprise_customer( | ||
name=name, slug=slug, country=country, **kwargs, | ||
) | ||
logger.info('Provisioning: created enterprise customer with slug %s', slug) | ||
|
||
|
||
def get_or_create_enterprise_admin_users(enterprise_customer_uuid, user_emails): | ||
""" | ||
Creates pending admin records from the given ``user_email`` for the customer | ||
identified by ``enterprise_customer_uuid``. | ||
""" | ||
client = LmsApiClient() | ||
existing_admins = client.get_enterprise_admin_users(enterprise_customer_uuid) | ||
existing_admin_emails = {record['email'] for record in existing_admins} | ||
logger.info( | ||
'Provisioning: customer %s has existing admin emails %s', | ||
enterprise_customer_uuid, | ||
existing_admin_emails, | ||
) | ||
|
||
existing_pending_admins = client.get_enterprise_pending_admin_users(enterprise_customer_uuid) | ||
existing_pending_admin_emails = {record['user_email'] for record in existing_pending_admins} | ||
logger.info( | ||
'Provisioning: customer %s has existing pending admin emails %s', | ||
enterprise_customer_uuid, | ||
existing_pending_admin_emails, | ||
) | ||
|
||
user_emails_to_create = list( | ||
(set(user_emails) - existing_admin_emails) - existing_pending_admin_emails | ||
) | ||
|
||
created_admins = [] | ||
for user_email in user_emails_to_create: | ||
created_admins.append( | ||
client.create_enterprise_admin_user(enterprise_customer_uuid, user_email) | ||
) | ||
logger.info( | ||
'Provisioning: created admin %s for customer %s', | ||
user_email, | ||
enterprise_customer_uuid, | ||
) | ||
|
||
return created_admins + existing_pending_admins |