def test_create_pending_enterprise_user_rate_limited(self, mock_oauth_client): """ Verify the ``create_pending_enterprise_user`` method retries on a 429 response code. """ rate_limited_response = MockResponse({'detail': 'Rate limited'}, 429) # Mock out a few rate-limited response and one good from the lms mock_oauth_client().post.side_effect = [ rate_limited_response, rate_limited_response, rate_limited_response, MockResponse({'detail': 'Good Request'}, 201), ] EnterpriseApiClient().create_pending_enterprise_user(self.uuid, self.user_email) assert mock_oauth_client().post.call_count == 4
def test_create_pending_enterprise_user_rate_with_error_retries( self, mock_oauth_client): """ Verify the ``create_pending_enterprise_user`` method retries on a non 429 error code """ error_response = MockResponse({'detail': '500 Internal Server'}, 500) # Mock out a few rate-limited response and one good from the lms mock_oauth_client().post.side_effect = [ error_response, error_response, MockResponse({'detail': 'Good Request'}, 201), ] EnterpriseApiClient().create_pending_enterprise_user( self.uuid, self.user_email) assert mock_oauth_client().post.call_count == 3
def test_create_pending_enterprise_users_http_error( self, mock_oauth_client): """ Verify the ``create_pending_enterprise_users`` method does not raise an exception for successful requests. """ # Mock out the response from the lms mock_oauth_client.return_value.post.return_value = MockResponse( {'detail': 'Bad Request'}, 400, content=b'error response', ) user_emails = [ '*****@*****.**', '*****@*****.**', '*****@*****.**', ] enterprise_client = EnterpriseApiClient() with self.assertRaises(requests.exceptions.HTTPError): response = enterprise_client.create_pending_enterprise_users( self.uuid, user_emails) mock_oauth_client.return_value.post.assert_called_once_with( enterprise_client.pending_enterprise_learner_endpoint, json=[{ 'enterprise_customer': self.uuid, 'user_email': user_email } for user_email in user_emails], ) assert response.status_code == 400 assert response.content == 'error response'
def test_create_pending_enterprise_user_logs(self, mock_oauth_client, mock_logger): """ Verify the ``create_pending_enterprise_user`` method logs an error for a status code of >=400. """ # Mock out the response from the lms mock_oauth_client().post.return_value = MockResponse({'detail': 'Bad Request'}, 400) EnterpriseApiClient().create_pending_enterprise_user(self.uuid, self.user_email) mock_logger.error.assert_called_once()
def test_create_pending_enterprise_user_successful(self, mock_oauth_client, mock_logger): """ Verify the ``create_pending_enterprise_user`` method does not retry or log an error on success """ # Mock out the response from the lms mock_oauth_client().post.return_value = MockResponse( {'detail': 'Good Request'}, 201) EnterpriseApiClient().create_pending_enterprise_user( self.uuid, self.user_email) assert mock_oauth_client().post.call_count == 1 mock_logger.error.assert_not_called()
def test_create_pending_enterprise_user_rate_with_error_retries_and_rate_limiting( self, mock_oauth_client): """ Verify the ``create_pending_enterprise_user`` method has 3 error retries if a rate limited error occurs """ error_response = MockResponse({'detail': '500 Internal Server'}, 500) rate_limited_response = MockResponse({'detail': 'Rate limited'}, 429) # Mock out a few rate-limited response and one good from the lms mock_oauth_client().post.side_effect = [ error_response, error_response, rate_limited_response, error_response, error_response, error_response, error_response, MockResponse({'detail': 'Good Request'}, 201), ] EnterpriseApiClient().create_pending_enterprise_user( self.uuid, self.user_email) assert mock_oauth_client().post.call_count == 6
def test_revoke_course_enrollments_for_user_with_error(self, mock_oauth_client, mock_logger): """ Verify the ``update_course_enrollment_mode_for_user`` method logs an error for a status code of >=400. """ # Mock out the response from the lms mock_oauth_client().post.return_value = MockResponse({'detail': 'Bad Request'}, 400) mock_oauth_client().post.return_value.content = 'error response' EnterpriseApiClient().revoke_course_enrollments_for_user( user_id=self.user_id, enterprise_id=self.uuid, ) mock_logger.error.assert_called_once()