def test_sailthru_delete_not_found_response(test_learner, caplog): # pylint: disable=redefined-outer-name """ The user never opted into email marketing, so they never had a sailthru profile generated. Sailthru will respond with a "not found" error, which we should treat as "no action needed". """ with mock.patch('tubular.sailthru_api.SailthruClient'): sailthru_api = SailthruApi('key', 'secret') # pylint: disable=redefined-outer-name mock_response = mock.Mock() mock_response.is_ok.return_value = False mock_error = mock.Mock() mock_error.get_message.return_value = 'User not found with email: [email protected]' mock_response.get_error.return_value = mock_error sailthru_api._sailthru_client.api_delete.return_value = mock_response # pylint: disable=protected-access with caplog.at_level(logging.INFO): sailthru_api.delete_user(test_learner) assert 'No action taken because no user was found in Sailthru.' in caplog.text
def test_sailthru_delete_no_email(): with mock.patch('tubular.sailthru_api.SailthruClient'): with pytest.raises(TypeError) as exc: SailthruApi('key', 'secret').delete_user({}) assert 'Expected an email address for user to delete, but received None.' in str(exc)
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 _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)