/
githubmirror
/
oppia
Обзор
Документация
Войти
/
githubmirror
/
oppia
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
core/domain/email_services_test.py
352 строки
12 KB
Sean Lip
Remove the bulk email functionality. (#26472)
02 июл 2026, 00:42
Не верифицирован
02 июл 2026, 00:42
5ab5db6
Код
Авторство
О чём код?
# Copyright 2018 The Oppia Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS-IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for services relating to emails.""" from __future__ import annotations import textwrap from core.domain import email_services, platform_parameter_list from core.platform import models from core.tests import test_utils (email_models,) = models.Registry.import_models([models.Names.EMAIL]) platform_email_services = models.Registry.import_email_services() class EmailServicesTest(test_utils.EmailTestBase): """Tests for email_services functions.""" def setUp(self) -> None: super().setUp() self.admin_email_address = 'testadmin@example.com' self.system_email_address = 'system@example.com' @test_utils.set_platform_parameters( [ (platform_parameter_list.ParamName.SERVER_CAN_SEND_EMAILS, True), (platform_parameter_list.ParamName.EMAIL_SENDER_NAME, 'sender'), (platform_parameter_list.ParamName.EMAIL_FOOTER, ''), ( platform_parameter_list.ParamName.ADMIN_EMAIL_ADDRESS, 'testadmin@example.com', ), ] ) def test_send_mail_data_properly_sent(self) -> None: """Verifies that the data sent in send_mail is correct.""" email_services.send_mail( self.system_email_address, self.admin_email_address, 'subject', 'body', 'html', bcc_admin=False, ) messages = self._get_sent_email_messages(self.admin_email_address) self.assertEqual(len(messages), 1) self.assertEqual(messages[0].subject, 'subject') self.assertEqual(messages[0].body, 'body') self.assertEqual(messages[0].html, 'html') @test_utils.set_platform_parameters( [ (platform_parameter_list.ParamName.SERVER_CAN_SEND_EMAILS, True), (platform_parameter_list.ParamName.EMAIL_SENDER_NAME, 'sender'), (platform_parameter_list.ParamName.EMAIL_FOOTER, ''), ( platform_parameter_list.ParamName.ADMIN_EMAIL_ADDRESS, 'testadmin@example.com', ), ] ) def test_bcc_admin_flag(self) -> None: """Verifies that the bcc admin flag is working properly in send_mail. """ email_services.send_mail( self.system_email_address, self.admin_email_address, 'subject', 'body', 'html', cc_emails=None, bcc_admin=True, ) messages = self._get_sent_email_messages(self.admin_email_address) self.assertEqual(len(messages), 1) self.assertEqual(messages[0].bcc, self.admin_email_address) @test_utils.set_platform_parameters( [ (platform_parameter_list.ParamName.SERVER_CAN_SEND_EMAILS, True), (platform_parameter_list.ParamName.EMAIL_SENDER_NAME, 'sender'), (platform_parameter_list.ParamName.EMAIL_FOOTER, ''), ] ) def test_email_not_sent_if_email_addresses_are_malformed(self) -> None: """Tests that email is not sent if recipient email address is malformed. """ # Case when malformed_recipient_email is None when calling send_mail. malformed_recipient_email = None email_exception = self.assertRaisesRegex( ValueError, 'Malformed recipient email address: %s' % malformed_recipient_email, ) with email_exception: # TODO(#13528): Here we use MyPy ignore because we remove this test # after the backend is fully type-annotated. send_mail() method # doesn't expect recipient_email to be None, and the case when # the recipient_email is malformed must be tested this is why # ignore[arg-type] is used here. email_services.send_mail( 'sender@example.com', malformed_recipient_email, # type: ignore[arg-type] 'subject', 'body', 'html', ) # Case when malformed_recipient_email is an empty string when # calling send_mail. malformed_recipient_email = '' email_exception = self.assertRaisesRegex( ValueError, 'Malformed recipient email address: %s' % malformed_recipient_email, ) with email_exception: email_services.send_mail( 'sender@example.com', malformed_recipient_email, 'subject', 'body', 'html', ) # Case when sender is malformed for send_mail. malformed_sender_email = 'x@x@x' email_exception = self.assertRaisesRegex( ValueError, 'Malformed sender email address: %s' % malformed_sender_email, ) with email_exception: email_services.send_mail( malformed_sender_email, 'recipient@example.com', 'subject', 'body', 'html', ) # Case when the SENDER_EMAIL in brackets of 'SENDER NAME <SENDER_EMAIL> # is malformed when calling send_mail. malformed_sender_email = 'Name <malformed_email>' email_exception = self.assertRaisesRegex( ValueError, 'Malformed sender email address: %s' % malformed_sender_email, ) with email_exception: email_services.send_mail( malformed_sender_email, 'recipient@example.com', 'subject', 'body', 'html', ) # Case when the sender email token is not wrapped in angle brackets # when calling send_mail. malformed_sender_emails = [ 'Name malformed_email>', 'Name <malformed_email', ] for malformed_sender_email in malformed_sender_emails: email_exception = self.assertRaisesRegex( ValueError, 'Malformed sender email address: %s' % malformed_sender_email, ) with email_exception: email_services.send_mail( malformed_sender_email, 'recipient@example.com', 'subject', 'body', 'html', ) @test_utils.set_platform_parameters( [ (platform_parameter_list.ParamName.SERVER_CAN_SEND_EMAILS, True), (platform_parameter_list.ParamName.EMAIL_SENDER_NAME, 'sender'), (platform_parameter_list.ParamName.EMAIL_FOOTER, ''), ( platform_parameter_list.ParamName.ADMIN_EMAIL_ADDRESS, 'testadmin@example.com', ), ] ) def test_unsuccessful_status_codes_raises_exception(self) -> None: """Test that unsuccessful status codes returned raises an exception.""" email_exception = self.assertRaisesRegex( Exception, ( 'Email to %s failed to send. Please try again later or ' 'contact us to report a bug at ' 'https://www.oppia.org/contact.' ) % self.admin_email_address, ) swap_send_email_to_recipients = self.swap( platform_email_services, 'send_email_to_recipients', lambda *_: False, ) with email_exception, swap_send_email_to_recipients: email_services.send_mail( self.system_email_address, self.admin_email_address, 'subject', 'body', 'html', bcc_admin=True, ) @test_utils.set_platform_parameters( [ (platform_parameter_list.ParamName.SERVER_CAN_SEND_EMAILS, True), ( platform_parameter_list.ParamName.ADMIN_EMAIL_ADDRESS, 'testadmin@example.com', ), ( platform_parameter_list.ParamName.SYSTEM_EMAIL_ADDRESS, 'system@example.com', ), ] ) def test_loggable_email_string_generation(self) -> None: """Tests that loggable email string is generated correctly.""" system_email_address = 'system@example.com' admin_email_address = 'testadmin@example.com' msg_body = """ EmailService.SendMail From: %s To: %s Subject: %s Body: Content-type: text/plain Data length: %d Body: Content-type: text/html Data length: %d Html content: html Cc: None Bcc: None Reply_to: None Recipient Variables: Length: 0 Attachments: None """ % ( system_email_address, admin_email_address, 'subject', 4, 4, ) self.assertEqual( textwrap.dedent(msg_body), email_services.convert_email_to_loggable_string( system_email_address, [admin_email_address], 'subject', 'body', 'html', ), ) def test_should_be_able_to_log_email_successfully(self) -> None: """Tests that emails are logged successfully.""" sender_email = 'test@oppia.org' recipient_emails = [ 'testrecipient@oppia.org', 'user1@gmail.com', 'user2@yahoo.com', 'user3@hotmail.com', ] subject = 'Test Subject' plaintext_body = 'This is a test email.' html_body = '<p>This is a test email.</p>' cc = [ 'test1@example.com', 'test2@example.com', 'test3@example.com', 'test4@example.com', ] bcc = [ 'bccUser1@example.com', 'bccUser2@example.com', 'bccUser3@example.com', 'bccUser4@example.com', ] attachments = [ {'filename': 'file1.txt', 'path': '/path/to/file1.txt'}, {'filename': 'file2.jpg', 'path': '/path/to/file2.jpg'}, ] response = email_services.convert_email_to_loggable_string( sender_email, recipient_emails, subject, plaintext_body, html_body, cc, bcc, reply_to=None, recipient_variables=None, attachments=attachments, ) expected_email_log = """ EmailService.SendMail From: test@oppia.org To: testrecipient@oppia.org user1@gmail.com user2@yahoo.com... Total: 4 emails. Subject: Test Subject Body: Content-type: text/plain Data length: 21 Body: Content-type: text/html Data length: 28 Html content: <p>This is a test email.</p> Cc: test1@example.com test2@example.com test3@example.com... Total: 4 emails. Bcc: bccUser1@example.com bccUser2@example.com bccUser3@example.com... Total: 4 emails. Reply_to: None Recipient Variables: Length: 0 Attachments: file1.txt, file2.jpg """ self.assertEqual( textwrap.dedent(expected_email_log).strip(), response.strip() )