def test_print_mailbox_mime_image(self): assert_pformat_equal(len(mail.outbox), 0) ok = SendMail( template_base="mail_test.{ext}", mail_context={"foo": "first", "bar": "second"}, subject="test test_print_mailbox()", recipient_list="*****@*****.**", attachments=[MIMEImage(b"GIF89a This is not a gif picture ;)")], ).send() assert_pformat_equal(ok, True) assert_pformat_equal(len(mail.outbox), 1) with StdoutStderrBuffer() as buff: print_mailbox(mail.outbox) output = buff.get_output() print(output) self.assertIn("*** Mail No. 1: ***", output) self.assertIn("subject: test test_print_mailbox()", output) self.assertIn("<!-- START 'mail_test.txt' -->", output) self.assertIn("<!-- END 'mail_test.txt' -->", output) self.assertIn("from_email: webmaster@localhost", output) self.assertIn("to: ['*****@*****.**']", output) self.assertIn("attachments:", output) self.assertIn("MIMEImage object", output) self.assertIn("b'GIF89a", output)
def test_print_mailbox(self): self.assertEqual(len(mail.outbox), 0) ok = SendMail(template_base="mail_test.{ext}", mail_context={ "foo": "first", "bar": "second" }, subject="test test_print_mailbox()", recipient_list="*****@*****.**").send() self.assertEqual(ok, True) self.assertEqual(len(mail.outbox), 1) with StdoutStderrBuffer() as buff: print_mailbox(mail.outbox) output = buff.get_output() print(output) self.assertIn("*** Mail No. 1: ***", output) self.assertIn("subject: test test_print_mailbox()", output) self.assertIn("<!-- START 'mail_test.txt' -->", output) self.assertIn("<!-- END 'mail_test.txt' -->", output) self.assertIn("from_email: webmaster@localhost", output) self.assertIn("to: ['*****@*****.**']", output)
def test_SendMail(self): assert_pformat_equal(len(mail.outbox), 0) ok = SendMail( template_base="mail_test.{ext}", mail_context={ "foo": "first", "bar": "second" }, subject="Only a test", recipient_list="*****@*****.**", ).send() assert_pformat_equal(ok, True) print_mailbox(mail.outbox) assert_pformat_equal(len(mail.outbox), 1) email = mail.outbox[0] assert_pformat_equal(email.subject, "Only a test") assert_equal_dedent( email.body, """ <!-- START 'mail_test.txt' --> This is is a test mail. It used the django template: first, second <!-- END 'mail_test.txt' --> """, ) assert_pformat_equal(email.from_email, "webmaster@localhost") assert_pformat_equal(email.to, ["*****@*****.**"]) self.assertIsInstance(email, EmailMultiAlternatives) html_email = email.alternatives[0] assert_equal_dedent( html_email[0].strip(), """ <!-- START 'mail_test.html' --> <!DOCTYPE html> <html> <head></head> <body> <p>This is is a test mail.</br> It used the django template: first, second</p> </body> </html> <!-- END 'mail_test.html' --> """, ) assert_pformat_equal(html_email[1], "text/html")
def test_throttle_admin_email_handler(): handler = ThrottledAdminEmailHandler(min_delay_sec=0.1) for char in ("A", "B"): for no in range(3): handler.send_mail(subject=f"subject {char} {no:d}", message=f"message {char} {no:d}") time.sleep(0.11) handler.send_mail(subject=f"subject {char} last", message=f"message {char} last") time.sleep(0.11) print_mailbox(mail.outbox) assert len(mail.outbox) == 4 ########################################################################## # 1. "normal" mail: email = mail.outbox[0] assert "subject A 0" in email.subject assert "message A 0" in email.body assert "skipped mails" not in email.body ########################################################################## # 2. mail with skipped mails: email = mail.outbox[1] assert "subject A last" in email.subject assert "2 skipped mails" in email.body assert "* subject A 1" in email.body assert "* subject A 2" in email.body assert "message A last" in email.body ########################################################################## # 3. "normal" mail: email = mail.outbox[2] assert "subject B 0" in email.subject assert "message B 0" in email.body assert "skipped mails" not in email.body ########################################################################## # 4. mail with skipped mails: email = mail.outbox[3] assert "subject B last" in email.subject assert "2 skipped mails" in email.body assert "* subject B 1" in email.body assert "* subject B 2" in email.body assert "message B last" in email.body
def test_SendMail(self): assert_pformat_equal(len(mail.outbox), 0) ok = SendMail( template_base="mail_test.{ext}", mail_context={"foo": "first", "bar": "second"}, subject="Only a test", recipient_list="*****@*****.**", ).send() assert_pformat_equal(ok, True) print_mailbox(mail.outbox) assert_pformat_equal(len(mail.outbox), 1) email = mail.outbox[0] assert_pformat_equal(email.subject, "Only a test") assert_equal_dedent( email.body, """ <!-- START 'mail_test.txt' --> This is is a test mail. It used the django template: first, second <!-- END 'mail_test.txt' --> """, ) assert_pformat_equal(email.from_email, "webmaster@localhost") assert_pformat_equal(email.to, ["*****@*****.**"]) self.assertIsInstance(email, EmailMultiAlternatives) html_email = email.alternatives[0] assert_equal_dedent( html_email[0].strip(), """ <!-- START 'mail_test.html' --> <!DOCTYPE html> <html> <head></head> <body> <p>This is is a test mail.</br> It used the django template: first, second</p> </body> </html> <!-- END 'mail_test.html' --> """, ) assert_pformat_equal(html_email[1], "text/html")
def test_SendMailCelery_more_mails(self): self.assertEqual(len(mail.outbox), 0) for no in range(1, 4): subject = "test_SendMailCelery_more_mails() - no. %i" % no print("Send mail %r" % subject) ok = SendMailCelery( template_base="mail_test.{ext}", mail_context={ "foo": "first %i" % no, "bar": "second %i" % no, }, subject=subject, recipient_list="*****@*****.**", ).send() self.assertTrue(ok) with StdoutStderrBuffer() as buff: print_mailbox(mail.outbox) output = buff.get_output() print(output) self.assertEqual(len(mail.outbox), 3) self.assertIn("*** Mail No. 1: ***", output) self.assertIn("subject: test_SendMailCelery_more_mails() - no. 1", output) self.assertIn("It used the django template: first 1, second 1", output) self.assertIn("*** Mail No. 2: ***", output) self.assertIn("subject: test_SendMailCelery_more_mails() - no. 2", output) self.assertIn("It used the django template: first 2, second 2", output) self.assertIn("*** Mail No. 3: ***", output) self.assertIn("subject: test_SendMailCelery_more_mails() - no. 3", output) self.assertIn("It used the django template: first 3, second 3", output)