From c608fff72d6efa54dde018f7c26ca823a97c2e1e Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Tue, 14 Jul 2015 11:24:28 +1200 Subject: [PATCH] Added starttls unit tests and cleaned up starttls - use _checkok() to check command response - set _starttls_done, not _tls_established - return data[0], don't check untagged responses Also standardised error raised by _check_resp() across all Python versions. --- imapclient/imapclient.py | 13 +++---- imapclient/test/test_starttls.py | 63 ++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 imapclient/test/test_starttls.py diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 7208a94..b08e922 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -177,16 +177,15 @@ class IMAPClient(object): or an SSL connection is already established. """ if self.ssl or self._starttls_done: - raise self.abort('TLS session already established') + raise self.AbortError('TLS session already established') - typ, dat = self._imap._simple_command("STARTTLS") - if typ != 'OK': - raise self.Error("Couldn't establish TLS session") + typ, data = self._imap._simple_command("STARTTLS") + self._checkok('starttls', typ, data) - self._tls_established = True + self._starttls_done = True self._imap.sock = tls.wrap_socket(self._imap.sock, self.host, ssl_context) self._imap.file = self._imap.sock.makefile() - return self._imap._untagged_response(typ, dat, "STARTTLS") + return data[0] def login(self, username, password): """Login using *username* and *password*, returning the @@ -1000,7 +999,7 @@ class IMAPClient(object): Raises IMAPClient.Error if the command fails. """ if typ != expected: - raise self.Error('%s failed: %r' % (command, data[0])) + raise self.Error("%s failed: %s" % (command, to_unicode(data[0]))) def _consume_until_tagged_response(self, tag, command): tagged_commands = self._imap.tagged_commands diff --git a/imapclient/test/test_starttls.py b/imapclient/test/test_starttls.py new file mode 100644 index 0000000..a8ad7ee --- /dev/null +++ b/imapclient/test/test_starttls.py @@ -0,0 +1,63 @@ +# Copyright (c) 2015, Menno Smits +# Released subject to the New BSD License +# Please see http://en.wikipedia.org/wiki/BSD_licenses + +from __future__ import unicode_literals + +from mock import Mock, patch, sentinel + +from ..imapclient import IMAPClient +from .imapclient_test import IMAPClientTest + + +class TestStarttls(IMAPClientTest): + + def setUp(self): + super(TestStarttls, self).setUp() + + patcher = patch("imapclient.imapclient.tls") + self.tls = patcher.start() + self.addCleanup(patcher.stop) + + self.client._imap.sock = sentinel.old_sock + + self.new_sock = Mock() + self.new_sock.makefile.return_value = sentinel.file + self.tls.wrap_socket.return_value = self.new_sock + + self.client.host = sentinel.host + self.client.ssl = False + self.client._starttls_done = False + self.client._imap._simple_command.return_value = "OK", [b'start TLS negotiation'] + + def test_works(self): + resp = self.client.starttls(sentinel.ssl_context) + + self.tls.wrap_socket.assert_called_once_with( + sentinel.old_sock, + sentinel.host, + sentinel.ssl_context, + ) + self.new_sock.makefile.assert_called_once_with() + self.assertEqual(self.client._imap.file, sentinel.file) + self.assertEqual(resp, b'start TLS negotiation') + + def test_command_fails(self): + self.client._imap._simple_command.return_value = "NO", [b'sorry'] + + with self.assertRaises(IMAPClient.Error) as raised: + self.client.starttls(sentinel.ssl_context) + self.assertEqual(str(raised.exception), "starttls failed: sorry") + + def test_fails_if_called_twice(self): + self.client.starttls(sentinel.ssl_context) + self.assert_tls_already_established() + + def test_fails_if_ssl_true(self): + self.client.ssl = True + self.assert_tls_already_established() + + def assert_tls_already_established(self): + with self.assertRaises(IMAPClient.AbortError) as raised: + self.client.starttls(sentinel.ssl_context) + self.assertEqual(str(raised.exception), "TLS session already established") -- 2.43.0