Commit f45f0989 by Clinton Blackburn

Sailthru Cleanup

- Moved tasks to end of file, below the functions consumed by the tasks
- Added helper functions to get Sailthru configuration and API client
- Added exception classes

ECOM-6975
parent d83ffb50
""" Sailthru-related exception classes. """
class SailthruError(Exception):
""" Base class for Sailthru-related errors. """
pass
class ConfigurationError(SailthruError):
""" Raised when Sailthru is not properly configured. """
pass
class SailthruNotEnabled(SailthruError):
""" Raised when Sailthru is not enabled. """
pass
""" Tests for utility functions. """
from unittest import TestCase
import ddt
import mock
from ecommerce_worker.sailthru.v1.exceptions import SailthruNotEnabled, ConfigurationError
from ecommerce_worker.sailthru.v1.utils import get_sailthru_client
@ddt.ddt
class SailthruUtilsTests(TestCase):
""" Tests for Sailthru utility functions. """
SITE_CODE = 'test'
SITE_OVERRIDES_MODULE = 'ecommerce_worker.configuration.test.SITE_OVERRIDES'
def assert_get_sailthru_client_raises(self, exc_class, config):
""" Asserts an error is raised by a call to get_sailthru_client. """
overrides = {
self.SITE_CODE: {
'SAILTHRU': config
}
}
with mock.patch.dict(self.SITE_OVERRIDES_MODULE, overrides):
with self.assertRaises(exc_class):
get_sailthru_client(self.SITE_CODE)
def test_get_sailthru_client_with_sailthru_disabled(self):
""" Verify the method raises a SailthruNotEnabled if Sailthru is not enabled for the site. """
with mock.patch('ecommerce_worker.sailthru.v1.utils.log.debug') as mock_log:
self.assert_get_sailthru_client_raises(SailthruNotEnabled, {'SAILTHRU_ENABLE': False})
mock_log.assert_called_once_with('Sailthru is not enabled for site {}'.format(self.SITE_CODE))
@ddt.data(
{},
{'SAILTHRU_KEY': None, 'SAILTHRU_SECRET': None},
{'SAILTHRU_KEY': 'test', 'SAILTHRU_SECRET': None},
{'SAILTHRU_KEY': None, 'SAILTHRU_SECRET': 'test'},
)
def test_get_sailthru_client_without_credentials(self, sailthru_config):
""" Verify the method raises a ConfigurationError if Sailthru is not configured properly. """
sailthru_config['SAILTHRU_ENABLE'] = True
with mock.patch('ecommerce_worker.sailthru.v1.utils.log.error') as mock_log:
self.assert_get_sailthru_client_raises(ConfigurationError, sailthru_config)
mock_log.assert_called_once_with('Both key and secret are required for site {}'.format(self.SITE_CODE))
def test_get_sailthru_client(self):
""" Verify the method returns an authenticated SailthruClient. """
key = 'key'
secret = 'secret'
overrides = {
self.SITE_CODE: {
'SAILTHRU': {
'SAILTHRU_ENABLE': True,
'SAILTHRU_KEY': key,
'SAILTHRU_SECRET': secret
}
}
}
with mock.patch.dict(self.SITE_OVERRIDES_MODULE, overrides):
client = get_sailthru_client(self.SITE_CODE)
self.assertEqual(client.api_key, key)
self.assertEqual(client.secret, secret)
""" Utility functions. """
from celery.utils.log import get_task_logger
from sailthru.sailthru_client import SailthruClient
from ecommerce_worker.sailthru.v1.exceptions import ConfigurationError, SailthruNotEnabled
from ecommerce_worker.utils import get_configuration
log = get_task_logger(__name__)
def get_sailthru_configuration(site_code):
""" Returns the Sailthru configuration for the specified site. """
config = get_configuration('SAILTHRU', site_code=site_code)
return config
def get_sailthru_client(site_code):
"""
Returns a Sailthru client for the specified site.
Args:
site_code (str): Site for which the client should be configured.
Returns:
SailthruClient
Raises:
SailthruNotEnabled: If Sailthru is not enabled for the specified site.
ConfigurationError: If either the Sailthru API key or secret are not set for the site.
"""
# Get configuration
config = get_sailthru_configuration(site_code)
# Return if Sailthru integration disabled
if not config.get('SAILTHRU_ENABLE'):
msg = 'Sailthru is not enabled for site {}'.format(site_code)
log.debug(msg)
raise SailthruNotEnabled(msg)
# Make sure key and secret configured
key = config.get('SAILTHRU_KEY')
secret = config.get('SAILTHRU_SECRET')
if not (key and secret):
msg = 'Both key and secret are required for site {}'.format(site_code)
log.error(msg)
raise ConfigurationError(msg)
return SailthruClient(key, secret)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment