~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smtp_connection.py

  • Committer: Vincent Ladeuil
  • Date: 2007-10-20 16:49:14 UTC
  • mto: (2961.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 2962.
  • Revision ID: v.ladeuil+lp@free.fr-20071020164914-zr8s4gimxnbmvecd
Since all schemes query AuthenticationConfig then prompt user, make that
a method.

* bzrlib/transport/ssh.py:
(_paramiko_auth): Simplified.

* bzrlib/transport/http/_urllib2_wrappers.py:
Add FIXME about https never mentioned in prompts.
(AbstractAuthHandler.get_password): Simplified.

* bzrlib/transport/ftp.py:
(FtpTransport._create_connection): Simplified.

* bzrlib/tests/test_smtp_connection.py:
(TestSMTPConnection.test_smtp_password_from_user): Moved from
TestSMTPConnectionWithUI and given a proper stdout for ui_factory.
(TestSMTPConnectionWithUI): Deleted.

* bzrlib/tests/test_http.py:
(TestAuth.setUp): Mother class already install a SilentUIFactory
and restore the proper one, so there is no need to do that
ourselves.
(TestAuth.restoreUIFactory): Deleted.
(TestAuth.test_prompt_for_password,
TestAuth.test_no_prompt_for_password_when_using_auth_config):
Provides a suitable stdout to TestUIFactory.


* bzrlib/tests/test_ftp_transport.py:
(TestFTPServerUI.setUp, TestFTPServerUI.restoreUIFactory):
Deleted. Mother class already install a SilentUIFactory and
restore the proper one, so there is no need to do that ourselves.
(TestFTPServerUI.test_prompt_for_password,
TestFTPServerUI.test_no_prompt_for_password_when_using_auth_config):
Provides a suitable stdout to TestUIFactory.

* bzrlib/smtp_connection.py:
(SMTPConnection._authenticate): Simplified.

* bzrlib/config.py:
(AuthenticationConfig.get_password): Encapsulate pattern common to
all schemes actually used.

Show diffs side-by-side

added added

removed removed

Lines of Context:
101
101
        conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
102
102
        self.assertEqual(u'mypass', conn._smtp_password)
103
103
 
 
104
    def test_smtp_password_from_user(self):
 
105
        user = 'joe'
 
106
        password = 'hispass'
 
107
        conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user,
 
108
                                   smtp_factory=everybody_is_welcome)
 
109
        self.assertIs(None, conn._smtp_password)
 
110
 
 
111
        ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
 
112
                                            stdout=tests.StringIOWrapper())
 
113
        conn._connect()
 
114
        self.assertEqual(password, conn._smtp_password)
 
115
        # stdin should be empty (the provided password have been consumed)
 
116
        self.assertEqual('', ui.ui_factory.stdin.readline())
 
117
 
104
118
    def test_smtp_password_from_auth_config(self):
105
119
        user = 'joe'
106
120
        password = 'hispass'
146
160
        self.assertEqual('jrandom@example.com', from_)
147
161
        self.assertEqual(sorted(['john@doe.com', 'jane@doe.com',
148
162
            'pperez@ejemplo.com', 'user@localhost']), sorted(to))
 
163
 
149
164
    def test_destination_address_required(self):
150
165
        class FakeConfig:
151
166
            def get_user_option(self, option):
166
181
        self.assertRaises(
167
182
            errors.NoDestinationAddress,
168
183
            smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
169
 
 
170
 
 
171
 
class TestSMTPConnectionWithUI(tests.TestCaseInTempDir):
172
 
 
173
 
    def setUp(self):
174
 
        super(TestSMTPConnectionWithUI, self).setUp()
175
 
        self.old_factory = ui.ui_factory
176
 
        # The following has the unfortunate side-effect of hiding any ouput
177
 
        # during the tests (including pdb prompts). Feel free to comment them
178
 
        # for debugging purposes but leave them in place, there are needed to
179
 
        # run the tests without any console
180
 
        self.old_stdout = sys.stdout
181
 
        sys.stdout = tests.StringIOWrapper()
182
 
        self.addCleanup(self.restoreUIFactory)
183
 
 
184
 
    def restoreUIFactory(self):
185
 
        ui.ui_factory = self.old_factory
186
 
        sys.stdout = self.old_stdout
187
 
 
188
 
    def get_connection(self, text, smtp_factory=None):
189
 
        return _get_connection(text, smtp_factory)
190
 
 
191
 
    def test_smtp_password_from_user(self):
192
 
        user = 'joe'
193
 
        password = 'hispass'
194
 
        conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user,
195
 
                                   smtp_factory=everybody_is_welcome)
196
 
        self.assertIs(None, conn._smtp_password)
197
 
 
198
 
        ui.ui_factory = tests.TestUIFactory(stdin=password + '\n')
199
 
        conn._connect()
200
 
        self.assertEqual(password, conn._smtp_password)
201
 
        # stdin should be empty (the provided password have been consumed)
202
 
        self.assertEqual('', ui.ui_factory.stdin.readline())
203