tests for basic Mail functionality
This commit is contained in:
+142
-2
@@ -6,6 +6,7 @@
|
|||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import smtplib
|
||||||
if sys.version < "2.7":
|
if sys.version < "2.7":
|
||||||
import unittest2 as unittest
|
import unittest2 as unittest
|
||||||
else:
|
else:
|
||||||
@@ -19,7 +20,7 @@ DEFAULT_URI = os.getenv('DB', 'sqlite:memory')
|
|||||||
|
|
||||||
from gluon.dal import DAL, Field
|
from gluon.dal import DAL, Field
|
||||||
from pydal.objects import Table
|
from pydal.objects import Table
|
||||||
from tools import Auth
|
from tools import Auth, Mail
|
||||||
from gluon.globals import Request, Response, Session
|
from gluon.globals import Request, Response, Session
|
||||||
from storage import Storage
|
from storage import Storage
|
||||||
from languages import translator
|
from languages import translator
|
||||||
@@ -29,7 +30,7 @@ python_version = sys.version[:3]
|
|||||||
IS_IMAP = "imap" in DEFAULT_URI
|
IS_IMAP = "imap" in DEFAULT_URI
|
||||||
|
|
||||||
@unittest.skipIf(IS_IMAP, "TODO: Imap raises 'Connection refused'")
|
@unittest.skipIf(IS_IMAP, "TODO: Imap raises 'Connection refused'")
|
||||||
class testAuth(unittest.TestCase):
|
class TestAuth(unittest.TestCase):
|
||||||
|
|
||||||
def testRun(self):
|
def testRun(self):
|
||||||
# setup
|
# setup
|
||||||
@@ -79,5 +80,144 @@ class testAuth(unittest.TestCase):
|
|||||||
pass
|
pass
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
class TestMail(unittest.TestCase):
|
||||||
|
"""
|
||||||
|
Test the Mail class.
|
||||||
|
"""
|
||||||
|
|
||||||
|
class Message(object):
|
||||||
|
def __init__(self,sender, to, payload):
|
||||||
|
self.sender = sender
|
||||||
|
self.to = to
|
||||||
|
self.payload = payload
|
||||||
|
|
||||||
|
class DummySMTP(object):
|
||||||
|
"""
|
||||||
|
Dummy smtp server
|
||||||
|
|
||||||
|
NOTE: Test methods should take care of always leaving inbox and users empty when they finish.
|
||||||
|
"""
|
||||||
|
inbox = []
|
||||||
|
users = {}
|
||||||
|
|
||||||
|
def __init__(self,address, port, **kwargs):
|
||||||
|
self.address=address
|
||||||
|
self.port = port
|
||||||
|
self.has_quit = False
|
||||||
|
self.tls = False
|
||||||
|
|
||||||
|
def login(self,username,password):
|
||||||
|
if username not in self.users or self.users[username] != password:
|
||||||
|
raise smtplib.SMTPAuthenticationError
|
||||||
|
self.username=username
|
||||||
|
self.password=password
|
||||||
|
|
||||||
|
def sendmail(self,from_address,to_address,fullmessage):
|
||||||
|
self.inbox.append(TestMail.Message(from_address,to_address,fullmessage))
|
||||||
|
return []
|
||||||
|
|
||||||
|
def quit(self):
|
||||||
|
self.has_quit=True
|
||||||
|
|
||||||
|
def ehlo(self, hostname=None):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def starttls(self):
|
||||||
|
self.tls = True
|
||||||
|
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.original_SMTP = smtplib.SMTP
|
||||||
|
self.original_SMTP_SSL = smtplib.SMTP_SSL
|
||||||
|
smtplib.SMTP = TestMail.DummySMTP
|
||||||
|
smtplib.SMTP_SSL = TestMail.DummySMTP
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
smtplib.SMTP = self.original_SMTP
|
||||||
|
smtplib.SMTP_SSL = self.original_SMTP_SSL
|
||||||
|
|
||||||
|
def test_hello_world(self):
|
||||||
|
mail = Mail()
|
||||||
|
mail.settings.server = 'smtp.example.com:25'
|
||||||
|
mail.settings.sender = 'you@example.com'
|
||||||
|
self.assertTrue(mail.send(to=['somebody@example.com'],
|
||||||
|
subject='hello',
|
||||||
|
# If reply_to is omitted, then mail.settings.sender is used
|
||||||
|
reply_to='us@example.com',
|
||||||
|
message='world'))
|
||||||
|
message = TestMail.DummySMTP.inbox.pop()
|
||||||
|
self.assertEqual(message.sender, mail.settings.sender)
|
||||||
|
self.assertEqual(message.to, ['somebody@example.com'])
|
||||||
|
header = "To: somebody@example.com\nReply-To: us@example.com\nSubject: hello\n"
|
||||||
|
self.assertTrue(header in message.payload)
|
||||||
|
self.assertTrue(message.payload.endswith('world'))
|
||||||
|
|
||||||
|
def test_failed_login(self):
|
||||||
|
mail = Mail()
|
||||||
|
mail.settings.server = 'smtp.example.com:25'
|
||||||
|
mail.settings.sender = 'you@example.com'
|
||||||
|
mail.settings.login = 'username:password'
|
||||||
|
self.assertFalse(mail.send(to=['somebody@example.com'],
|
||||||
|
subject='hello',
|
||||||
|
# If reply_to is omitted, then mail.settings.sender is used
|
||||||
|
reply_to='us@example.com',
|
||||||
|
message='world'))
|
||||||
|
|
||||||
|
def test_login(self):
|
||||||
|
TestMail.DummySMTP.users['username'] = 'password'
|
||||||
|
mail = Mail()
|
||||||
|
mail.settings.server = 'smtp.example.com:25'
|
||||||
|
mail.settings.sender = 'you@example.com'
|
||||||
|
mail.settings.login = 'username:password'
|
||||||
|
self.assertTrue(mail.send(to=['somebody@example.com'],
|
||||||
|
subject='hello',
|
||||||
|
# If reply_to is omitted, then mail.settings.sender is used
|
||||||
|
reply_to='us@example.com',
|
||||||
|
message='world'))
|
||||||
|
del TestMail.DummySMTP.users['username']
|
||||||
|
TestMail.DummySMTP.inbox.pop()
|
||||||
|
|
||||||
|
def test_html(self):
|
||||||
|
mail = Mail()
|
||||||
|
mail.settings.server = 'smtp.example.com:25'
|
||||||
|
mail.settings.sender = 'you@example.com'
|
||||||
|
self.assertTrue(mail.send(to=['somebody@example.com'],
|
||||||
|
subject='hello',
|
||||||
|
# If reply_to is omitted, then mail.settings.sender is used
|
||||||
|
reply_to='us@example.com',
|
||||||
|
message='<html><head></head><body></body></html>'))
|
||||||
|
message = TestMail.DummySMTP.inbox.pop()
|
||||||
|
self.assertTrue('Content-Type: text/html' in message.payload)
|
||||||
|
|
||||||
|
def test_ssl(self):
|
||||||
|
mail = Mail()
|
||||||
|
mail.settings.server = 'smtp.example.com:25'
|
||||||
|
mail.settings.sender = 'you@example.com'
|
||||||
|
mail.settings.ssl = True
|
||||||
|
self.assertTrue(mail.send(to=['somebody@example.com'],
|
||||||
|
subject='hello',
|
||||||
|
# If reply_to is omitted, then mail.settings.sender is used
|
||||||
|
reply_to='us@example.com',
|
||||||
|
message='world'))
|
||||||
|
TestMail.DummySMTP.inbox.pop()
|
||||||
|
|
||||||
|
def test_tls(self):
|
||||||
|
mail = Mail()
|
||||||
|
mail.settings.server = 'smtp.example.com:25'
|
||||||
|
mail.settings.sender = 'you@example.com'
|
||||||
|
mail.settings.tls = True
|
||||||
|
self.assertTrue(mail.send(to=['somebody@example.com'],
|
||||||
|
subject='hello',
|
||||||
|
# If reply_to is omitted, then mail.settings.sender is used
|
||||||
|
reply_to='us@example.com',
|
||||||
|
message='world'))
|
||||||
|
TestMail.DummySMTP.inbox.pop()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user