~bzr-pqm/bzr/bzr.dev

2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
1
# Copyright (C) 2006 Canonical Ltd
2
#
1952.1.1 by John Arbash Meinel
Ghozzy: Fix Bzr's support of Active FTP (aftp://)
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.
7
#
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.
12
#
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1952.1.1 by John Arbash Meinel
Ghozzy: Fix Bzr's support of Active FTP (aftp://)
16
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
17
from cStringIO import StringIO
4222.3.14 by Jelmer Vernooij
Fix missing import.
18
import getpass
2790.1.1 by Vincent Ladeuil
Fix #137044 by prompting for a password if *none* is provided for ftp.
19
import sys
20
2790.1.2 by Vincent Ladeuil
Review feedback.
21
from bzrlib import (
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
22
    config,
2790.1.2 by Vincent Ladeuil
Review feedback.
23
    tests,
24
    transport,
25
    ui,
26
    )
27
3508.1.7 by Vincent Ladeuil
Prepare test framework for pyftpdlib injection.
28
from bzrlib.tests import ftp_server
29
2790.1.2 by Vincent Ladeuil
Review feedback.
30
31
class TestCaseWithFTPServer(tests.TestCaseWithTransport):
2413.1.1 by John Arbash Meinel
Implement TestCaseWithFTPServer using the new shiny Feature mechanism.
32
3508.1.7 by Vincent Ladeuil
Prepare test framework for pyftpdlib injection.
33
    _test_needs_features = [ftp_server.FTPServerFeature]
2413.1.1 by John Arbash Meinel
Implement TestCaseWithFTPServer using the new shiny Feature mechanism.
34
35
    def setUp(self):
3508.1.23 by Vincent Ladeuil
Fix as per Martin's review.
36
        self.transport_server = ftp_server.FTPTestServer
2413.1.1 by John Arbash Meinel
Implement TestCaseWithFTPServer using the new shiny Feature mechanism.
37
        super(TestCaseWithFTPServer, self).setUp()
38
39
2790.1.2 by Vincent Ladeuil
Review feedback.
40
class TestCaseAFTP(tests.TestCaseWithTransport):
2413.1.2 by John Arbash Meinel
Clean up test ordering
41
    """Test aftp transport."""
42
43
    def test_aftp_degrade(self):
2790.1.2 by Vincent Ladeuil
Review feedback.
44
        t = transport.get_transport('aftp://host/path')
2413.1.2 by John Arbash Meinel
Clean up test ordering
45
        self.failUnless(t.is_active)
46
        parent = t.clone('..')
47
        self.failUnless(parent.is_active)
48
49
        self.assertEqual('aftp://host/path', t.abspath(''))
50
51
3508.1.23 by Vincent Ladeuil
Fix as per Martin's review.
52
class TestFTPTestServer(TestCaseWithFTPServer):
2413.1.1 by John Arbash Meinel
Implement TestCaseWithFTPServer using the new shiny Feature mechanism.
53
54
    def test_basic_exists(self):
55
        url = self.get_url()
56
        self.assertStartsWith(url, 'ftp://')
57
58
        t = self.get_transport()
59
        t.put_bytes('foo', 'test bytes\n')
60
        self.assertEqual('test bytes\n', t.get_bytes('foo'))
2790.1.1 by Vincent Ladeuil
Fix #137044 by prompting for a password if *none* is provided for ftp.
61
2917.1.1 by Vincent Ladeuil
Reproduce the bug.
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'))
66
        t._reconnect()
2917.1.3 by Vincent Ladeuil
Review feedback.
67
        t.put_bytes('foo', 'test more bytes\n')
68
        self.assertEqual('test more bytes\n', t.get_bytes('foo'))
2917.1.1 by Vincent Ladeuil
Reproduce the bug.
69
2790.1.1 by Vincent Ladeuil
Fix #137044 by prompting for a password if *none* is provided for ftp.
70
3508.1.23 by Vincent Ladeuil
Fix as per Martin's review.
71
class TestFTPTestServerUI(TestCaseWithFTPServer):
2790.1.1 by Vincent Ladeuil
Fix #137044 by prompting for a password if *none* is provided for ftp.
72
3508.1.13 by Vincent Ladeuil
Fix last failing tests under python2.6.
73
    def setUp(self):
3508.1.23 by Vincent Ladeuil
Fix as per Martin's review.
74
        super(TestFTPTestServerUI, self).setUp()
3508.1.13 by Vincent Ladeuil
Fix last failing tests under python2.6.
75
        self.user = 'joe'
76
        self.password = 'secret'
77
        self.get_server().add_user(self.user, self.password)
78
79
    def get_url(self, relpath=None):
80
        """Overrides get_url to inject our user."""
3508.1.23 by Vincent Ladeuil
Fix as per Martin's review.
81
        base = super(TestFTPTestServerUI, self).get_url(relpath)
3508.1.13 by Vincent Ladeuil
Fix last failing tests under python2.6.
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)
86
        return url
2900.2.15 by Vincent Ladeuil
AuthenticationConfig can be queried for logins too (first step).
87
4222.3.13 by Jelmer Vernooij
Add tests to ensure sftp and ftp don't prompt for usernames.
88
    def test_no_prompt_for_username(self):
89
        """ensure getpass.getuser() is used if there's no username in the 
90
        configuration.""",
91
        self.get_server().add_user(getpass.getuser(), self.password)
92
        t = self.get_transport()
4449.3.46 by Martin Pool
Update ftp tests to use CannedInputUIFactory
93
        ui.ui_factory = ui.CannedInputUIFactory([self.password])
4222.3.13 by Jelmer Vernooij
Add tests to ensure sftp and ftp don't prompt for usernames.
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
4449.3.46 by Martin Pool
Update ftp tests to use CannedInputUIFactory
98
        ui.ui_factory.assert_all_input_consumed()
4222.3.13 by Jelmer Vernooij
Add tests to ensure sftp and ftp don't prompt for usernames.
99
2790.1.1 by Vincent Ladeuil
Fix #137044 by prompting for a password if *none* is provided for ftp.
100
    def test_prompt_for_password(self):
101
        t = self.get_transport()
4449.3.46 by Martin Pool
Update ftp tests to use CannedInputUIFactory
102
        ui.ui_factory = ui.CannedInputUIFactory([self.password])
2790.1.1 by Vincent Ladeuil
Fix #137044 by prompting for a password if *none* is provided for ftp.
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)
4449.3.46 by Martin Pool
Update ftp tests to use CannedInputUIFactory
106
        ui.ui_factory.assert_all_input_consumed()
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
107
108
    def test_no_prompt_for_password_when_using_auth_config(self):
109
        t = self.get_transport()
4449.3.46 by Martin Pool
Update ftp tests to use CannedInputUIFactory
110
        ui.ui_factory = ui.CannedInputUIFactory([])
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
111
        # Create a config file with the right password
112
        conf = config.AuthenticationConfig()
2900.2.14 by Vincent Ladeuil
More tests.
113
        conf._get_config().update({'ftptest': {'scheme': 'ftp',
3508.1.13 by Vincent Ladeuil
Fix last failing tests under python2.6.
114
                                               'user': self.user,
115
                                               'password': self.password}})
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
116
        conf._save()
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'))
3508.1.17 by Vincent Ladeuil
Allows empty passwords with pyftpdlib ftp test server.
120
121
    def test_empty_password(self):
122
        # Override the default user/password from setUp
123
        self.user = 'jim'
124
        self.password = ''
125
        self.get_server().add_user(self.user, self.password)
126
        t = self.get_transport()
4449.3.47 by Martin Pool
Typo correction
127
        ui.ui_factory = ui.CannedInputUIFactory([self.password])
3508.1.17 by Vincent Ladeuil
Allows empty passwords with pyftpdlib ftp test server.
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.
4449.3.46 by Martin Pool
Update ftp tests to use CannedInputUIFactory
132
        ui.ui_factory.assert_all_input_consumed()