matomo: use dict formatting for email template (#67666)
gitea-wip/hobo/pipeline/head There was a failure building this commit Details
gitea/hobo/pipeline/head Something is wrong with the build of this commit Details

This commit is contained in:
Frédéric Péters 2022-07-24 11:15:38 +02:00
parent 53a3226e55
commit 6c10f62ea3
3 changed files with 33 additions and 3 deletions

View File

@ -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)

View File

@ -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 = {}

View File

@ -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"""<?xml version="1.0" encoding="utf-8" ?>
<result>
@ -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'