diff options
author | Kali Kaneko <kali@leap.se> | 2016-08-24 13:19:16 -0400 |
---|---|---|
committer | Kali Kaneko <kali@leap.se> | 2016-08-25 11:13:20 -0400 |
commit | 6d1284873e379901dbdc3ac5eb7a730cf26d79b1 (patch) | |
tree | 20f2bd59c57538ad166cfd5881c792790ff4fc7e /mail | |
parent | eee94a3dde0d8ecdfed81ef6e7ffa1f950250b72 (diff) |
[tests] adapt smtp module tests
Diffstat (limited to 'mail')
-rw-r--r-- | mail/src/leap/mail/smtp/gateway.py | 1 | ||||
-rw-r--r-- | mail/src/leap/mail/smtp/tests/test_gateway.py | 87 |
2 files changed, 24 insertions, 64 deletions
diff --git a/mail/src/leap/mail/smtp/gateway.py b/mail/src/leap/mail/smtp/gateway.py index 74676089..e49bbe82 100644 --- a/mail/src/leap/mail/smtp/gateway.py +++ b/mail/src/leap/mail/smtp/gateway.py @@ -113,7 +113,6 @@ class LocalSMTPRealm(object): return d def lookupKeymanagerInstance(self, userid): - print 'getting KM INSTNACE>>>' try: keymanager = self._keymanager_sessions[userid] except: diff --git a/mail/src/leap/mail/smtp/tests/test_gateway.py b/mail/src/leap/mail/smtp/tests/test_gateway.py index de31e110..9d88afb8 100644 --- a/mail/src/leap/mail/smtp/tests/test_gateway.py +++ b/mail/src/leap/mail/smtp/tests/test_gateway.py @@ -18,23 +18,20 @@ """ SMTP gateway tests. """ - import re +import tempfile from datetime import datetime -from twisted.mail import smtp from twisted.internet import reactor from twisted.internet.defer import inlineCallbacks, fail, succeed, Deferred from twisted.test import proto_helpers from mock import Mock -from leap.mail.smtp.gateway import SMTPFactory, LOCAL_FQDN -from leap.mail.smtp.gateway import SMTPDelivery -from leap.mail.outgoing.service import outgoingFactory -from leap.mail.tests import TestCaseWithKeyManager -from leap.mail.tests import ADDRESS, ADDRESS_2 from leap.keymanager import openpgp, errors +from leap.mail.testing import KeyManagerWithSoledadTestCase +from leap.mail.testing import ADDRESS, ADDRESS_2 +from leap.mail.testing.smtp import getSMTPFactory, TEST_USER # some regexps @@ -44,54 +41,8 @@ HOSTNAME_REGEX = "(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*" + \ "([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])" IP_OR_HOST_REGEX = '(' + IP_REGEX + '|' + HOSTNAME_REGEX + ')' -TEST_USER = u'anotheruser@leap.se' - - -def getSMTPFactory(soledad_s, keymanager_s, sendmail_opts, - encrypted_only=False): - factory = UnauthenticatedSMTPFactory - factory.encrypted_only = encrypted_only - proto = factory( - soledad_s, keymanager_s, sendmail_opts).buildProtocol(('127.0.0.1', 0)) - return proto - - -class UnauthenticatedSMTPServer(smtp.SMTP): - - encrypted_only = False - - def __init__(self, soledads, keyms, opts, encrypted_only=False): - smtp.SMTP.__init__(self) - - userid = TEST_USER - keym = keyms[userid] - class Opts: - cert = '/tmp/cert' - key = '/tmp/cert' - hostname = 'remote' - port = 666 - - outgoing = outgoingFactory( - userid, keym, Opts, check_cert=False) - avatar = SMTPDelivery(userid, keym, encrypted_only, outgoing) - self.delivery = avatar - - def validateFrom(self, helo, origin): - return origin - - -class UnauthenticatedSMTPFactory(SMTPFactory): - """ - A Factory that produces a SMTP server that does not authenticate user. - Only for tests! - """ - protocol = UnauthenticatedSMTPServer - domain = LOCAL_FQDN - encrypted_only = False - - -class TestSmtpGateway(TestCaseWithKeyManager): +class TestSmtpGateway(KeyManagerWithSoledadTestCase): EMAIL_DATA = ['HELO gateway.leap.se', 'MAIL FROM: <%s>' % ADDRESS_2, @@ -109,6 +60,16 @@ class TestSmtpGateway(TestCaseWithKeyManager): '.', 'QUIT'] + def setUp(self): + # pytest handles correctly the setupEnv for the class, + # but trial ignores it. + if not getattr(self, 'tempdir', None): + self.tempdir = tempfile.mkdtemp() + return KeyManagerWithSoledadTestCase.setUp(self) + + def tearDown(self): + return KeyManagerWithSoledadTestCase.tearDown(self) + def assertMatch(self, string, pattern, msg=None): if not re.match(pattern, string): msg = self._formatMessage(msg, '"%s" does not match pattern "%s".' @@ -130,7 +91,7 @@ class TestSmtpGateway(TestCaseWithKeyManager): '354 Continue'] user = TEST_USER - proto = getSMTPFactory({user: None}, {user: self._km}, {user: None}) + proto = getSMTPFactory({user: None}, {user: self.km}, {user: None}) transport = proto_helpers.StringTransport() proto.makeConnection(transport) reply = "" @@ -147,16 +108,16 @@ class TestSmtpGateway(TestCaseWithKeyManager): True. """ # remove key from key manager - pubkey = yield self._km.get_key(ADDRESS) + pubkey = yield self.km.get_key(ADDRESS) pgp = openpgp.OpenPGPScheme( - self._soledad, gpgbinary=self.GPG_BINARY_PATH) + self._soledad, gpgbinary=self.gpg_binary_path) yield pgp.delete_key(pubkey) # mock the key fetching - self._km._fetch_keys_from_server = Mock( + self.km._fetch_keys_from_server = Mock( return_value=fail(errors.KeyNotFound())) user = TEST_USER proto = getSMTPFactory( - {user: None}, {user: self._km}, {user: None}, + {user: None}, {user: self.km}, {user: None}, encrypted_only=True) transport = proto_helpers.StringTransport() proto.makeConnection(transport) @@ -178,15 +139,15 @@ class TestSmtpGateway(TestCaseWithKeyManager): False. """ # remove key from key manager - pubkey = yield self._km.get_key(ADDRESS) + pubkey = yield self.km.get_key(ADDRESS) pgp = openpgp.OpenPGPScheme( - self._soledad, gpgbinary=self.GPG_BINARY_PATH) + self._soledad, gpgbinary=self.gpg_binary_path) yield pgp.delete_key(pubkey) # mock the key fetching - self._km._fetch_keys_from_server = Mock( + self.km._fetch_keys_from_server = Mock( return_value=fail(errors.KeyNotFound())) user = TEST_USER - proto = getSMTPFactory({user: None}, {user: self._km}, {user: None}) + proto = getSMTPFactory({user: None}, {user: self.km}, {user: None}) transport = proto_helpers.StringTransport() proto.makeConnection(transport) yield self.getReply(self.EMAIL_DATA[0] + '\r\n', proto, transport) |