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
17
from cStringIO import StringIO
19
20
from bzrlib import (
26
class _MedusaFeature(tests.Feature):
27
"""Some tests want an FTP Server, check if one is available.
29
Right now, the only way this is available is if 'medusa' is installed.
36
import medusa.ftp_server
41
def feature_name(self):
44
MedusaFeature = _MedusaFeature()
47
28
class TestCaseWithFTPServer(tests.TestCaseWithTransport):
49
_test_needs_features = [MedusaFeature]
30
_test_needs_features = [tests.FTPServerFeature]
52
from bzrlib.transport.ftp import FtpServer
53
self.transport_server = FtpServer
33
from bzrlib.tests import ftp_server
34
self.transport_server = ftp_server.FTPServer
54
35
super(TestCaseWithFTPServer, self).setUp()
58
38
class TestCaseAFTP(tests.TestCaseWithTransport):
59
39
"""Test aftp transport."""
77
57
t.put_bytes('foo', 'test bytes\n')
78
58
self.assertEqual('test bytes\n', t.get_bytes('foo'))
60
def test__reconnect(self):
61
t = self.get_transport()
62
t.put_bytes('foo', 'test bytes\n')
63
self.assertEqual('test bytes\n', t.get_bytes('foo'))
65
t.put_bytes('foo', 'test more bytes\n')
66
self.assertEqual('test more bytes\n', t.get_bytes('foo'))
81
69
class TestFTPServerUI(TestCaseWithFTPServer):
84
super(TestFTPServerUI, self).setUp()
85
self.old_factory = ui.ui_factory
86
# The following has the unfortunate side-effect of hiding any ouput
87
# during the tests (including pdb prompts). Feel free to comment them
88
# for debugging purposes but leave them in place, there are needed to
89
# run the tests without any console
90
self.old_stdout = sys.stdout
91
sys.stdout = tests.StringIOWrapper()
92
self.addCleanup(self.restoreUIFactory)
94
def restoreUIFactory(self):
95
ui.ui_factory = self.old_factory
96
sys.stdout = self.old_stdout
98
def test_prompt_for_password(self):
99
t = self.get_transport()
100
# Ensure that the test framework set the password
101
self.assertIsNot(t._password, None)
102
# Reset the password (get_url set the password to 'bar' so we
103
# reset it to None in the transport before the connection).
104
password = t._password
106
ui.ui_factory = tests.TestUIFactory(stdin=password+'\n')
107
# Ask the server to check the password
71
def _add_authorized_user(self, user, password):
108
72
server = self.get_server()
109
73
# FIXME: There should be a better way to declare authorized users and
110
74
# passwords to the server
111
75
authorizer = server._ftp_server.authorizer
112
authorizer.secured_user = t._user
76
authorizer.secured_user = user
113
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)
114
91
# Issue a request to the server to connect
115
92
t.has('whatever/not/existing')
116
93
# stdin should be empty (the provided password have been consumed)
117
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())