/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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
2790.1.1 by Vincent Ladeuil
Fix #137044 by prompting for a password if *none* is provided for ftp.
17
import sys
18
2790.1.2 by Vincent Ladeuil
Review feedback.
19
from bzrlib import (
20
    tests,
21
    transport,
22
    ui,
23
    )
24
25
26
class _MedusaFeature(tests.Feature):
2413.1.1 by John Arbash Meinel
Implement TestCaseWithFTPServer using the new shiny Feature mechanism.
27
    """Some tests want an FTP Server, check if one is available.
28
29
    Right now, the only way this is available is if 'medusa' is installed.
30
    """
31
32
    def _probe(self):
33
        try:
34
            import medusa
35
            import medusa.filesys
36
            import medusa.ftp_server
37
            return True
38
        except ImportError:
39
            return False
40
41
    def feature_name(self):
42
        return 'medusa'
43
44
MedusaFeature = _MedusaFeature()
45
46
2790.1.2 by Vincent Ladeuil
Review feedback.
47
class TestCaseWithFTPServer(tests.TestCaseWithTransport):
2413.1.1 by John Arbash Meinel
Implement TestCaseWithFTPServer using the new shiny Feature mechanism.
48
49
    _test_needs_features = [MedusaFeature]
50
51
    def setUp(self):
52
        from bzrlib.transport.ftp import FtpServer
53
        self.transport_server = FtpServer
54
        super(TestCaseWithFTPServer, self).setUp()
55
56
57
2790.1.2 by Vincent Ladeuil
Review feedback.
58
class TestCaseAFTP(tests.TestCaseWithTransport):
2413.1.2 by John Arbash Meinel
Clean up test ordering
59
    """Test aftp transport."""
60
61
    def test_aftp_degrade(self):
2790.1.2 by Vincent Ladeuil
Review feedback.
62
        t = transport.get_transport('aftp://host/path')
2413.1.2 by John Arbash Meinel
Clean up test ordering
63
        self.failUnless(t.is_active)
64
        parent = t.clone('..')
65
        self.failUnless(parent.is_active)
66
67
        self.assertEqual('aftp://host/path', t.abspath(''))
68
69
2413.1.1 by John Arbash Meinel
Implement TestCaseWithFTPServer using the new shiny Feature mechanism.
70
class TestFTPServer(TestCaseWithFTPServer):
71
72
    def test_basic_exists(self):
73
        url = self.get_url()
74
        self.assertStartsWith(url, 'ftp://')
75
76
        t = self.get_transport()
77
        t.put_bytes('foo', 'test bytes\n')
78
        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.
79
2917.1.1 by Vincent Ladeuil
Reproduce the bug.
80
    def test__reconnect(self):
81
        t = self.get_transport()
82
        t.put_bytes('foo', 'test bytes\n')
83
        self.assertEqual('test bytes\n', t.get_bytes('foo'))
84
        t._reconnect()
2917.1.3 by Vincent Ladeuil
Review feedback.
85
        t.put_bytes('foo', 'test more bytes\n')
86
        self.assertEqual('test more bytes\n', t.get_bytes('foo'))
2917.1.1 by Vincent Ladeuil
Reproduce the bug.
87
2790.1.1 by Vincent Ladeuil
Fix #137044 by prompting for a password if *none* is provided for ftp.
88
89
class TestFTPServerUI(TestCaseWithFTPServer):
90
91
    def setUp(self):
92
        super(TestFTPServerUI, self).setUp()
93
        self.old_factory = ui.ui_factory
94
        # The following has the unfortunate side-effect of hiding any ouput
95
        # during the tests (including pdb prompts). Feel free to comment them
96
        # for debugging purposes but leave them in place, there are needed to
97
        # run the tests without any console
98
        self.old_stdout = sys.stdout
2790.1.2 by Vincent Ladeuil
Review feedback.
99
        sys.stdout = tests.StringIOWrapper()
2790.1.1 by Vincent Ladeuil
Fix #137044 by prompting for a password if *none* is provided for ftp.
100
        self.addCleanup(self.restoreUIFactory)
101
102
    def restoreUIFactory(self):
103
        ui.ui_factory = self.old_factory
104
        sys.stdout = self.old_stdout
105
106
    def test_prompt_for_password(self):
107
        t = self.get_transport()
108
        # Ensure that the test framework set the password
109
        self.assertIsNot(t._password, None)
110
        # Reset the password (get_url set the password to 'bar' so we
111
        # reset it to None in the transport before the connection).
112
        password = t._password
113
        t._password = None
2790.1.2 by Vincent Ladeuil
Review feedback.
114
        ui.ui_factory = tests.TestUIFactory(stdin=password+'\n')
2790.1.1 by Vincent Ladeuil
Fix #137044 by prompting for a password if *none* is provided for ftp.
115
        # Ask the server to check the password
116
        server = self.get_server()
117
        # FIXME: There should be a better way to declare authorized users and
118
        # passwords to the server
119
        authorizer = server._ftp_server.authorizer
120
        authorizer.secured_user = t._user
121
        authorizer.secured_password = password
122
        # Issue a request to the server to connect
123
        t.has('whatever/not/existing')
124
        # stdin should be empty (the provided password have been consumed)
125
        self.assertEqual('', ui.ui_factory.stdin.readline())