From fdbd1cc43895dc02158f39a161c0d596f6f7cfaa Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Tue, 21 Jul 2015 16:39:01 +1200 Subject: [PATCH] Support multiple configurations per livetest config file These are now used by the test_alternate livetest case. This allows quicker testing different TLS and authentication configuration without requiring a separate livetest file (and therefore a separate full livetest run). Configurations that are expected to failre are now also allowed to support testing of the various new TLS options. --- NEWS.rst | 2 ++ imapclient/config.py | 64 ++++++++++++++++++++++++++---------------- imapclient/livetest.py | 21 +++++++++++++- 3 files changed, 62 insertions(+), 25 deletions(-) diff --git a/NEWS.rst b/NEWS.rst index 534d579..9e29a1e 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -2,7 +2,9 @@ 1.0.0 ======= +XXX TLS support XXX Removed kwargs support from constructor - replaced by ssl_context +XXX livetest config file change re DEFAULT ====== 0.13 diff --git a/imapclient/config.py b/imapclient/config.py index 7014f4c..dac6c20 100644 --- a/imapclient/config.py +++ b/imapclient/config.py @@ -4,6 +4,7 @@ from __future__ import unicode_literals +# TODO: can't six do this for us? try: from ConfigParser import SafeConfigParser, NoOptionError except ImportError: @@ -45,44 +46,59 @@ def parse_config_file(filename): oauth2_client_id=None, oauth2_client_secret=None, oauth2_refresh_token=None, + expect_failure=None, )) with open(filename, 'r') as fh: parser.readfp(fh) - section = 'main' - assert parser.sections() == [section], 'Only expected a [main] section' - try: - port = parser.getint(section, 'port') - except NoOptionError: - port = None + conf = _read_config_section(parser, "DEFAULT") + if conf.expect_failure: + raise ValueError("expect_failure should not be set for the DEFAULT section") - ssl_ca_file = parser.get(section, 'ssl_ca_file') + conf.alternates = {} + for section in parser.sections(): + conf.alternates[section] = _read_config_section(parser, section) + + return conf + +def _read_config_section(parser, section): + get = lambda name: parser.get(section, name) + getboolean = lambda name: parser.getboolean(section, name) + def getint(name): + try: + return parser.getint(section, name) + except NoOptionError: + return None + + ssl_ca_file = get('ssl_ca_file') if ssl_ca_file: ssl_ca_file = path.expanduser(ssl_ca_file) return Bunch( - host=parser.get(section, 'host'), - port=port, - ssl=parser.getboolean(section, 'ssl'), - starttls=parser.getboolean(section, 'starttls'), - ssl_check_hostname=parser.getboolean(section, 'ssl_check_hostname'), - ssl_verify_cert=parser.getboolean(section, 'ssl_verify_cert'), + host=get('host'), + port=getint('port'), + ssl=getboolean('ssl'), + starttls=getboolean('starttls'), + ssl_check_hostname=getboolean('ssl_check_hostname'), + ssl_verify_cert=getboolean('ssl_verify_cert'), ssl_ca_file=ssl_ca_file, - stream=parser.getboolean(section, 'stream'), + stream=getboolean('stream'), + + username=get('username'), + password=get('password'), - username=parser.get(section, 'username'), - password=parser.get(section, 'password'), + oauth=getboolean('oauth'), + oauth_url=get('oauth_url'), + oauth_token=get('oauth_token'), + oauth_token_secret=get('oauth_token_secret'), - oauth=parser.getboolean(section, 'oauth'), - oauth_url=parser.get(section, 'oauth_url'), - oauth_token=parser.get(section, 'oauth_token'), - oauth_token_secret=parser.get(section, 'oauth_token_secret'), + oauth2=getboolean('oauth2'), + oauth2_client_id=get('oauth2_client_id'), + oauth2_client_secret=get('oauth2_client_secret'), + oauth2_refresh_token=get('oauth2_refresh_token'), - oauth2=parser.getboolean(section, 'oauth2'), - oauth2_client_id=parser.get(section, 'oauth2_client_id'), - oauth2_client_secret=parser.get(section, 'oauth2_client_secret'), - oauth2_refresh_token=parser.get(section, 'oauth2_refresh_token'), + expect_failure=get('expect_failure') ) def refresh_oauth2_token(client_id, client_secret, refresh_token): diff --git a/imapclient/livetest.py b/imapclient/livetest.py index f8325af..e572452 100644 --- a/imapclient/livetest.py +++ b/imapclient/livetest.py @@ -18,7 +18,7 @@ from email.utils import make_msgid from .fixed_offset import FixedOffset from .imapclient import IMAPClient, DELETED, to_unicode, to_bytes, normalise_untagged_responses from .response_types import Envelope, Address -from .six import binary_type, text_type, PY3 +from .six import binary_type, text_type, PY3, iteritems from .test.util import unittest from .config import parse_config_file, create_client_from_config @@ -174,6 +174,25 @@ class TestGeneral(_TestBase): Tests that don't involve message number/UID functionality. """ + def test_alternates(self): + # Check alternate connection/login configurations. + for name, conf in iteritems(self.conf.alternates): + if PY3 and conf.oauth: + print("Skipping OAUTH test %r on Python 3 (not compatible)" % name) + continue + try: + client = create_client_from_config(conf) + client.logout() + except Exception as err: + if conf.expect_failure: + if conf.expect_failure not in str(err): + self.fail("connection test %r failed with %r, expected %r" % (name, err, conf.expect_failure)) + else: + self.fail("connection test %r failed unexpectedly with %r" % (name, err)) + else: + if conf.expect_failure: + self.fail("connection test %r didn't fail, expected %r" % (name, conf.expect_failure)) + def test_capabilities(self): caps = self.client.capabilities() self.assertIsInstance(caps, tuple) -- 2.43.0