67
69
class TestFTPServerUI(TestCaseWithFTPServer):
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)
80
def restoreUIFactory(self):
81
ui.ui_factory = self.old_factory
82
sys.stdout = self.old_stdout
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
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
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
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())
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
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)
107
# Create a config file with the right password
108
conf = config.AuthenticationConfig()
109
conf._get_config().update({'ftptest': {'scheme': 'ftp',
111
'password': password}})
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())