]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Support multiple configurations per livetest config file
authorMenno Smits <menno@freshfoo.com>
Tue, 21 Jul 2015 04:39:01 +0000 (16:39 +1200)
committerMenno Smits <menno@freshfoo.com>
Tue, 21 Jul 2015 04:39:01 +0000 (16:39 +1200)
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
imapclient/config.py
imapclient/livetest.py

index 534d579696391b13a1a6746034ce110fe422bcf1..9e29a1e7cd3ac5fcbdc7546c049d6bec16c2d6b7 100644 (file)
--- 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
index 7014f4cd912ca0e15449dbc7d82aa3f9f277e3c7..dac6c2082e23a66a4e059475813fbcbe71a8735e 100644 (file)
@@ -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):
index f8325affb2d6e3d5ad3885f9edaf70d824a65992..e5724522a631c7ea89254fb79b5994dfd6015e1f 100644 (file)
@@ -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)