~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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
17
from cStringIO import StringIO
2790.1.1 by Vincent Ladeuil
Fix #137044 by prompting for a password if *none* is provided for ftp.
18
import sys
19
2790.1.2 by Vincent Ladeuil
Review feedback.
20
from bzrlib import (
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
21
    config,
2790.1.2 by Vincent Ladeuil
Review feedback.
22
    tests,
23
    transport,
24
    ui,
25
    )
26
27
28
class TestCaseWithFTPServer(tests.TestCaseWithTransport):
2413.1.1 by John Arbash Meinel
Implement TestCaseWithFTPServer using the new shiny Feature mechanism.
29
2917.3.1 by Vincent Ladeuil
Separate transport from test server.
30
    _test_needs_features = [tests.FTPServerFeature]
2413.1.1 by John Arbash Meinel
Implement TestCaseWithFTPServer using the new shiny Feature mechanism.
31
32
    def setUp(self):
2949.3.2 by Vincent Ladeuil
Rename FTPServer.py to ftp_server.py.
33
        from bzrlib.tests import ftp_server
34
        self.transport_server = ftp_server.FTPServer
2413.1.1 by John Arbash Meinel
Implement TestCaseWithFTPServer using the new shiny Feature mechanism.
35
        super(TestCaseWithFTPServer, self).setUp()
36
37
2790.1.2 by Vincent Ladeuil
Review feedback.
38
class TestCaseAFTP(tests.TestCaseWithTransport):
2413.1.2 by John Arbash Meinel
Clean up test ordering
39
    """Test aftp transport."""
40
41
    def test_aftp_degrade(self):
2790.1.2 by Vincent Ladeuil
Review feedback.
42
        t = transport.get_transport('aftp://host/path')
2413.1.2 by John Arbash Meinel
Clean up test ordering
43
        self.failUnless(t.is_active)
44
        parent = t.clone('..')
45
        self.failUnless(parent.is_active)
46
47
        self.assertEqual('aftp://host/path', t.abspath(''))
48
49
2413.1.1 by John Arbash Meinel
Implement TestCaseWithFTPServer using the new shiny Feature mechanism.
50
class TestFTPServer(TestCaseWithFTPServer):
51
52
    def test_basic_exists(self):
53
        url = self.get_url()
54
        self.assertStartsWith(url, 'ftp://')
55
56
        t = self.get_transport()
57
        t.put_bytes('foo', 'test bytes\n')
58
        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.
59
2917.1.1 by Vincent Ladeuil
Reproduce the bug.
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'))
64
        t._reconnect()
2917.1.3 by Vincent Ladeuil
Review feedback.
65
        t.put_bytes('foo', 'test more bytes\n')
66
        self.assertEqual('test more bytes\n', t.get_bytes('foo'))
2917.1.1 by Vincent Ladeuil
Reproduce the bug.
67
2790.1.1 by Vincent Ladeuil
Fix #137044 by prompting for a password if *none* is provided for ftp.
68
69
class TestFTPServerUI(TestCaseWithFTPServer):
70
2900.2.15 by Vincent Ladeuil
AuthenticationConfig can be queried for logins too (first step).
71
    def _add_authorized_user(self, user, password):
72
        server = self.get_server()
73
        # FIXME: There should be a better way to declare authorized users and
74
        # passwords to the server
75
        authorizer = server._ftp_server.authorizer
76
        authorizer.secured_user = user
77
        authorizer.secured_password = password
78
2790.1.1 by Vincent Ladeuil
Fix #137044 by prompting for a password if *none* is provided for ftp.
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
86
        t._password = None
2900.2.12 by Vincent Ladeuil
Since all schemes query AuthenticationConfig then prompt user, make that
87
        ui.ui_factory = tests.TestUIFactory(stdin=password+'\n',
88
                                            stdout=tests.StringIOWrapper())
2790.1.1 by Vincent Ladeuil
Fix #137044 by prompting for a password if *none* is provided for ftp.
89
        # Ask the server to check the password
2900.2.15 by Vincent Ladeuil
AuthenticationConfig can be queried for logins too (first step).
90
        self._add_authorized_user(t._user, password)
2790.1.1 by Vincent Ladeuil
Fix #137044 by prompting for a password if *none* is provided for ftp.
91
        # Issue a request to the server to connect
92
        t.has('whatever/not/existing')
93
        # stdin should be empty (the provided password have been consumed)
94
        self.assertEqual('', ui.ui_factory.stdin.readline())
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
95
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
101
        t._password = None
2900.2.12 by Vincent Ladeuil
Since all schemes query AuthenticationConfig then prompt user, make that
102
        ui.ui_factory = tests.TestUIFactory(stdin='precious\n',
103
                                            stdout=tests.StringIOWrapper())
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
104
        # Ask the server to check the password
2900.2.15 by Vincent Ladeuil
AuthenticationConfig can be queried for logins too (first step).
105
        self._add_authorized_user(t._user, password)
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
106
107
        # Create a config file with the right password
108
        conf = config.AuthenticationConfig()
2900.2.14 by Vincent Ladeuil
More tests.
109
        conf._get_config().update({'ftptest': {'scheme': 'ftp',
110
                                               'user': t._user,
2900.2.5 by Vincent Ladeuil
ake ftp aware of authentication config.
111
                                               'password': password}})
112
        conf._save()
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())