def test_delete_email_does_not_exist(self, req_mock): self._mock_get_vid(req_mock, 404) logger = logging.getLogger('tubular.hubspot_api') with mock.patch.object(logger, 'info') as mock_info: HubspotAPI(self.api_key).delete_user(self.test_learner) mock_info.assert_called_once_with( "No action taken because no user was found in Hubspot.")
def test_delete_no_email(self, req_mock, mock_alert): # pylint: disable=unused-argument with self.assertRaises(TypeError) as exc: HubspotAPI(self.api_key, self.test_region, self.from_address, self.alert_email).delete_user({}) self.assertIn( 'Expected an email address for user to delete, but received None.', str(exc)) mock_alert.assert_not_called()
def test_delete_server_failure_on_deletion(self, req_mock): self._mock_get_vid(req_mock, 200) self._mock_delete(req_mock, 500) with self.assertRaises(hubspot_api.HubspotException) as exc: HubspotAPI(self.api_key).delete_user(self.test_learner) self.assertIn( "Hubspot user deletion failed due to server-side (Hubspot) issues", str(exc))
def test_delete_vid_not_found(self, req_mock): self._mock_get_vid(req_mock, 200) self._mock_delete(req_mock, 404) with self.assertRaises(hubspot_api.HubspotException) as exc: HubspotAPI(self.api_key).delete_user(self.test_learner) self.assertIn( "Hubspot user deletion failed because vid doesn't match user", str(exc))
def test_delete_unauthorized_deletion(self, req_mock): self._mock_get_vid(req_mock, 200) self._mock_delete(req_mock, 401) with self.assertRaises(hubspot_api.HubspotException) as exc: HubspotAPI(self.api_key).delete_user(self.test_learner) self.assertIn( "Hubspot user deletion failed due to authorized API call", str(exc))
def test_delete_success(self, req_mock): self._mock_get_vid(req_mock, 200) self._mock_delete(req_mock, 200) logger = logging.getLogger('tubular.hubspot_api') with mock.patch.object(logger, 'info') as mock_info: HubspotAPI(self.api_key).delete_user(self.test_learner) mock_info.assert_called_once_with( "User successfully deleted from Hubspot")
def test_delete_server_failure_on_user_retrieval(self, req_mock, mock_alert): self._mock_get_vid(req_mock, 500) with self.assertRaises(hubspot_api.HubspotException) as exc: HubspotAPI(self.api_key, self.test_region, self.from_address, self.alert_email).delete_user(self.test_learner) self.assertIn("Error attempted to get user_vid from Hubspot", str(exc)) mock_alert.assert_not_called()
def test_delete_catch_all_on_deletion(self, req_mock): self._mock_get_vid(req_mock, 200) # Testing 403 as it's not a response type per the Hubspot API docs, so it doesn't have it's own error. self._mock_delete(req_mock, 403) with self.assertRaises(hubspot_api.HubspotException) as exc: HubspotAPI(self.api_key).delete_user(self.test_learner) self.assertIn( "Hubspot user deletion failed due to unknown reasons", str(exc))
def test_delete_unauthorized_deletion(self, req_mock, mock_alert): self._mock_get_vid(req_mock, 200) self._mock_delete(req_mock, 401) with self.assertRaises(hubspot_api.HubspotException) as exc: HubspotAPI(self.api_key, self.test_region, self.from_address, self.alert_email).delete_user(self.test_learner) self.assertIn( "Hubspot user deletion failed due to authorized API call", str(exc)) mock_alert.assert_not_called()
def test_delete_server_failure_on_deletion(self, req_mock, mock_alert): self._mock_get_vid(req_mock, 200) self._mock_delete(req_mock, 500) with self.assertRaises(hubspot_api.HubspotException) as exc: HubspotAPI(self.api_key, self.test_region, self.from_address, self.alert_email).delete_user(self.test_learner) self.assertIn( "Hubspot user deletion failed due to server-side (Hubspot) issues", str(exc)) mock_alert.assert_not_called()
def test_delete_vid_not_found(self, req_mock, mock_alert): self._mock_get_vid(req_mock, 200) self._mock_delete(req_mock, 404) with self.assertRaises(hubspot_api.HubspotException) as exc: HubspotAPI(self.api_key, self.test_region, self.from_address, self.alert_email).delete_user(self.test_learner) self.assertIn( "Hubspot user deletion failed because vid doesn't match user", str(exc)) mock_alert.assert_not_called()
def _setup_all_apis_or_exit(fail_func, fail_code, config): """ Performs setup of EdxRestClientApi instances for LMS, E-Commerce, Credentials, and Demographics, as well as fetching the learner's record from LMS and validating that it is in a state to work on. Returns the learner dict and their current stage in the retirement flow. """ try: lms_base_url = config['base_urls']['lms'] ecommerce_base_url = config['base_urls'].get('ecommerce', None) credentials_base_url = config['base_urls'].get('credentials', None) segment_base_url = config['base_urls'].get('segment', None) demographics_base_url = config['base_urls'].get('demographics', None) license_manager_base_url = config['base_urls'].get( 'license_manager', None) wordpress_base_url = config['base_urls'].get('wordpress', None) client_id = config['client_id'] client_secret = config['client_secret'] wordpress_username = config['wordpress_username'] wordpress_app_password = config['wordpress_app_password'] sailthru_key = config.get('sailthru_key', None) sailthru_secret = config.get('sailthru_secret', None) salesforce_user = config.get('salesforce_user', None) salesforce_password = config.get('salesforce_password', None) salesforce_token = config.get('salesforce_token', None) salesforce_domain = config.get('salesforce_domain', None) salesforce_assignee = config.get('salesforce_assignee', None) segment_auth_token = config.get('segment_auth_token', None) segment_workspace_slug = config.get('segment_workspace_slug', None) hubspot_api_key = config.get('hubspot_api_key', None) hubspot_aws_region = config.get('hubspot_aws_region', None) hubspot_from_address = config.get('hubspot_from_address', None) hubspot_alert_email = config.get('hubspot_alert_email', None) panel_backend_base_url = config['base_urls'].get('panel_backend', None) panel_backend_auth_token = config.get('panel_backend_auth_token', None) for state in config['retirement_pipeline']: for service, service_url in (('ECOMMERCE', ecommerce_base_url), ('CREDENTIALS', credentials_base_url), ('SEGMENT', segment_base_url), ('SAILTHRU', sailthru_key), ('HUBSPOT', hubspot_api_key), ('DEMOGRAPHICS', demographics_base_url)): if state[2] == service and service_url is None: fail_func( fail_code, 'Service URL is not configured, but required for state {}' .format(state)) config['LMS'] = LmsApi(lms_base_url, lms_base_url, client_id, client_secret) if sailthru_key: config['SAILTHRU'] = SailthruApi(sailthru_key, sailthru_secret) if salesforce_user and salesforce_password and salesforce_token: config['SALESFORCE'] = SalesforceApi(salesforce_user, salesforce_password, salesforce_token, salesforce_domain, salesforce_assignee) if hubspot_api_key: config['HUBSPOT'] = HubspotAPI(hubspot_api_key, hubspot_aws_region, hubspot_from_address, hubspot_alert_email) if ecommerce_base_url: config['ECOMMERCE'] = EcommerceApi(lms_base_url, ecommerce_base_url, client_id, client_secret) if credentials_base_url: config['CREDENTIALS'] = CredentialsApi(lms_base_url, credentials_base_url, client_id, client_secret) if demographics_base_url: config['DEMOGRAPHICS'] = DemographicsApi(lms_base_url, demographics_base_url, client_id, client_secret) if license_manager_base_url: config['LICENSE_MANAGER'] = LicenseManagerApi( lms_base_url, license_manager_base_url, client_id, client_secret, ) if segment_base_url: config['SEGMENT'] = SegmentApi(segment_base_url, segment_auth_token, segment_workspace_slug) if wordpress_base_url: config['WORDPRESS'] = WordPressAPI(wordpress_base_url, wordpress_username, wordpress_app_password) if panel_backend_base_url: config['PANEL_BACKEND'] = PanelBackendAPI( panel_backend_base_url, panel_backend_auth_token) except Exception as exc: # pylint: disable=broad-except fail_func(fail_code, 'Unexpected error occurred!', exc)
def test_delete_server_failure_on_user_retrieval(self, req_mock): self._mock_get_vid(req_mock, 500) with self.assertRaises(hubspot_api.HubspotException) as exc: HubspotAPI(self.api_key).delete_user(self.test_learner) self.assertIn("Error attempted to get user_vid from Hubspot", str(exc))
def test_delete_no_email(self, req_mock): # pylint: disable=unused-argument with self.assertRaises(TypeError) as exc: HubspotAPI(self.api_key).delete_user({}) self.assertIn( 'Expected an email address for user to delete, but received None.', str(exc))
def _setup_all_apis_or_exit(fail_func, fail_code, config): """ Performs setup of EdxRestClientApi instances for LMS, E-Commerce, and Credentials, as well as fetching the learner's record from LMS and validating that it is in a state to work on. Returns the learner dict and their current stage in the retirement flow. """ try: lms_base_url = config['base_urls']['lms'] ecommerce_base_url = config['base_urls'].get('ecommerce', None) credentials_base_url = config['base_urls'].get('credentials', None) segment_base_url = config['base_urls'].get('segment', None) client_id = config['client_id'] client_secret = config['client_secret'] sailthru_key = config.get('sailthru_key', None) sailthru_secret = config.get('sailthru_secret', None) salesforce_user = config.get('salesforce_user', None) salesforce_password = config.get('salesforce_password', None) salesforce_token = config.get('salesforce_token', None) salesforce_domain = config.get('salesforce_domain', None) salesforce_assignee = config.get('salesforce_assignee', None) segment_auth_token = config.get('segment_auth_token', None) segment_workspace_slug = config.get('segment_workspace_slug', None) hubspot_api_key = config.get('hubspot_api_key', None) for state in config['retirement_pipeline']: for service, service_url in (('ECOMMERCE', ecommerce_base_url), ('CREDENTIALS', credentials_base_url), ('SEGMENT', segment_base_url), ('SAILTHRU', sailthru_key), ('HUBSPOT', hubspot_api_key)): if state[2] == service and service_url is None: fail_func( fail_code, 'Service URL is not configured, but required for state {}' .format(state)) config['LMS'] = LmsApi(lms_base_url, lms_base_url, client_id, client_secret) if sailthru_key: config['SAILTHRU'] = SailthruApi(sailthru_key, sailthru_secret) if salesforce_user and salesforce_password and salesforce_token: config['SALESFORCE'] = SalesforceApi(salesforce_user, salesforce_password, salesforce_token, salesforce_domain, salesforce_assignee) if hubspot_api_key: config['HUBSPOT'] = HubspotAPI(hubspot_api_key) if ecommerce_base_url: config['ECOMMERCE'] = EcommerceApi(lms_base_url, ecommerce_base_url, client_id, client_secret) if credentials_base_url: config['CREDENTIALS'] = CredentialsApi(lms_base_url, credentials_base_url, client_id, client_secret) if segment_base_url: config['SEGMENT'] = SegmentApi(segment_base_url, segment_auth_token, segment_workspace_slug) except Exception as exc: # pylint: disable=broad-except fail_func(fail_code, 'Unexpected error occurred!', exc)