Skip to content

Commit 39af41e

Browse files
committed
feat: add LmsApiClient.create_enterprise_admin_user() method
1 parent 1ede04c commit 39af41e

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

enterprise_access/apps/api_client/lms_client.py

Lines changed: 35 additions & 0 deletions
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(
@@ -153,6 +154,40 @@ def get_enterprise_admin_users(self, enterprise_customer_uuid):
153154

154155
return results
155156

157+
def create_enterprise_admin_user(self, enterprise_customer_uuid, user_email):
158+
"""
159+
Creates a new enterprise pending admin record.
160+
161+
Arguments:
162+
enterprise_customer_uuid (UUID): UUID of the enterprise customer.
163+
user_email (string): The email address of the admin.
164+
Returns:
165+
dictionary describing the created pending admin record.
166+
"""
167+
payload = {
168+
'enterprise_customer': enterprise_customer_uuid,
169+
'user_email': user_email,
170+
}
171+
try:
172+
response = self.client.post(
173+
self.pending_enterprise_admin_endpoint,
174+
json=payload,
175+
timeout=settings.LMS_CLIENT_TIMEOUT,
176+
)
177+
response.raise_for_status()
178+
logger.info(
179+
'Successfully created pending admin record for customer %s, email %s',
180+
enterprise_customer_uuid, user_email,
181+
)
182+
payload = response.json()
183+
return payload
184+
except requests.exceptions.HTTPError:
185+
logger.exception(
186+
'Failed to create pending admin record for customer %s, email %s',
187+
enterprise_customer_uuid, user_email,
188+
)
189+
raise
190+
156191
def unlink_users_from_enterprise(self, enterprise_customer_uuid, user_emails, is_relinkable=True):
157192
"""
158193
Unlink users with the given emails from the enterprise.

enterprise_access/apps/api_client/tests/test_lms_client.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,72 @@ def test_get_enterprise_customer_data(
174174
timeout=settings.LMS_CLIENT_TIMEOUT,
175175
)
176176

177+
@mock.patch('requests.Response.json')
178+
@mock.patch('enterprise_access.apps.api_client.base_oauth.OAuthAPIClient')
179+
def test_create_enterprise_admin_user(self, mock_oauth_client, mock_json):
180+
"""
181+
Test that we can use the LmsApiClient to create a new customer admin.
182+
"""
183+
customer_uuid = str(uuid4())
184+
185+
mock_created_admin_payload = {
186+
'id': 1,
187+
'enterprise_customer': customer_uuid,
188+
'user_email': '[email protected]',
189+
}
190+
mock_json.return_value = mock_created_admin_payload
191+
192+
mock_post = mock_oauth_client.return_value.post
193+
194+
mock_post.return_value = requests.Response()
195+
mock_post.return_value.status_code = 201
196+
197+
client = LmsApiClient()
198+
response_payload = client.create_enterprise_admin_user(
199+
customer_uuid, '[email protected]',
200+
)
201+
202+
self.assertEqual(response_payload, mock_created_admin_payload)
203+
expected_url = 'http://edx-platform.example.com/enterprise/api/v1/pending-enterprise-admin/'
204+
expected_input = {
205+
'enterprise_customer': customer_uuid,
206+
'user_email': '[email protected]',
207+
}
208+
mock_post.assert_called_once_with(
209+
expected_url,
210+
json=expected_input,
211+
timeout=settings.LMS_CLIENT_TIMEOUT,
212+
)
213+
214+
@mock.patch('enterprise_access.apps.api_client.base_oauth.OAuthAPIClient')
215+
def test_create_enterprise_admin_error(self, mock_oauth_client):
216+
"""
217+
Tests that we raise an exception appropriately when creating a
218+
new customer admin record with the LmsApiClient().
219+
"""
220+
customer_uuid = str(uuid4())
221+
mock_post = mock_oauth_client.return_value.post
222+
223+
mock_post.side_effect = requests.exceptions.HTTPError('whoopsie')
224+
mock_post.return_value.status_code = 400
225+
226+
client = LmsApiClient()
227+
with self.assertRaises(requests.exceptions.HTTPError):
228+
client.create_enterprise_admin_user(
229+
customer_uuid, '[email protected]',
230+
)
231+
232+
expected_url = 'http://edx-platform.example.com/enterprise/api/v1/pending-enterprise-admin/'
233+
expected_input = {
234+
'enterprise_customer': customer_uuid,
235+
'user_email': '[email protected]',
236+
}
237+
mock_post.assert_called_once_with(
238+
expected_url,
239+
json=expected_input,
240+
timeout=settings.LMS_CLIENT_TIMEOUT,
241+
)
242+
177243
@mock.patch('enterprise_access.apps.api_client.base_oauth.OAuthAPIClient')
178244
def test_unlink_users_from_enterprise(self, mock_oauth_client):
179245
"""

0 commit comments

Comments
 (0)