1
# Copyright (C) 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from cStringIO import StringIO
28
from bzrlib.tests import ftp_server
31
class TestCaseWithFTPServer(tests.TestCaseWithTransport):
33
_test_needs_features = [ftp_server.FTPServerFeature]
36
self.transport_server = ftp_server.FTPTestServer
37
super(TestCaseWithFTPServer, self).setUp()
40
class TestCaseAFTP(tests.TestCaseWithTransport):
41
"""Test aftp transport."""
43
def test_aftp_degrade(self):
44
t = transport.get_transport('aftp://host/path')
45
self.failUnless(t.is_active)
46
parent = t.clone('..')
47
self.failUnless(parent.is_active)
49
self.assertEqual('aftp://host/path', t.abspath(''))
52
class TestFTPTestServer(TestCaseWithFTPServer):
54
def test_basic_exists(self):
56
self.assertStartsWith(url, 'ftp://')
58
t = self.get_transport()
59
t.put_bytes('foo', 'test bytes\n')
60
self.assertEqual('test bytes\n', t.get_bytes('foo'))
62
def test__reconnect(self):
63
t = self.get_transport()
64
t.put_bytes('foo', 'test bytes\n')
65
self.assertEqual('test bytes\n', t.get_bytes('foo'))
67
t.put_bytes('foo', 'test more bytes\n')
68
self.assertEqual('test more bytes\n', t.get_bytes('foo'))
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()
100
def test_prompt_for_password(self):
101
t = self.get_transport()
102
ui.ui_factory = ui.CannedInputUIFactory([self.password])
103
# Issue a request to the server to connect
104
t.has('whatever/not/existing')
105
# stdin should be empty (the provided password have been consumed)
106
ui.ui_factory.assert_all_input_consumed()
108
def test_no_prompt_for_password_when_using_auth_config(self):
109
t = self.get_transport()
110
ui.ui_factory = ui.CannedInputUIFactory([])
111
# Create a config file with the right password
112
conf = config.AuthenticationConfig()
113
conf._get_config().update({'ftptest': {'scheme': 'ftp',
115
'password': self.password}})
117
# Issue a request to the server to connect
118
t.put_bytes('foo', 'test bytes\n')
119
self.assertEqual('test bytes\n', t.get_bytes('foo'))
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()