~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ftp_transport.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-11-04 18:51:39 UTC
  • mfrom: (2961.1.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20071104185139-kaio3sneodg2kp71
Authentication ring implementation (read-only)

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
 
17
from cStringIO import StringIO
17
18
import sys
18
19
 
19
20
from bzrlib import (
 
21
    config,
20
22
    tests,
21
23
    transport,
22
24
    ui,
66
68
 
67
69
class TestFTPServerUI(TestCaseWithFTPServer):
68
70
 
69
 
    def setUp(self):
70
 
        super(TestFTPServerUI, self).setUp()
71
 
        self.old_factory = ui.ui_factory
72
 
        # The following has the unfortunate side-effect of hiding any ouput
73
 
        # during the tests (including pdb prompts). Feel free to comment them
74
 
        # for debugging purposes but leave them in place, there are needed to
75
 
        # run the tests without any console
76
 
        self.old_stdout = sys.stdout
77
 
        sys.stdout = tests.StringIOWrapper()
78
 
        self.addCleanup(self.restoreUIFactory)
79
 
 
80
 
    def restoreUIFactory(self):
81
 
        ui.ui_factory = self.old_factory
82
 
        sys.stdout = self.old_stdout
83
 
 
84
 
    def test_prompt_for_password(self):
85
 
        t = self.get_transport()
86
 
        # Ensure that the test framework set the password
87
 
        self.assertIsNot(t._password, None)
88
 
        # Reset the password (get_url set the password to 'bar' so we
89
 
        # reset it to None in the transport before the connection).
90
 
        password = t._password
91
 
        t._password = None
92
 
        ui.ui_factory = tests.TestUIFactory(stdin=password+'\n')
93
 
        # Ask the server to check the password
 
71
    def _add_authorized_user(self, user, password):
94
72
        server = self.get_server()
95
73
        # FIXME: There should be a better way to declare authorized users and
96
74
        # passwords to the server
97
75
        authorizer = server._ftp_server.authorizer
98
 
        authorizer.secured_user = t._user
 
76
        authorizer.secured_user = user
99
77
        authorizer.secured_password = password
 
78
 
 
79
    def test_prompt_for_password(self):
 
80
        t = self.get_transport()
 
81
        # Ensure that the test framework set the password
 
82
        self.assertIsNot(t._password, None)
 
83
        # Reset the password (get_url set the password to 'bar' so we
 
84
        # reset it to None in the transport before the connection).
 
85
        password = t._password
 
86
        t._password = None
 
87
        ui.ui_factory = tests.TestUIFactory(stdin=password+'\n',
 
88
                                            stdout=tests.StringIOWrapper())
 
89
        # Ask the server to check the password
 
90
        self._add_authorized_user(t._user, password)
100
91
        # Issue a request to the server to connect
101
92
        t.has('whatever/not/existing')
102
93
        # stdin should be empty (the provided password have been consumed)
103
94
        self.assertEqual('', ui.ui_factory.stdin.readline())
 
95
 
 
96
    def test_no_prompt_for_password_when_using_auth_config(self):
 
97
        t = self.get_transport()
 
98
        # Reset the password (get_url set the password to 'bar' so we
 
99
        # reset it to None in the transport before the connection).
 
100
        password = t._password
 
101
        t._password = None
 
102
        ui.ui_factory = tests.TestUIFactory(stdin='precious\n',
 
103
                                            stdout=tests.StringIOWrapper())
 
104
        # Ask the server to check the password
 
105
        self._add_authorized_user(t._user, password)
 
106
 
 
107
        # Create a config file with the right password
 
108
        conf = config.AuthenticationConfig()
 
109
        conf._get_config().update({'ftptest': {'scheme': 'ftp',
 
110
                                               'user': t._user,
 
111
                                               'password': password}})
 
112
        conf._save()
 
113
        # Issue a request to the server to connect
 
114
        t.put_bytes('foo', 'test bytes\n')
 
115
        self.assertEqual('test bytes\n', t.get_bytes('foo'))
 
116
        # stdin should have  been left untouched
 
117
        self.assertEqual('precious\n', ui.ui_factory.stdin.readline())