Skip to content

Commit

Permalink
Fix a bug where the email header was incorrect. (oppia#6795)
Browse files Browse the repository at this point in the history
* Fix erroneous email header.

* Add backend test.
  • Loading branch information
seanlip authored and aks681 committed May 30, 2019
1 parent 18f8fb3 commit 3acc374
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/domain/email_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ def send_mail_to_admin(email_subject, email_body):

app_id = app_identity_services.get_application_id()
body = '(Sent from %s)\n\n%s' % (app_id, email_body)
system_name_email = '%s <%s>' % ('SYSTEM', feconf.SYSTEM_EMAIL_ADDRESS)
system_name_email = '%s <%s>' % (
feconf.SYSTEM_EMAIL_NAME, feconf.SYSTEM_EMAIL_ADDRESS)
email_services.send_mail(
system_name_email, feconf.ADMIN_EMAIL_ADDRESS, email_subject,
body, body.replace('\n', '<br/>'), bcc_admin=False)
Expand Down
36 changes: 36 additions & 0 deletions core/domain/email_manager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,42 @@ def test_send_failed_ml_email(self):
self.assertEqual(messages[0].subject.decode(), expected_subject)


class EmailToAdminTest(test_utils.GenericTestBase):
"""Test that emails are correctly sent to the admin."""

def test_email_to_admin_is_sent_correctly(self):
dummy_system_name = 'DUMMY_SYSTEM_NAME'
dummy_system_address = '[email protected]'
dummy_admin_address = '[email protected]'

send_email_ctx = self.swap(feconf, 'CAN_SEND_EMAILS', True)
system_name_ctx = self.swap(
feconf, 'SYSTEM_EMAIL_NAME', dummy_system_name)
system_email_ctx = self.swap(
feconf, 'SYSTEM_EMAIL_ADDRESS', dummy_system_address)
admin_email_ctx = self.swap(
feconf, 'ADMIN_EMAIL_ADDRESS', dummy_admin_address)

with send_email_ctx, system_name_ctx, system_email_ctx, admin_email_ctx:
# Make sure there are no emails already sent.
messages = self.mail_stub.get_sent_messages(
to=feconf.ADMIN_EMAIL_ADDRESS)
self.assertEqual(len(messages), 0)

# Send an email to the admin.
email_manager.send_mail_to_admin('Dummy Subject', 'Dummy Body')

# Make sure emails are sent.
messages = self.mail_stub.get_sent_messages(
to=feconf.ADMIN_EMAIL_ADDRESS)
self.assertEqual(len(messages), 1)
self.assertEqual(
messages[0].sender, 'DUMMY_SYSTEM_NAME <[email protected]>')
self.assertEqual(messages[0].to, '[email protected]')
self.assertEqual(messages[0].subject.decode(), 'Dummy Subject')
self.assertIn('Dummy Body', messages[0].html.decode())


class EmailRightsTest(test_utils.GenericTestBase):
"""Test that only certain users can send certain types of emails."""

Expand Down

0 comments on commit 3acc374

Please sign in to comment.