def test_correct_bulk_emails_sent(self): message_a = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[0]) self.assertEqual(len(message_a), 0) message_b = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[1]) self.assertEqual(len(message_b), 0) with self.swap(feconf, 'CAN_SEND_EMAILS', True): gae_email_services.send_bulk_mail(self.SENDER_EMAIL, self.RECIPIENT_EMAILS, 'subject', 'body', 'html') message_a = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[0]) self.assertEqual(len(message_a), 1) message_b = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[1]) self.assertEqual(len(message_b), 1)
def test_bulk_emails_not_sent_if_can_send_emails_is_false(self): # Emails are not sent if the CAN_SEND_EMAILS setting is not turned on. message_a = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[0]) self.assertEqual(len(message_a), 0) message_b = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[1]) self.assertEqual(len(message_b), 0) email_exception = self.assertRaisesRegexp( Exception, 'This app cannot send emails.') with email_exception: gae_email_services.send_bulk_mail(self.SENDER_EMAIL, self.RECIPIENT_EMAILS, 'subject', 'body', 'html') message_a = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[0]) self.assertEqual(len(message_a), 0) message_b = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[1]) self.assertEqual(len(message_b), 0)
def test_bulk_mails_not_sent_if_recipient_email_is_malformed(self): """Tests that bulk emails are not sent if recipient email address is malformed. """ # Case when both recipient email address strings are None. malformed_recipient_emails = [None, None] email_exception = self.assertRaisesRegexp( ValueError, 'Malformed recipient email address: %s' % malformed_recipient_emails[0]) with self.swap(feconf, 'CAN_SEND_EMAILS', True), email_exception: gae_email_services.send_bulk_mail(self.SENDER_EMAIL, malformed_recipient_emails, 'subject', 'body', 'html') messages_a = self.mail_stub.get_sent_messages( to=malformed_recipient_emails[0]) self.assertEqual(len(messages_a), 0) messages_b = self.mail_stub.get_sent_messages( to=malformed_recipient_emails[1]) self.assertEqual(len(messages_b), 0) # Case when both recipient email address strings are empty. malformed_recipient_emails = ['', ''] email_exception = self.assertRaisesRegexp( ValueError, 'Malformed recipient email address: %s' % malformed_recipient_emails[0]) with self.swap(feconf, 'CAN_SEND_EMAILS', True), email_exception: gae_email_services.send_bulk_mail(self.SENDER_EMAIL, malformed_recipient_emails, 'subject', 'body', 'html') messages_a = self.mail_stub.get_sent_messages( to=malformed_recipient_emails[0]) self.assertEqual(len(messages_a), 0) messages_b = self.mail_stub.get_sent_messages( to=malformed_recipient_emails[1]) self.assertEqual(len(messages_b), 0) # Case when one of the recipient email address strings is None. malformed_recipient_emails = [self.RECIPIENT_A_EMAIL, None] with self.swap(feconf, 'CAN_SEND_EMAILS', True), email_exception: gae_email_services.send_bulk_mail(self.SENDER_EMAIL, malformed_recipient_emails, 'subject', 'body', 'html') messages_a = self.mail_stub.get_sent_messages( to=malformed_recipient_emails[0]) self.assertEqual(len(messages_a), 0) messages_b = self.mail_stub.get_sent_messages( to=malformed_recipient_emails[1]) self.assertEqual(len(messages_b), 0) # Case when one of the recipient email address strings is empty. malformed_recipient_emails = [self.RECIPIENT_A_EMAIL, ''] with self.swap(feconf, 'CAN_SEND_EMAILS', True), email_exception: gae_email_services.send_bulk_mail(self.SENDER_EMAIL, malformed_recipient_emails, 'subject', 'body', 'html') messages_a = self.mail_stub.get_sent_messages( to=malformed_recipient_emails[0]) self.assertEqual(len(messages_a), 0) messages_b = self.mail_stub.get_sent_messages( to=malformed_recipient_emails[1]) self.assertEqual(len(messages_b), 0)
def test_bulk_mails_not_sent_if_sender_email_is_malformed(self): """Tests that bulk emails are not sent if sender email address is malformed. """ # Case when malformed_sender_email is None. message_a = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[0]) self.assertEqual(0, len(message_a)) message_b = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[1]) self.assertEqual(0, len(message_b)) malformed_sender_email = None email_exception = self.assertRaisesRegexp( ValueError, 'Malformed sender email address: %s' % malformed_sender_email) with self.swap(feconf, 'CAN_SEND_EMAILS', True), email_exception: gae_email_services.send_bulk_mail(malformed_sender_email, self.RECIPIENT_EMAILS, 'subject', 'body', 'html') message_a = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[0]) self.assertEqual(0, len(message_a)) message_b = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[1]) self.assertEqual(0, len(message_b)) # Case when malformed_sender_email is an empty string. message_a = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[0]) self.assertEqual(0, len(message_a)) message_b = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[1]) self.assertEqual(0, len(message_b)) malformed_sender_email = '' email_exception = self.assertRaisesRegexp( ValueError, 'Malformed sender email address: %s' % malformed_sender_email) with self.swap(feconf, 'CAN_SEND_EMAILS', True), email_exception: gae_email_services.send_bulk_mail(malformed_sender_email, self.RECIPIENT_EMAILS, 'subject', 'body', 'html') message_a = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[0]) self.assertEqual(0, len(message_a)) message_b = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[1]) self.assertEqual(0, len(message_b)) # Case when malformed_sender_email does not have name of the sender # and only the email address. message_a = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[0]) self.assertEqual(0, len(message_a)) message_b = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[1]) self.assertEqual(0, len(message_b)) malformed_sender_email = '<*****@*****.**>' email_exception = self.assertRaisesRegexp( ValueError, 'Malformed sender email address: %s' % malformed_sender_email) with self.swap(feconf, 'CAN_SEND_EMAILS', True), email_exception: gae_email_services.send_bulk_mail(malformed_sender_email, self.RECIPIENT_EMAILS, 'subject', 'body', 'html') message_a = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[0]) self.assertEqual(0, len(message_a)) message_b = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[1]) self.assertEqual(0, len(message_b)) # Case when malformed_sender_email does not have email address of the # sender and only the name. message_a = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[0]) self.assertEqual(0, len(message_a)) message_b = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[1]) self.assertEqual(0, len(message_b)) malformed_sender_email = 'MalformedSender' email_exception = self.assertRaisesRegexp( ValueError, 'Malformed sender email address: %s' % malformed_sender_email) with self.swap(feconf, 'CAN_SEND_EMAILS', True), email_exception: gae_email_services.send_bulk_mail(malformed_sender_email, self.RECIPIENT_EMAILS, 'subject', 'body', 'html') message_a = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[0]) self.assertEqual(0, len(message_a)) message_b = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[1]) self.assertEqual(0, len(message_b)) # Case when malformed_sender_email does not have email address of the # form '<sender_email_address>'. message_a = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[0]) self.assertEqual(0, len(message_a)) message_b = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[1]) self.assertEqual(0, len(message_b)) malformed_sender_email = 'Malformed Sender [email protected]' email_exception = self.assertRaisesRegexp( ValueError, 'Malformed sender email address: %s' % malformed_sender_email) with self.swap(feconf, 'CAN_SEND_EMAILS', True), email_exception: gae_email_services.send_bulk_mail(malformed_sender_email, self.RECIPIENT_EMAILS, 'subject', 'body', 'html') message_a = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[0]) self.assertEqual(0, len(message_a)) message_b = self.mail_stub.get_sent_messages( to=self.RECIPIENT_EMAILS[1]) self.assertEqual(0, len(message_b))