Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
bug 1142576: Standardize email headers
Browse files Browse the repository at this point in the history
Standardize email headers across first edit and watch emails.
  • Loading branch information
jwhitlock committed Apr 28, 2017
1 parent 6a23390 commit a36d655
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
24 changes: 18 additions & 6 deletions kuma/wiki/events.py
Expand Up @@ -64,6 +64,16 @@ def notification_context(revision):
return context


def extra_headers(user, document=None):
"""Get extra headers for filtering notification emails."""
headers = {
'X-Kuma-Editor-Username': user.username
}
if document:
headers['X-Kuma-Document-Url'] = document.get_full_url()
return headers


class EditDocumentEvent(InstanceEvent):
"""
Event fired when a certain document is edited
Expand Down Expand Up @@ -96,7 +106,8 @@ def _mails(self, users_and_watches):
html_template=None,
context_vars=context,
users_and_watches=users_and_watches,
default_locale=document.locale)
default_locale=document.locale,
headers=extra_headers(revision.creator, document))

def fire(self, **kwargs):
parent_events = [EditDocumentInTreeEvent(doc) for doc in
Expand Down Expand Up @@ -131,8 +142,7 @@ def first_edit_email(revision):
notification_context(revision))
email = EmailMessage(subject, message, settings.DEFAULT_FROM_EMAIL,
to=[config.EMAIL_LIST_SPAM_WATCH],
headers={'X-Kuma-Document-Url': doc.get_full_url(),
'X-Kuma-Editor-Username': user.username})
headers=extra_headers(user, doc))
return email


Expand All @@ -143,12 +153,14 @@ def spam_attempt_email(spam_attempt):
Because the spam may be on document creation, the document might be null.
"""
subject = '[MDN] Wiki spam attempt recorded'
if spam_attempt.document:
subject = '%s for document %s' % (subject, spam_attempt.document)
document = spam_attempt.document
if document:
subject = '%s for document %s' % (subject, document)
elif spam_attempt.title:
subject = '%s with title %s' % (subject, spam_attempt.title)
body = render_to_string('wiki/email/spam.ltxt',
{'spam_attempt': spam_attempt})
email = EmailMessage(subject, body, settings.DEFAULT_FROM_EMAIL,
to=[config.EMAIL_LIST_SPAM_WATCH])
to=[config.EMAIL_LIST_SPAM_WATCH],
headers=extra_headers(spam_attempt.user, document))
return email
16 changes: 15 additions & 1 deletion kuma/wiki/tests/test_events.py
Expand Up @@ -131,7 +131,11 @@ def test_edit_document_event_emails_on_create(mock_emails, create_revision):
'html_template': None,
'context_vars': notification_context(create_revision),
'users_and_watches': users_and_watches,
'default_locale': 'en-US'
'default_locale': 'en-US',
'headers': {
'X-Kuma-Document-Url': u'https://example.com/en-US/docs/Root',
'X-Kuma-Editor-Username': u'wiki_user'
}
}
subject = kwargs['subject'] % kwargs['context_vars']
expected = '[MDN][en-US][New] Page "Root Document" created by wiki_user'
Expand Down Expand Up @@ -197,6 +201,9 @@ def test_spam_attempt_email_on_create(wiki_user):
mail = spam_attempt_email(spam_attempt)
assert mail.subject == ('[MDN] Wiki spam attempt recorded with title'
' My new spam page')
assert mail.extra_headers == {
'X-Kuma-Editor-Username': u'wiki_user'
}


def test_spam_attempt_email_on_change(wiki_user, root_doc):
Expand All @@ -211,6 +218,10 @@ def test_spam_attempt_email_on_change(wiki_user, root_doc):
mail = spam_attempt_email(spam_attempt)
assert mail.subject == ('[MDN] Wiki spam attempt recorded for document'
' /en-US/docs/Root (Root Document)')
assert mail.extra_headers == {
'X-Kuma-Document-Url': u'https://example.com/en-US/docs/Root',
'X-Kuma-Editor-Username': u'wiki_user'
}


def test_spam_attempt_email_on_translate(wiki_user, trans_doc):
Expand All @@ -235,3 +246,6 @@ def test_spam_attempt_email_partial_model(wiki_user):
)
mail = spam_attempt_email(spam_attempt)
assert mail.subject == ('[MDN] Wiki spam attempt recorded')
assert mail.extra_headers == {
'X-Kuma-Editor-Username': u'wiki_user'
}

0 comments on commit a36d655

Please sign in to comment.