Skip to content

Commit 1898891

Browse files
committed
Rework email send as a reference in context and provide its fake.
Use the runtime configuration and its persistent context as the means to expose the email sending logic to codepaths making use of it. This allow centralised control over the active email sending implementation without having to thread extra arguments through internal codepaths. As a result, we are able to centrally intercept email sending. Do this, and as a consequence sprinkle a bunch of additional assertions into calls that happen to send email but this side-effect was completely uncovered. Add a means for tests to check the the number of emails sent and whether an email was sent to a particular recipient. For cases where the email sent is not relevant to what is being tested, there is an escape hatch.
1 parent d6bd257 commit 1898891

17 files changed

Lines changed: 238 additions & 71 deletions

mig/server/grid_notify.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,10 @@ def send_notifications(configuration):
130130
notify_message = "Found %s new events since: %s\n\n" \
131131
% (total_events, timestr) \
132132
+ notify_message
133-
status = send_email(
133+
status = send_email(configuration,
134134
recipient,
135135
subject,
136-
notify_message,
137-
logger,
138-
configuration)
136+
notify_message)
139137
if status:
140138
logger.info("Send email with %s events to: %s"
141139
% (total_events, recipient))

mig/shared/conf.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class RuntimeConfiguration:
5454

5555
def __init__(self, configuration):
5656
object.__setattr__(self, '_configuration', configuration)
57+
object.__setattr__(self, '_context', {})
5758

5859
def __delattr__(self, attr):
5960
return delattr(self._configuration, attr)
@@ -64,6 +65,17 @@ def __getattr__(self, attr):
6465
def __setattr__(self, attr, value):
6566
return setattr(self._configuration, attr, value)
6667

68+
def context_get(self, context_key):
69+
return self._context.get(context_key, None)
70+
71+
def context_set(self, context_key, context_value):
72+
self._context[context_key] = context_value
73+
74+
@classmethod
75+
def is_runtime_configuration(cls, obj):
76+
"""Is the given object a runtime configuration."""
77+
return isinstance(obj, cls)
78+
6779

6880
def get_configuration_object(config_file=None,
6981
skip_log=False,

mig/shared/functionality/autocreate.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,8 +740,7 @@ def main(client_id, user_arguments_dict, environ=None):
740740
logger.info('Send email: to: %s, header: %s, smtp_server: %s'
741741
% (email, email_header, smtp_server))
742742
logger.debug('email body: %s' % email_msg)
743-
if not send_email(email, email_header, email_msg, logger,
744-
configuration):
743+
if not send_email(configuration, email, email_header, email_msg):
745744
output_objects.append({
746745
'object_type': 'error_text', 'text': """An error occurred trying
747746
to send your account welcome email. Please contact support (%s) and include the

mig/shared/functionality/extcertaction.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,7 @@ def main(client_id, user_arguments_dict):
316316

317317
logger.info('Sending email: to: %s, header: %s, msg: %s, smtp_server: %s'
318318
% (admin_email, email_header, email_msg, smtp_server))
319-
if not send_email(admin_email, email_header, email_msg, logger,
320-
configuration):
319+
if not send_email(configuration, admin_email, email_header, email_msg):
321320
output_objects.append({'object_type': 'error_text', 'text':
322321
"""An error occurred trying to inform the site
323322
admins about your request for existing certificate sign up. Please contact %s site

mig/shared/functionality/extoidaction.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,7 @@ def main(client_id, user_arguments_dict):
282282

283283
logger.info('Sending email: to: %s, header: %s, msg: %s, smtp_server: %s'
284284
% (admin_email, email_header, email_msg, smtp_server))
285-
if not send_email(admin_email, email_header, email_msg, logger,
286-
configuration):
285+
if not send_email(configuration, admin_email, email_header, email_msg):
287286
output_objects.append({'object_type': 'error_text', 'text':
288287
"""An error occurred trying to inform the site
289288
admins about your request for OpenID account access. Please contact %s site

mig/shared/functionality/peersaction.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,7 @@ def main(client_id, user_arguments_dict):
309309
logger.info('Sending invitation: to: %s, header: %s, msg: %s, smtp_server: %s'
310310
% (peer_email, email_header, email_msg,
311311
smtp_server))
312-
if send_email(peer_email, email_header, email_msg, logger,
313-
configuration):
312+
if send_email(configuration, peer_email, email_header, email_msg):
314313
succeeded.append(peer_email)
315314
else:
316315
failed.append(peer_email)
@@ -369,8 +368,7 @@ def main(client_id, user_arguments_dict):
369368

370369
logger.info('Sending email: to: %s, header: %s, msg: %s, smtp_server: %s'
371370
% (admin_email, email_header, email_msg, smtp_server))
372-
if not send_email(admin_email, email_header, email_msg, logger,
373-
configuration):
371+
if not send_email(configuration, admin_email, email_header, email_msg):
374372
output_objects.append({'object_type': 'error_text', 'text': '''
375373
An error occurred trying to send the email about your %s peers to the site
376374
administrators. Please contact %s site support at %s or manually inform the

mig/shared/functionality/reqcertaction.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,7 @@ def main(client_id, user_arguments_dict):
346346

347347
logger.info('Sending email: to: %s, header: %s, msg: %s, smtp_server: %s'
348348
% (admin_email, email_header, email_msg, smtp_server))
349-
if not send_email(admin_email, email_header, email_msg, logger,
350-
configuration):
349+
if not send_email(configuration, admin_email, email_header, email_msg):
351350
output_objects.append({'object_type': 'error_text', 'text':
352351
"""An error occurred trying to inform the site
353352
admins about your request for certificate sign up. Please contact %s site

mig/shared/functionality/reqoidaction.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,7 @@ def main(client_id, user_arguments_dict):
357357

358358
logger.info('Sending email: to: %s, header: %s, msg: %s, smtp_server: %s'
359359
% (admin_email, email_header, email_msg, smtp_server))
360-
if not send_email(admin_email, email_header, email_msg, logger,
361-
configuration):
360+
if not send_email(configuration, admin_email, email_header, email_msg):
362361
output_objects.append({'object_type': 'error_text', 'text':
363362
"""An error occurred trying to inform the site
364363
admins about your request for OpenID account access. Please contact %s site

mig/shared/functionality/reqpwresetaction.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,7 @@ def main(client_id, user_arguments_dict):
271271

272272
logger.info('Sending email: to: %s, header: %s, msg: %s, smtp_server: %s'
273273
% (email_to, email_header, email_msg, smtp_server))
274-
if not send_email(email_to, email_header, email_msg, logger,
275-
configuration):
274+
if not send_email(configuration, email_to, email_header, email_msg):
276275
output_objects.append({'object_type': 'error_text', 'text':
277276
"""An error occurred trying to send the email
278277
for an account %s password reset request. Please contact %s site support at %s

mig/shared/gdp/base.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -683,12 +683,10 @@ def __send_project_action_confirmation(configuration,
683683
684684
Attached you'll find the details registered in relation to the operation.
685685
""" % mail_fill
686-
status = send_email(
686+
status = send_email(configuration,
687687
recipients,
688688
subject,
689689
message,
690-
_logger,
691-
configuration,
692690
files=[pdf_filepath],
693691
)
694692
if status:

0 commit comments

Comments
 (0)