Yorgos Pagles avatar Yorgos Pagles committed 3cad294

Back to 100% coverage after postmark backend introduction

Comments (0)

Files changed (10)

email_services/models.py

-from django.db import models
-
 # Create your models here.

email_services/services/postmark.py

     """ Simple filter to manage postmark authentication"""
     
     def on_request(self, request):
-        request.headers['X-Postmark-Server-Token'] = settings.EMAIL_SERVICES_CLIENT_KEY
+        request.headers['X-Postmark-Server-Token'] = \
+        settings.EMAIL_SERVICES_CLIENT_KEY
 
 class PostmarkResource(Resource):
 
         if not self.resource:
             self.open()
             
-            
         for message in messages:
             
             html_content= ""
                                     from_email = message.from_email,
                                     html_body = html_content,
                                     reply_to = reply_to)
-            
-            
-            

email_services/tests.py

-"""
-Tests for the email_services django application
-"""
-import urlparse
-import os
-import mock
-
-from django.test import TestCase
-from django.core.mail import EmailMessage, EmailMultiAlternatives
-from django.contrib.sites.models import Site
-
-from email_services.backends import CritsendEmailBackend
-from email_services.services import CritsendEmailService
-from email_services import settings
-from django.conf import settings as djsettings
-
-class CritsendBackendTest(TestCase):
-
-    def setUp(self):
-        self.backend = CritsendEmailBackend()
-        self.backend.service = mock.Mock(spec=CritsendEmailService)
-
-    def test_connection_delegated_to_service(self):
-        """
-        When opening a connection the action is delegated to service
-        """
-        self.backend.open()
-        self.assertTrue(self.backend.service.open.called)
-
-    def test_connection_closing_delegated_to_service(self):
-        """
-        When closing a connection the action is delegated to service
-        """
-        self.backend.close()
-        self.assertTrue(self.backend.service.close.called)
-
-    def test_connection_exception_handling_on_open(self):
-        """
-        When an exception is raised while connecting it is reraised be the backend
-        in the default non silent mode
-        """
-        self.backend.service.open.side_effect = Exception("Raised something arbitrary")
-        with self.assertRaises(Exception) as e:
-            self.backend.open()
-        
-    def test_called_service_send(self):
-        """
-        When sending email from the backend the service call method is called
-        """
-        messages = []
-        self.backend.send_messages(messages)
-        self.assertTrue(self.backend.service.send_messages.called)
-
-    def test_connection_exception_handling_on_close(self):
-        """
-        When an exception is raised while closing it is reraised be the backend
-        in the default non silent mode
-        """
-        self.backend.service.close.side_effect = Exception("Raised something arbitrary")
-        with self.assertRaises(Exception) as e:
-            self.backend.close()
-
-    def test_connection_exception_handling_on_send(self):
-        """
-        When an exception is raised while sending it is reraised be the backend
-        in the default non silent mode
-        """
-        self.backend.service.send_messages.side_effect = Exception("Raised something arbitrary")
-        with self.assertRaises(Exception) as e:
-            self.backend.send_messages([])
-
-
-class  CritsendServiceTest(TestCase):
-
-    def setUp(self):
-        self.fake_user = "QWERTY"
-        self.fake_key = "QWERTY12345"
-        self.default_user = settings.EMAIL_SERVICES_CLIENT_ID
-        self.default_key = settings.EMAIL_SERVICES_CLIENT_KEY
-        settings.EMAIL_SERVICES_CLIENT_ID = self.fake_user
-        settings.EMAIL_SERVICES_CLIENT_KEY = self.fake_key
-        self.service = CritsendEmailService()
-        
-        # Get the wsdl locally
-        self.service.wsdl = "%stest.wsdl" % os.path.sep
-        self.service.hosts = ["file://%s" % os.path.join(djsettings.PROJECT_PATH, 
-                                                         "resources")]
-        
-    def tearDown(self):
-        settings.EMAIL_SERVICES_CLIENT_ID = self.default_user
-        settings.EMAIL_SERVICES_CLIENT_KEY = self.default_key
-        
-    def test_credentials(self):
-        """
-        Test that the credentials are created
-        """
-        self.assertEquals(self.service.credentials['user'],
-                          settings.EMAIL_SERVICES_CLIENT_ID)
-        self.assertTrue(self.service.credentials['timestamp'])
-        self.assertTrue(self.service.credentials['signature'])
-        
-    def test_connection_open(self):
-        """
-        Test that the service is opening correctly
-        """
-        self.service.open()
-        url_parsed = urlparse.urlparse(self.service.client.wsdl.url)
-        
-        # We are using a local file so the host should be in path
-        # In online resources it would be different
-        self.assertTrue("%s://%s" % (url_parsed.scheme, 
-                                     os.path.dirname(url_parsed.path)) 
-                        in self.service.hosts)
-        
-        self.assertEquals(os.path.basename(url_parsed.path), 
-                          self.service.wsdl.strip(os.path.sep))
-
-        # Check that it is not recreated
-        client = self.service.client
-        self.service.open()
-        self.assertTrue(client is self.service.client)
-        
-    def test_connection_close(self):
-        """
-        Test that the service is closing correctly
-        """
-        self.service.close()
-        self.assertEquals(self.service.client, None)
-
-    def test_email_sending_content(self):
-        """
-        Test that email messages body and content are correctly sent using the
-        critsend api
-        """
-        self.service.client = mock.Mock()
-
-        content_obj = mock.Mock()
-        self.service.client.factory.create.return_value = content_obj
-
-        subject = 'hello'
-        text_content = 'This is an important message.'
-        html_content = '<p>This is an <strong>important</strong> message.</p>'
-
-        msg = EmailMessage(subject, text_content)
-
-        html_msg = EmailMultiAlternatives(subject, text_content)
-        html_msg.attach_alternative(html_content, "text/html")
-
-        messages = [msg,]
-        self.service.send_messages(messages)
-
-        self.assertEquals(content_obj.subject, subject)
-        self.assertEquals(content_obj.text, text_content)
-        self.assertEquals(content_obj.html, "")
-
-        content_obj = mock.Mock()
-        self.service.client.factory.create.return_value = content_obj
-
-        messages = [html_msg,]
-        self.service.send_messages(messages)
-
-        self.assertEquals(content_obj.subject, subject)
-        self.assertEquals(content_obj.text, text_content)
-        self.assertEquals(content_obj.html, html_content)
-        
-    def test_email_sending_parameters(self):
-        """
-        Test that email messages parameters are correctly set using the
-        critsend api
-        """
-
-        self.service.client = mock.Mock()
-
-        parameters_obj = mock.Mock()
-        self.service.client.factory.create.return_value = parameters_obj
-        
-        from_email = 'from@example.com'
-        from_name = 'example dude'
-        reply_to = "replyto@example.com"
-        
-        msg = EmailMessage(from_email = "%s<%s>" % (from_name, from_email),
-                           headers = {'Reply-To': reply_to})
-        
-        messages = [msg,]
-        self.service.send_messages(messages)
-        
-        self.assertEquals(parameters_obj.mailfrom, from_email)
-        self.assertEquals(parameters_obj.mailfrom_friendly, from_name)
-        self.assertEquals(parameters_obj.replyto, reply_to)
-        
-    def test_email_tags(self):
-        """
-        Test that email messages tags are correctly set using the
-        critsend api
-        """
-
-        self.service.client = mock.Mock()
-
-        tags_obj = mock.Mock()
-        self.service.client.factory.create.return_value = tags_obj
-        tags_obj.Tag = []
-        
-        msg = EmailMessage()
-        
-        messages = [msg,]
-        self.service.send_messages(messages)
-        
-        self.assertEquals(tags_obj.Tag[0], Site.objects.get_current().domain)
-        
-    def test_email_sending_subscribers(self):
-        """
-        Test that email messages recipients are correctly set using the
-        critsend api
-        """
-
-        self.service.client = mock.Mock()
-
-        parameters_obj = mock.Mock()
-        self.service.client.factory.create.return_value = parameters_obj
-        self.service._create_email = mock.Mock()
-        
-        subscribers = [
-            "foo bar<foobar@example.com>",
-            "spam eggs<spameggs@example.com>",
-        ]
-        
-        msg = EmailMessage(to = subscribers)
-        
-        messages = [msg,]
-        self.service.send_messages(messages)
-        
-        self.assertTrue(self.service._create_email.called)
-        self.assertTrue(self.service.client.service.sendCampaign.called)
-
-        
-    def test_email_creation(self):
-
-        self.service.client = mock.Mock()
-
-        email_obj = mock.Mock()
-        self.service.client.factory.create.return_value = email_obj
-        
-        email = 'foobar@example.com'
-        first_name = 'foo'
-        last_name = 'bar'
-        subscriber = "%s %s<%s>" % (first_name, last_name, email)
-        
-        mail = self.service._create_email(subscriber)        
-        
-        self.assertEquals(mail.email, email)
-        self.assertEquals(mail.field1, first_name)
-        self.assertEquals(mail.field2, last_name)
-
-    def opening_side_effect(self):
-        self.service.client = mock.Mock()
-
-    def test_email_sending(self):
-        """
-        Test that email messages send is called
-        """
-
-        self.service.open = mock.Mock()
-        self.service.open.side_effect = self.opening_side_effect
-
-        msg = EmailMessage()
-        
-        messages = [msg,]
-        self.service.send_messages(messages)
-
-        # We are sending without calling open beforehand
-        self.assertTrue(self.service.open.called)
-        self.assertTrue(self.service.client.service.sendCampaign.called)
-

email_services/tests/__init__.py

+from email_services.tests.base import EmailServiceTest
+from email_services.tests.critsend import ( CritsendBackendTest,
+                                            CritsendServiceTest )
+from email_services.tests.postmark import ( PostmarkBackendTest,
+                                            PostmarkServiceTest,
+                                            PostmarkAuthTest,
+                                            PostmarkResourceTest, )

email_services/tests/base.py

+from django.test import TestCase
+from email_services.services.base import EmailService
+
+class EmailServiceTest(TestCase):
+    
+    def setUp(self):
+        self.service = EmailService()
+
+    def test_base_non_implemented(self):
+        with self.assertRaises(Exception):
+            self.service.open()
+            
+        with self.assertRaises(Exception):
+            self.service.close()
+            
+        with self.assertRaises(Exception):
+            self.service.send_messages(messages=[])
+        

email_services/tests/critsend.py

+"""
+Tests for the email_services django application
+"""
+import urlparse
+import os
+import mock
+
+from django.test import TestCase
+from django.core.mail import EmailMessage, EmailMultiAlternatives
+from django.contrib.sites.models import Site
+
+from email_services.backends import CritsendEmailBackend
+from email_services.services import CritsendEmailService
+from email_services import settings
+from django.conf import settings as djsettings
+
+class CritsendBackendTest(TestCase):
+
+    def setUp(self):
+        self.backend = CritsendEmailBackend()
+        self.backend.service = mock.Mock(spec=CritsendEmailService)
+
+    def test_connection_delegated_to_service(self):
+        """
+        When opening a connection the action is delegated to service
+        """
+        self.backend.open()
+        self.assertTrue(self.backend.service.open.called)
+
+    def test_connection_closing_delegated_to_service(self):
+        """
+        When closing a connection the action is delegated to service
+        """
+        self.backend.close()
+        self.assertTrue(self.backend.service.close.called)
+
+    def test_connection_exception_handling_on_open(self):
+        """
+        When an exception is raised while connecting it is reraised be the backend
+        in the default non silent mode
+        """
+        self.backend.service.open.side_effect = Exception("Raised something arbitrary")
+        with self.assertRaises(Exception) as e:
+            self.backend.open()
+        
+    def test_called_service_send(self):
+        """
+        When sending email from the backend the service call method is called
+        """
+        messages = []
+        self.backend.send_messages(messages)
+        self.assertTrue(self.backend.service.send_messages.called)
+
+    def test_connection_exception_handling_on_close(self):
+        """
+        When an exception is raised while closing it is reraised be the backend
+        in the default non silent mode
+        """
+        self.backend.service.close.side_effect = Exception("Raised something arbitrary")
+        with self.assertRaises(Exception) as e:
+            self.backend.close()
+
+    def test_connection_exception_handling_on_send(self):
+        """
+        When an exception is raised while sending it is reraised be the backend
+        in the default non silent mode
+        """
+        self.backend.service.send_messages.side_effect = Exception("Raised something arbitrary")
+        with self.assertRaises(Exception) as e:
+            self.backend.send_messages([])
+
+
+class  CritsendServiceTest(TestCase):
+
+    def setUp(self):
+        self.fake_user = "QWERTY"
+        self.fake_key = "QWERTY12345"
+        self.default_user = settings.EMAIL_SERVICES_CLIENT_ID
+        self.default_key = settings.EMAIL_SERVICES_CLIENT_KEY
+        settings.EMAIL_SERVICES_CLIENT_ID = self.fake_user
+        settings.EMAIL_SERVICES_CLIENT_KEY = self.fake_key
+        self.service = CritsendEmailService()
+        
+        # Get the wsdl locally
+        self.service.wsdl = "%stest.wsdl" % os.path.sep
+        self.service.hosts = ["file://%s" % os.path.join(djsettings.PROJECT_PATH, 
+                                                         "resources")]
+        
+    def tearDown(self):
+        settings.EMAIL_SERVICES_CLIENT_ID = self.default_user
+        settings.EMAIL_SERVICES_CLIENT_KEY = self.default_key
+        
+    def test_credentials(self):
+        """
+        Test that the credentials are created
+        """
+        self.assertEquals(self.service.credentials['user'],
+                          settings.EMAIL_SERVICES_CLIENT_ID)
+        self.assertTrue(self.service.credentials['timestamp'])
+        self.assertTrue(self.service.credentials['signature'])
+        
+    def test_connection_open(self):
+        """
+        Test that the service is opening correctly
+        """
+        self.service.open()
+        url_parsed = urlparse.urlparse(self.service.client.wsdl.url)
+        
+        # We are using a local file so the host should be in path
+        # In online resources it would be different
+        self.assertTrue("%s://%s" % (url_parsed.scheme, 
+                                     os.path.dirname(url_parsed.path)) 
+                        in self.service.hosts)
+        
+        self.assertEquals(os.path.basename(url_parsed.path), 
+                          self.service.wsdl.strip(os.path.sep))
+
+        # Check that it is not recreated
+        client = self.service.client
+        self.service.open()
+        self.assertTrue(client is self.service.client)
+        
+    def test_connection_close(self):
+        """
+        Test that the service is closing correctly
+        """
+        self.service.close()
+        self.assertEquals(self.service.client, None)
+
+    def test_email_sending_content(self):
+        """
+        Test that email messages body and content are correctly sent using the
+        critsend api
+        """
+        self.service.client = mock.Mock()
+
+        content_obj = mock.Mock()
+        self.service.client.factory.create.return_value = content_obj
+
+        subject = 'hello'
+        text_content = 'This is an important message.'
+        html_content = '<p>This is an <strong>important</strong> message.</p>'
+
+        msg = EmailMessage(subject, text_content)
+
+        html_msg = EmailMultiAlternatives(subject, text_content)
+        html_msg.attach_alternative(html_content, "text/html")
+
+        messages = [msg,]
+        self.service.send_messages(messages)
+
+        self.assertEquals(content_obj.subject, subject)
+        self.assertEquals(content_obj.text, text_content)
+        self.assertEquals(content_obj.html, "")
+
+        content_obj = mock.Mock()
+        self.service.client.factory.create.return_value = content_obj
+
+        messages = [html_msg,]
+        self.service.send_messages(messages)
+
+        self.assertEquals(content_obj.subject, subject)
+        self.assertEquals(content_obj.text, text_content)
+        self.assertEquals(content_obj.html, html_content)
+        
+    def test_email_sending_parameters(self):
+        """
+        Test that email messages parameters are correctly set using the
+        critsend api
+        """
+
+        self.service.client = mock.Mock()
+
+        parameters_obj = mock.Mock()
+        self.service.client.factory.create.return_value = parameters_obj
+        
+        from_email = 'from@example.com'
+        from_name = 'example dude'
+        reply_to = "replyto@example.com"
+        
+        msg = EmailMessage(from_email = "%s<%s>" % (from_name, from_email),
+                           headers = {'Reply-To': reply_to})
+        
+        messages = [msg,]
+        self.service.send_messages(messages)
+        
+        self.assertEquals(parameters_obj.mailfrom, from_email)
+        self.assertEquals(parameters_obj.mailfrom_friendly, from_name)
+        self.assertEquals(parameters_obj.replyto, reply_to)
+        
+    def test_email_tags(self):
+        """
+        Test that email messages tags are correctly set using the
+        critsend api
+        """
+
+        self.service.client = mock.Mock()
+
+        tags_obj = mock.Mock()
+        self.service.client.factory.create.return_value = tags_obj
+        tags_obj.Tag = []
+        
+        msg = EmailMessage()
+        
+        messages = [msg,]
+        self.service.send_messages(messages)
+        
+        self.assertEquals(tags_obj.Tag[0], Site.objects.get_current().domain)
+        
+    def test_email_sending_subscribers(self):
+        """
+        Test that email messages recipients are correctly set using the
+        critsend api
+        """
+
+        self.service.client = mock.Mock()
+
+        parameters_obj = mock.Mock()
+        self.service.client.factory.create.return_value = parameters_obj
+        self.service._create_email = mock.Mock()
+        
+        subscribers = [
+            "foo bar<foobar@example.com>",
+            "spam eggs<spameggs@example.com>",
+        ]
+        
+        msg = EmailMessage(to = subscribers)
+        
+        messages = [msg,]
+        self.service.send_messages(messages)
+        
+        self.assertTrue(self.service._create_email.called)
+        self.assertTrue(self.service.client.service.sendCampaign.called)
+
+        
+    def test_email_creation(self):
+
+        self.service.client = mock.Mock()
+
+        email_obj = mock.Mock()
+        self.service.client.factory.create.return_value = email_obj
+        
+        email = 'foobar@example.com'
+        first_name = 'foo'
+        last_name = 'bar'
+        subscriber = "%s %s<%s>" % (first_name, last_name, email)
+        
+        mail = self.service._create_email(subscriber)        
+        
+        self.assertEquals(mail.email, email)
+        self.assertEquals(mail.field1, first_name)
+        self.assertEquals(mail.field2, last_name)
+
+    def opening_side_effect(self):
+        self.service.client = mock.Mock()
+
+    def test_email_sending(self):
+        """
+        Test that email messages send is called
+        """
+
+        self.service.open = mock.Mock()
+        self.service.open.side_effect = self.opening_side_effect
+
+        msg = EmailMessage()
+        
+        messages = [msg,]
+        self.service.send_messages(messages)
+
+        # We are sending without calling open beforehand
+        self.assertTrue(self.service.open.called)
+        self.assertTrue(self.service.client.service.sendCampaign.called)
+

email_services/tests/postmark.py

+"""
+Tests for the email_services django application
+"""
+import mock
+
+try:
+    import simplejson as json
+except ImportError:
+    import json  # py2.6 only
+
+from restkit import Resource
+
+from django.test import TestCase
+from django.core.mail import EmailMessage, EmailMultiAlternatives
+from django.contrib.sites.models import Site
+
+from email_services import settings
+from django.conf import settings as djsettings
+from email_services.services.postmark import (PostmarkEmailService,
+                                              PostmarkAuth,
+                                              PostmarkResource)
+from email_services.backends.postmark import PostmarkEmailBackend
+
+
+class PostmarkBackendTest(TestCase):
+
+    def setUp(self):
+        self.backend = PostmarkEmailBackend()
+        self.backend.service = mock.Mock(spec=PostmarkEmailService)
+
+    def test_connection_delegated_to_service(self):
+        """
+        When opening a connection the action is delegated to service
+        """
+        self.backend.open()
+        self.assertTrue(self.backend.service.open.called)
+
+    def test_connection_closing_delegated_to_service(self):
+        """
+        When closing a connection the action is delegated to service
+        """
+        self.backend.close()
+        self.assertTrue(self.backend.service.close.called)
+
+    def test_connection_exception_handling_on_open(self):
+        """
+        When an exception is raised while connecting it is
+        reraised be the backend in the default non silent mode
+        """
+        self.backend.service.open.side_effect = \
+        Exception("Raised something arbitrary")
+
+        with self.assertRaises(Exception) as e:
+            self.backend.open()
+
+    def test_called_service_send(self):
+        """
+        When sending email from the backend the service call method is called
+        """
+        messages = []
+        self.backend.send_messages(messages)
+        self.assertTrue(self.backend.service.send_messages.called)
+
+    def test_connection_exception_handling_on_close(self):
+        """
+        When an exception is raised while closing it is reraised be the backend
+        in the default non silent mode
+        """
+        self.backend.service.close.side_effect = \
+        Exception("Raised something arbitrary")
+
+        with self.assertRaises(Exception) as e:
+            self.backend.close()
+
+    def test_connection_exception_handling_on_send(self):
+        """
+        When an exception is raised while sending it is reraised be the backend
+        in the default non silent mode
+        """
+        self.backend.service.send_messages.side_effect = \
+        Exception("Raised something arbitrary")
+
+        with self.assertRaises(Exception) as e:
+            self.backend.send_messages([])
+
+
+class PostmarkAuthTest(TestCase):
+
+    def setUp(self):
+        self.fake_key = "QWERTY12345"
+        self.default_key = settings.EMAIL_SERVICES_CLIENT_KEY
+        settings.EMAIL_SERVICES_CLIENT_KEY = self.fake_key
+        self.auth = PostmarkAuth()
+
+    def tearDown(self):
+        settings.EMAIL_SERVICES_CLIENT_KEY = self.default_key
+
+    def test_set_auth_header(self):
+        """
+        Test that the authentication header is correctly set
+        """
+        request = mock.Mock()
+        request.headers = {}
+
+        self.auth.on_request(request)
+
+        self.assertEquals(request.headers["X-Postmark-Server-Token"],
+                          self.fake_key)
+
+
+class PostmarkResourceTest(TestCase):
+
+    def setUp(self):
+        self.fake_key = "QWERTY12345"
+        self.default_key = settings.EMAIL_SERVICES_CLIENT_KEY
+        settings.EMAIL_SERVICES_CLIENT_KEY = self.fake_key
+        self.resource = PostmarkResource()
+
+    def tearDown(self):
+        settings.EMAIL_SERVICES_CLIENT_KEY = self.default_key
+
+    def test_email_posting(self):
+        self.resource.post = mock.Mock()
+        self.resource.post.return_value = {"ErrorCode": 0}
+        ret = self.resource.email("subject", "body", to="test@example.com",
+                    from_email="sender@example.com", html_body="html",
+                    reply_to="sender@example.com")
+
+        self.assertTrue(ret)
+        self.assertTrue(self.resource.post.called)
+        self.assertEquals(self.resource.post.call_args[0], ('email',))
+
+        email_json = json.dumps({
+          "From": 'sender@example.com',
+          "To": 'test@example.com',
+          "Subject": 'subject',
+          "HtmlBody": 'html',
+          "TextBody": 'body',
+          "ReplyTo": 'sender@example.com',
+        })
+        self.assertEquals(self.resource.post.call_args[1],
+                          {'payload': email_json})
+
+    def test_email_posting_error(self):
+        self.resource.post = mock.Mock()
+        self.resource.post.return_value = {"ErrorCode": 1,
+                                           "ErrorMessage": "Message"}
+
+        with self.assertRaises(Exception) as e:
+            self.resource.email("subject", "body", to="test@example.com",
+                    from_email="sender@example.com", html_body="html",
+                    reply_to="sender@example.com")
+            
+    def test_email_posting_request(self):
+        request_func = mock.Mock()
+        Resource.request = request_func
+        response = mock.Mock()
+        response.body_string.return_value = '{}'
+        
+        Resource.request.return_value = response
+        headers = {}
+        self.resource.request(headers=headers)
+        
+        self.assertEquals(request_func.call_args[1]['headers']['Content-Type'],
+                          "application/json")
+        self.assertEquals(request_func.call_args[1]['headers']['Accept'],
+                          "application/json")
+        
+
+
+class PostmarkServiceTest(TestCase):
+
+    def setUp(self):
+        self.fake_key = "QWERTY12345"
+        self.default_key = settings.EMAIL_SERVICES_CLIENT_KEY
+        settings.EMAIL_SERVICES_CLIENT_KEY = self.fake_key
+        self.service = PostmarkEmailService()
+
+    def tearDown(self):
+        settings.EMAIL_SERVICES_CLIENT_KEY = self.default_key
+
+    def test_connection_open(self):
+        self.service.open()
+
+        self.assertTrue(isinstance(self.service.resource, PostmarkResource))
+
+        self.assertEquals(len(self.service.resource.filters), 1)
+        self.assertTrue(isinstance(self.service.resource.filters[0],
+                                   PostmarkAuth))
+
+    def test_connection_close(self):
+        self.service.open()
+        self.service.close()
+        self.assertEqual(self.service.resource, None)
+
+    def test_email_sending(self):
+        """
+        Test that email messages body and content are correctly sent
+        """
+        self.service.resource = mock.Mock()
+
+
+        subject = 'hello'
+        text_content = 'This is an important message.'
+        html_content = '<p>This is an <strong>important</strong> message.</p>'
+        from_email = 'sender@example.com'
+        to = ['recipient@example.com', ]
+        reply_to = "replyto@example.com"
+        html_msg = EmailMultiAlternatives(subject, text_content, 
+                                          from_email, to,
+                                          headers = {'Reply-To': reply_to})
+        html_msg.attach_alternative(html_content, "text/html")
+
+        messages = [html_msg,]
+        self.service.send_messages(messages)
+
+        self.assertTrue(self.service.resource.email.called)
+        self.assertEquals(self.service.resource.email.call_args[1]['subject'],
+                          subject)
+        self.assertEquals(self.service.resource.email.call_args[1]['body'],
+                          text_content)
+        self.assertEquals(self.service.resource.email.call_args[1]['html_body'],
+                          html_content)
+        self.assertEquals(self.service.resource.email.call_args[1]['from_email'],
+                          from_email)
+        self.assertEquals(self.service.resource.email.call_args[1]['reply_to'],
+                          reply_to)
+        self.assertEquals(self.service.resource.email.call_args[1]['to'],
+                          to[0])
+        
+    def opening_side_effect(self):
+        self.service.resource = mock.Mock()
+
+    def test_email_open_before_sending(self):
+        """
+        Test that resource is generated if not existing
+        """
+
+        self.service.open = mock.Mock()
+        self.service.open.side_effect = self.opening_side_effect
+
+        msg = EmailMessage()
+        
+        messages = [msg,]
+        self.service.send_messages(messages)
+
+        # We are sending without calling open beforehand
+        self.assertTrue(self.service.open.called)

test_project/manage.py

 from django.core.management import execute_manager
 import imp
 try:
-    imp.find_module('settings') # Assumed to be in the same directory.
+    imp.find_module('settings')  # Assumed to be in the same directory.
 except ImportError:
     import sys
-    sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__)
+    sys.stderr.write("""
+Error: Can't find the file 'settings.py' in the directory containing %r.
+It appears you've customized things.\nYou'll have to run django-admin.py,
+passing it your settings module.\n
+""" % __file__)
     sys.exit(1)
 
 import settings

test_project/settings.py

     'django.contrib.staticfiles',
     'south',
     'email_services',
-    'django_nose'
-)
+    'django_nose')
 
 EMAIL_BACKEND = 'email_services.backends.CritsendEmailBackend'
 
 TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
 
 NOSE_ARGS = ['--with-coverage', '--cover-package=email_services']
-

test_project/urls.py

-from django.conf.urls.defaults import patterns, include, url
-
-# Uncomment the next two lines to enable the admin:
-# from django.contrib import admin
-# admin.autodiscover()
-
-urlpatterns = patterns('',
-    # Examples:
-    # url(r'^$', 'example.views.home', name='home'),
-    # url(r'^example/', include('example.foo.urls')),
-
-    # Uncomment the admin/doc line below to enable admin documentation:
-    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
-
-    # Uncomment the next line to enable the admin:
-    # url(r'^admin/', include(admin.site.urls)),
-)
+from django.conf.urls.defaults import patterns
+urlpatterns = patterns('',)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.