diff --git a/hobo/matomo/utils.py b/hobo/matomo/utils.py index d7a3c04..e305147 100644 --- a/hobo/matomo/utils.py +++ b/hobo/matomo/utils.py @@ -118,6 +118,8 @@ class MatomoWS(object): self.url_ws_base = config['URL'] self.token_auth = config['TOKEN_AUTH'] self.email_template = config['EMAIL_TEMPLATE'] + if '%s' in self.email_template: + self.email_template = self.email_template.replace('%s', '%(user_login)s') except KeyError as exc: raise MatomoError('no settings for matomo: %s' % str(exc)) @@ -211,7 +213,7 @@ class MatomoWS(object): 'method': 'UsersManager.addUser', 'userLogin': user_login, 'password': password, - 'email': self.email_template % user_login, + 'email': self.email_template % {'user_login': user_login}, 'initialIdSite': initial_id_site, } tree = self.call(data) diff --git a/hobo/settings.py b/hobo/settings.py index f6bd7b8..16e62be 100644 --- a/hobo/settings.py +++ b/hobo/settings.py @@ -212,7 +212,7 @@ MELLON_USERNAME_TEMPLATE = '{attributes[name_id_content]}' # MATOMO_SERVER = { # 'URL': 'https://matomo.domain.org', # 'TOKEN_AUTH': '0123456789abcdef0123456789abcdef', -# 'EMAIL_TEMPLATE': 'noreply+%s@domain.org' +# 'EMAIL_TEMPLATE': 'noreply+%(user_login)s@domain.org' # } MATOMO_SERVER = {} diff --git a/tests/test_matomo_utils.py b/tests/test_matomo_utils.py index 40cf56d..dba20a7 100644 --- a/tests/test_matomo_utils.py +++ b/tests/test_matomo_utils.py @@ -27,7 +27,11 @@ from hobo.matomo.utils import ( pytestmark = pytest.mark.django_db -CONFIG = {'URL': 'https://matomo.test', 'TOKEN_AUTH': '1234', 'EMAIL_TEMPLATE': 'noreply+%s@entrouvert.test'} +CONFIG = { + 'URL': 'https://matomo.test', + 'TOKEN_AUTH': '1234', + 'EMAIL_TEMPLATE': 'noreply+%(user_login)s@entrouvert.test', +} MATOMO_SUCCESS = b""" @@ -859,3 +863,27 @@ def test_auto_configure_matomo_error(mocked_post): auto_configure_matomo(matomo) tracking_js_var = get_variable('visits_tracking_js') assert tracking_js_var.value == 'js_code' + + +def test_legacy_email_template_substitution(): + with override_settings( + MATOMO_SERVER={ + 'URL': 'https://matomo.test', + 'TOKEN_AUTH': '1234', + 'EMAIL_TEMPLATE': 'noreply+%s@example.net', + } + ): + matomo = MatomoWS() + assert matomo.email_template == 'noreply+%(user_login)s@example.net' + + +def test_email_template_no_substitution(): + with override_settings( + MATOMO_SERVER={ + 'URL': 'https://matomo.test', + 'TOKEN_AUTH': '1234', + 'EMAIL_TEMPLATE': 'noreply@example.net', + } + ): + matomo = MatomoWS() + assert matomo.email_template == 'noreply@example.net'