def test_init_no_setting(self): # Tests that SENDGRID_API_KEY must be set for the SendgridBackend to initialize. # (or an api key must be explicitly passed to the constructor) backend = SendgridBackend(api_key="DUMMY_API_KEY") with self.assertRaises(ImproperlyConfigured): backend = SendgridBackend() # noqa
def test_sandbox_mode(self): """ Tests combinations of DEBUG and SENDGRID_SANDBOX_MODE_IN_DEBUG to ensure that the behavior is as expected. """ msg = EmailMessage( subject="Hello, World!", body="Hello, World!", from_email="Sam Smith <*****@*****.**>", to=["John Doe <*****@*****.**>"], ) # Sandbox mode should be False with override_settings(DEBUG=False, SENDGRID_SANDBOX_MODE_IN_DEBUG=True): backend = SendgridBackend(api_key="stub") result = backend._build_sg_mail(msg) self.assertIn("mail_settings", result) self.assertIn("sandbox_mode", result["mail_settings"]) self.assertFalse(result["mail_settings"]["sandbox_mode"]["enable"]) # Sandbox mode should be True with override_settings(DEBUG=True, SENDGRID_SANDBOX_MODE_IN_DEBUG=True): backend = SendgridBackend(api_key="stub") result = backend._build_sg_mail(msg) self.assertIn("mail_settings", result) self.assertIn("sandbox_mode", result["mail_settings"]) self.assertTrue(result["mail_settings"]["sandbox_mode"]["enable"]) # Sandbox mode should be True (by default when DEBUG==True) with override_settings(DEBUG=True): backend = SendgridBackend(api_key="stub") result = backend._build_sg_mail(msg) self.assertIn("mail_settings", result) self.assertIn("sandbox_mode", result["mail_settings"]) self.assertTrue(result["mail_settings"]["sandbox_mode"]["enable"]) # Sandbox mode should be False with override_settings(DEBUG=True, SENDGRID_SANDBOX_MODE_IN_DEBUG=False): backend = SendgridBackend(api_key="stub") result = backend._build_sg_mail(msg) self.assertIn("mail_settings", result) self.assertIn("sandbox_mode", result["mail_settings"]) self.assertFalse(result["mail_settings"]["sandbox_mode"]["enable"])
def setUpClass(self): super(TestMailGeneration, self).setUpClass() with override_settings( EMAIL_BACKEND="sendgrid_backend.SendgridBackend", SENDGRID_API_KEY="DUMMY_API_KEY", ): self.backend = SendgridBackend()
def test_echo(self): settings = { "DEBUG": True, "SENDGRID_API_KEY": "DOESNT_MATTER", "EMAIL_BACKEND": "sendgrid_backend.SendgridBackend", "SENDGRID_ECHO_TO_STDOUT": True, } with override_settings(**settings): mocked_output_stream = MagicMock() connection = SendgridBackend(stream=mocked_output_stream) msg = EmailMessage( subject="Hello, World!", body="Hello, World!", from_email="Sam Smith <*****@*****.**>", to=["John Doe <*****@*****.**>"], connection=connection, ) try: msg.send() except UnauthorizedError: # Since Github only runs live server tests on protected branches (for security), # we will get an unauthorized error when attempting to hit the sendgrid api endpoint, even in # sandbox mode. warnings.warn( "Sendgrid requests using sandbox mode still need valid credentials for the " + "request to succeed." ) self.assertTrue(mocked_output_stream.write.called)
def contact_index(request): if request.method == 'GET': form = ContactForm() else: form = ContactForm(request.POST) if form.is_valid(): template = get_template('contact_template.txt') name = form.cleaned_data['name'] from_email = form.cleaned_data['from_email'] message = form.cleaned_data['message'] context = { 'name': name, 'from_email': from_email, 'message': message, } content = template.render(context) html_thanks = render_to_string("email.html", context) try: request_email = EmailMessage( name + ' - Customer Contact Request', content, 'Your Website<*****@*****.**>', ['*****@*****.**'], reply_to=[from_email]) thanks_email = EmailMessage( 'DO NOT REPLY - Senior Technology Associates', html_thanks, '*****@*****.**', [from_email]) thanks_email.content_subtype = 'html' sg = SendgridBackend() sg.send_messages([request_email, thanks_email]) messages.success( request, 'Message Recieved: Senior Technology Associates will get back to you ASAP' ) except BadHeaderError: return HttpResponse('Invalid header found.') return redirect('home_index') messages.info( request, 'Please pardon our appearance, this page is under construction.') recent_posts = Post.objects.all().order_by('-created_on')[:3] bios = Biography.objects.all() return render(request, "contact_index.html", { 'form': form, 'recent_posts': recent_posts, 'bios': bios, })
def test_sandbox_mode(self): msg = EmailMessage( subject="Hello, World!", body="Hello, World!", from_email="Sam Smith <*****@*****.**>", to=["John Doe <*****@*****.**>"], ) with override_settings(DEBUG=False, SENDGRID_SANDBOX_MODE_IN_DEBUG=True): backend = SendgridBackend(api_key="stub") result = backend._build_sg_mail(msg) self.assertIn("mail_settings", result) self.assertIn("sandbox_mode", result["mail_settings"]) self.assertFalse(result["mail_settings"]["sandbox_mode"]["enable"]) with override_settings(DEBUG=True, SENDGRID_SANDBOX_MODE_IN_DEBUG=True): backend = SendgridBackend(api_key="stub") result = backend._build_sg_mail(msg) self.assertIn("mail_settings", result) self.assertIn("sandbox_mode", result["mail_settings"]) self.assertTrue(result["mail_settings"]["sandbox_mode"]["enable"]) with override_settings(DEBUG=True): backend = SendgridBackend(api_key="stub") result = backend._build_sg_mail(msg) self.assertIn("mail_settings", result) self.assertIn("sandbox_mode", result["mail_settings"]) self.assertTrue(result["mail_settings"]["sandbox_mode"]["enable"]) with override_settings(DEBUG=True, SENDGRID_SANDBOX_MODE_IN_DEBUG=False): backend = SendgridBackend(api_key="stub") result = backend._build_sg_mail(msg) self.assertIn("mail_settings", result) self.assertIn("sandbox_mode", result["mail_settings"]) self.assertFalse(result["mail_settings"]["sandbox_mode"]["enable"])
def test_echo(self): settings = { "DEBUG": True, "SENDGRID_API_KEY": os.environ["SENDGRID_API_KEY"], "EMAIL_BACKEND": "sendgrid_backend.SendgridBackend", "SENDGRID_ECHO_TO_STDOUT": True } with override_settings(**settings): mocked_output_stream = MagicMock() connection = SendgridBackend(stream=mocked_output_stream) msg = EmailMessage( subject="Hello, World!", body="Hello, World!", from_email="Sam Smith <*****@*****.**>", to=["John Doe <*****@*****.**>"], connection=connection, ) msg.send() self.assertTrue(mocked_output_stream.write.called)
def test_init_no_setting(self): backend = SendgridBackend(api_key="DUMMY_API_KEY") with self.assertRaises(ImproperlyConfigured): backend = SendgridBackend()