Skip to content

Commit 56c7f07

Browse files
committed
feat: add LmsApiClient.create_enterprise_admin_user() method
1 parent f71f865 commit 56c7f07

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

enterprise_access/apps/api_client/lms_client.py

+35
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class LmsApiClient(BaseOAuthClient):
5050
enterprise_customer_endpoint = enterprise_api_base_url + 'enterprise-customer/'
5151
pending_enterprise_learner_endpoint = enterprise_api_base_url + 'pending-enterprise-learner/'
5252
enterprise_group_membership_endpoint = enterprise_api_base_url + 'enterprise-group/'
53+
pending_enterprise_admin_endpoint = enterprise_api_base_url + 'pending-enterprise-admin/'
5354

5455
def enterprise_customer_url(self, enterprise_customer_uuid):
5556
return os.path.join(
@@ -194,6 +195,40 @@ def get_enterprise_admin_users(self, enterprise_customer_uuid):
194195

195196
return results
196197

198+
def create_enterprise_admin_user(self, enterprise_customer_uuid, user_email):
199+
"""
200+
Creates a new enterprise pending admin record.
201+
202+
Arguments:
203+
enterprise_customer_uuid (UUID): UUID of the enterprise customer.
204+
user_email (string): The email address of the admin.
205+
Returns:
206+
dictionary describing the created pending admin record.
207+
"""
208+
payload = {
209+
'enterprise_customer': enterprise_customer_uuid,
210+
'user_email': user_email,
211+
}
212+
try:
213+
response = self.client.post(
214+
self.pending_enterprise_admin_endpoint,
215+
json=payload,
216+
timeout=settings.LMS_CLIENT_TIMEOUT,
217+
)
218+
response.raise_for_status()
219+
logger.info(
220+
'Successfully created pending admin record for customer %s, email %s',
221+
enterprise_customer_uuid, user_email,
222+
)
223+
payload = response.json()
224+
return payload
225+
except requests.exceptions.HTTPError:
226+
logger.exception(
227+
'Failed to create pending admin record for customer %s, email %s',
228+
enterprise_customer_uuid, user_email,
229+
)
230+
raise
231+
197232
def unlink_users_from_enterprise(self, enterprise_customer_uuid, user_emails, is_relinkable=True):
198233
"""
199234
Unlink users with the given emails from the enterprise.

enterprise_access/apps/api_client/tests/test_lms_client.py

+67
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ def test_create_enterprise_customer_data(self, mock_oauth_client, mock_json):
201201
mock_post.return_value.status_code = 201
202202

203203
client = LmsApiClient()
204+
204205
response_payload = client.create_enterprise_customer(**customer_input)
205206

206207
self.assertEqual(response_payload, mock_created_customer_payload)
@@ -215,6 +216,43 @@ def test_create_enterprise_customer_data(self, mock_oauth_client, mock_json):
215216
timeout=settings.LMS_CLIENT_TIMEOUT,
216217
)
217218

219+
@mock.patch('requests.Response.json')
220+
@mock.patch('enterprise_access.apps.api_client.base_oauth.OAuthAPIClient')
221+
def test_create_enterprise_admin_user(self, mock_oauth_client, mock_json):
222+
"""
223+
Test that we can use the LmsApiClient to create a new customer admin.
224+
"""
225+
customer_uuid = str(uuid4())
226+
227+
mock_created_admin_payload = {
228+
'id': 1,
229+
'enterprise_customer': customer_uuid,
230+
'user_email': '[email protected]',
231+
}
232+
mock_json.return_value = mock_created_admin_payload
233+
234+
mock_post = mock_oauth_client.return_value.post
235+
236+
mock_post.return_value = requests.Response()
237+
mock_post.return_value.status_code = 201
238+
239+
client = LmsApiClient()
240+
response_payload = client.create_enterprise_admin_user(
241+
customer_uuid, '[email protected]',
242+
)
243+
244+
self.assertEqual(response_payload, mock_created_admin_payload)
245+
expected_url = 'http://edx-platform.example.com/enterprise/api/v1/pending-enterprise-admin/'
246+
expected_input = {
247+
'enterprise_customer': customer_uuid,
248+
'user_email': '[email protected]',
249+
}
250+
mock_post.assert_called_once_with(
251+
expected_url,
252+
json=expected_input,
253+
timeout=settings.LMS_CLIENT_TIMEOUT,
254+
)
255+
218256
@mock.patch('enterprise_access.apps.api_client.base_oauth.OAuthAPIClient')
219257
def test_create_enterprise_customer_error(self, mock_oauth_client):
220258
"""
@@ -249,6 +287,35 @@ def test_create_enterprise_customer_error(self, mock_oauth_client):
249287
timeout=settings.LMS_CLIENT_TIMEOUT,
250288
)
251289

290+
@mock.patch('enterprise_access.apps.api_client.base_oauth.OAuthAPIClient')
291+
def test_create_enterprise_admin_error(self, mock_oauth_client):
292+
"""
293+
Tests that we raise an exception appropriately when creating a
294+
new customer admin record with the LmsApiClient().
295+
"""
296+
customer_uuid = str(uuid4())
297+
mock_post = mock_oauth_client.return_value.post
298+
299+
mock_post.side_effect = requests.exceptions.HTTPError('whoopsie')
300+
mock_post.return_value.status_code = 400
301+
302+
client = LmsApiClient()
303+
with self.assertRaises(requests.exceptions.HTTPError):
304+
client.create_enterprise_admin_user(
305+
customer_uuid, '[email protected]',
306+
)
307+
308+
expected_url = 'http://edx-platform.example.com/enterprise/api/v1/pending-enterprise-admin/'
309+
expected_input = {
310+
'enterprise_customer': customer_uuid,
311+
'user_email': '[email protected]',
312+
}
313+
mock_post.assert_called_once_with(
314+
expected_url,
315+
json=expected_input,
316+
timeout=settings.LMS_CLIENT_TIMEOUT,
317+
)
318+
252319
@mock.patch('enterprise_access.apps.api_client.base_oauth.OAuthAPIClient')
253320
def test_unlink_users_from_enterprise(self, mock_oauth_client):
254321
"""

0 commit comments

Comments
 (0)