66
68
self.assertEqual('test more bytes\n', t.get_bytes('foo'))
69
class TestFTPServerUI(TestCaseWithFTPServer):
71
def _add_authorized_user(self, user, password):
72
server = self.get_server()
73
# FIXME: There should be a better way to declare authorized users and
74
# passwords to the server
75
authorizer = server._ftp_server.authorizer
76
authorizer.secured_user = user
77
authorizer.secured_password = password
71
class TestFTPTestServerUI(TestCaseWithFTPServer):
74
super(TestFTPTestServerUI, self).setUp()
76
self.password = 'secret'
77
self.get_server().add_user(self.user, self.password)
79
def get_url(self, relpath=None):
80
"""Overrides get_url to inject our user."""
81
base = super(TestFTPTestServerUI, self).get_url(relpath)
82
(scheme, user, password,
83
host, port, path) = transport.ConnectedTransport._split_url(base)
84
url = transport.ConnectedTransport._unsplit_url(
85
scheme, self.user, self.password, host, port, path)
88
def test_no_prompt_for_username(self):
89
"""ensure getpass.getuser() is used if there's no username in the
91
self.get_server().add_user(getpass.getuser(), self.password)
92
t = self.get_transport()
93
ui.ui_factory = ui.CannedInputUIFactory([self.password])
94
# Issue a request to the server to connect
95
t.put_bytes('foo', 'test bytes\n')
96
self.assertEqual('test bytes\n', t.get_bytes('foo'))
97
# Only the password should've been read
98
ui.ui_factory.assert_all_input_consumed()
79
100
def test_prompt_for_password(self):
80
101
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
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)
102
ui.ui_factory = ui.CannedInputUIFactory([self.password])
91
103
# Issue a request to the server to connect
92
104
t.has('whatever/not/existing')
93
105
# stdin should be empty (the provided password have been consumed)
94
self.assertEqual('', ui.ui_factory.stdin.readline())
106
ui.ui_factory.assert_all_input_consumed()
96
108
def test_no_prompt_for_password_when_using_auth_config(self):
97
109
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
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)
110
ui.ui_factory = ui.CannedInputUIFactory([])
107
111
# Create a config file with the right password
108
112
conf = config.AuthenticationConfig()
109
113
conf._get_config().update({'ftptest': {'scheme': 'ftp',
111
'password': password}})
115
'password': self.password}})
113
117
# Issue a request to the server to connect
114
118
t.put_bytes('foo', 'test bytes\n')
115
119
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())
121
def test_empty_password(self):
122
# Override the default user/password from setUp
125
self.get_server().add_user(self.user, self.password)
126
t = self.get_transport()
127
ui.ui_factory = ui.CannedInputUIFactory([self.password])
128
# Issue a request to the server to connect
129
t.has('whatever/not/existing')
130
# stdin should be empty (the provided password have been consumed),
131
# even if the password is empty, it's followed by a newline.
132
ui.ui_factory.assert_all_input_consumed()