/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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()
93
        ui.ui_factory = tests.TestUIFactory(stdin=self.password + '\n',
94
                                            stdout=tests.StringIOWrapper())
95
        # Issue a request to the server to connect
96
        t.put_bytes('foo', 'test bytes\n')
97
        self.assertEqual('test bytes\n', t.get_bytes('foo'))
98
        # Only the password should've been read
99
        self.assertEqual('', ui.ui_factory.stdin.readline())
100
2790.1.1 by Vincent Ladeuil
Fix #137044 by prompting for a password if *none* is provided for ftp.
101
    def test_prompt_for_password(self):
102
        t = self.get_transport()
3508.1.13 by Vincent Ladeuil
Fix last failing tests under python2.6.
103
        ui.ui_factory = tests.TestUIFactory(stdin=self.password+'\n',
2900.2.12 by Vincent Ladeuil
Since all schemes query AuthenticationConfig then prompt user, make that
104
                                            stdout=tests.StringIOWrapper())
2790.1.1 by Vincent Ladeuil
Fix #137044 by prompting for a password if *none* is provided for ftp.
105
        # Issue a request to the server to connect
106
        t.has('whatever/not/existing')
107
        # stdin should be empty (the provided password have been consumed)
108
        self.assertEqual('', ui.ui_factory.stdin.readline())
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
109
110
    def test_no_prompt_for_password_when_using_auth_config(self):
111
        t = self.get_transport()
2900.2.12 by Vincent Ladeuil
Since all schemes query AuthenticationConfig then prompt user, make that
112
        ui.ui_factory = tests.TestUIFactory(stdin='precious\n',
113
                                            stdout=tests.StringIOWrapper())
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
114
        # Create a config file with the right password
115
        conf = config.AuthenticationConfig()
2900.2.14 by Vincent Ladeuil
More tests.
116
        conf._get_config().update({'ftptest': {'scheme': 'ftp',
3508.1.13 by Vincent Ladeuil
Fix last failing tests under python2.6.
117
                                               'user': self.user,
118
                                               'password': self.password}})
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
119
        conf._save()
120
        # Issue a request to the server to connect
121
        t.put_bytes('foo', 'test bytes\n')
122
        self.assertEqual('test bytes\n', t.get_bytes('foo'))
123
        # stdin should have  been left untouched
124
        self.assertEqual('precious\n', ui.ui_factory.stdin.readline())
3508.1.17 by Vincent Ladeuil
Allows empty passwords with pyftpdlib ftp test server.
125
126
    def test_empty_password(self):
127
        # Override the default user/password from setUp
128
        self.user = 'jim'
129
        self.password = ''
130
        self.get_server().add_user(self.user, self.password)
131
        t = self.get_transport()
132
        ui.ui_factory = tests.TestUIFactory(stdin=self.password+'\n',
133
                                            stdout=tests.StringIOWrapper())
134
        # Issue a request to the server to connect
135
        t.has('whatever/not/existing')
136
        # stdin should be empty (the provided password have been consumed),
137
        # even if the password is empty, it's followed by a newline.
138
        self.assertEqual('', ui.ui_factory.stdin.readline())
139